pin 8 (PB0)ATmega328PDDRB00PORTB005V0Vpin 8330 Ω0.0 mALED1void setup() {2 pinMode(8, OUTPUT);3 digitalWrite(8, HIGH);4 delay(5000);5 digitalWrite(8, LOW);6}7void loop() {}1.9 V3.1 Vpin max 40 mA310 → 330 Ω110
Back: the ATmega328P, its two register bits and the pin 8 switchBoard: resistor, ammeter, LED and ground in one loop; front: the sketchTokens: bits written into the registers; moving dashes: current flowingPin 8 of an Arduino Uno R3 (ATmega328P) drives a red LED through a 330 Ω resistor. pinMode sets DDRB0 to 1 (output); digitalWrite HIGH sets PORTB0 to 1 and the pin's switch joins it to 5 V, so 9.4 mA flows and the LED glows; LOW sets PORTB0 to 0 and it stops. The resistor takes the 3.1 V the LED does not, which keeps the current far below the pin's 40 mA maximum.

The parts

  1. Pin 8 on the Uno's ATmega328P chip can work as an output. Inside, it acts like a switch that joins the pin to 5 volts or to 0 volts, called ground. At power on it joins neither: every pin starts as an input.

Switching it on

Sizing the resistor

Switching it off

What happens

;; step 1/7 · pin
board   Arduino Uno R3, ATmega328P, 5 V
pin 8   port B, bit 0  (PB0)
output  a switch: pin -> 5 V (HIGH) or pin -> 0 V (LOW)
reset   the switch joins neither: the pin is an input

In the Arduino core, pinMode(pin, OUTPUT) only sets the DDRB0 bit. PORTB0 keeps its value, which is 0 after reset, so the pin starts LOW. digitalWrite then sets or clears PORTB0. Under load the pin gives a little less than 5 V: the datasheet guarantees at least 4.2 V at 20 mA (85 °C), and LEDs vary (this one is 1.9 V typical, 2.3 V maximum). So the real current is close to, and slightly below, 9.4 mA.