Digital output: driving an LED
About this lesson
A digital output pin is a switch inside the microcontroller: it joins the pin to 5 V (HIGH) or to 0 V (LOW). Follow pin 8 of an Arduino Uno R3 as pinMode sets its direction bit, digitalWrite sets its output bit, and current flows through a 330 Ω resistor and a red LED. Then use Ohm's law to see why the LED gets 9.4 mA and why the resistor is never optional. The Hardware lessons “Ohm's law”, “Diodes: one-way streets” and “Transistors: the switch that changed everything” explain the parts; here we drive them from firmware.
A pin is a switch
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.
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
Read the lesson step by step
A pin is a switch
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.
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 inputThe LED circuit
We wire pin 8 to a 330 Ω resistor, then a red LED, then ground. An LED, a light-emitting diode, glows only when current flows through it the right way: in at the long leg (anode), out at the short leg (cathode).
pin 8 -> 330 Ω -> ammeter -> LED anode (long leg) LED cathode (short leg) -> GND LED lights only for current from anode to cathode now the pin is an input: no current, LED darkpinMode: make it an output
pinMode(8, OUTPUT) writes a 1 into DDRB0, a bit in the direction register. A register is a tiny memory cell wired to hardware. The switch now works. PORTB0, the output bit, is still 0, so the pin is held at 0 V.
line 2 pinMode(8, OUTPUT); DDRB0 0 -> 1 (1 = output) PORTB0 0 (its value after reset) switch pin -> 0 VHIGH: 5 volts, light on
digitalWrite(8, HIGH) writes 1 into PORTB0. The switch joins pin 8 to 5 V. With 5 V at one end of the circuit and 0 V at the other, current flows through the resistor and the LED into ground, and the LED glows.
line 3 digitalWrite(8, HIGH); PORTB0 0 -> 1 switch pin -> 5 V current pin -> 330 Ω -> LED -> GND LED onHow much current flows?
The LED takes about 1.9 V, its forward voltage, leaving 5 − 1.9 = 3.1 V for the resistor. Ohm's law: I = 3.1 V ÷ 330 Ω ≈ 9.4 mA. The ammeter (a current meter) reads 9.4 mA, below the pin's 20 mA rating.
LED 1.9 V (forward voltage, WP7113ID, typ at 10 mA) resistor 5 - 1.9 = 3.1 V Ohm's law I = V / R = 3.1 / 330 = 9.39 mA rated pin 20 mA, LED 30 mA maxWhy the resistor matters
To choose R, divide the spare volts by the current wanted: 3.1 V ÷ 10 mA = 310 Ω, so use the next standard size, 330 Ω. With no resistor, only the driver limits the current, which can pass 40 mA and damage the pin.
want about 10 mA R 3.1 V / 10 mA = 310 Ω -> 330 Ω (standard E12 value) pin 40 mA absolute maximum, 20 mA rated LED 30 mA absolute maximum no R nothing but the driver limits the currentLOW: 0 volts, light off
delay(5000) waits five seconds. Then digitalWrite(8, LOW) writes 0 into PORTB0, and the switch joins pin 8 to 0 V. Both ends of the circuit are now at 0 V, so no current flows and the LED goes dark.
line 4 delay(5000); five seconds, LED on line 5 digitalWrite(8, LOW); PORTB0 1 -> 0 switch pin -> 0 V current 0 mA, LED off
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01On the Arduino Uno R3, what does digitalWrite(8, HIGH) do to an output pin?
On an OUTPUT pin, HIGH sets PORTB0 to 1 and the driver joins the pin to the supply, 5 V on the Uno (3.3 V on 3.3 V boards). LOW joins it to 0 V.
02What does pinMode(8, OUTPUT) change inside the ATmega328P?
pinMode only sets the direction bit DDRB0 to 1, which makes the pin an output. PORTB0 is still 0 after reset, so the pin sits at 0 V until digitalWrite sets it.
03A red LED drops 1.9 V and is in series with 330 Ω on a 5 V pin. About how much current flows?
The resistor gets 5 − 1.9 = 3.1 V, and Ohm's law gives I = 3.1 V ÷ 330 Ω ≈ 9.4 mA.
04You want about 10 mA through the same LED. Which standard resistor do you pick?
R = 3.1 V ÷ 10 mA = 310 Ω. That is not a standard value, so take the next one up, 330 Ω, which keeps the current just under 10 mA. 220 Ω would give about 14 mA; 10 kΩ only about 0.3 mA, a very dim glow.
05Why must an LED on a pin never be wired without a resistor?
An LED's voltage stays near 1.9 V whatever the current, so it cannot limit the current itself. The datasheet's absolute maximum is 40 mA per pin (and 30 mA for this LED); going past it can destroy the pin's output transistors.
Glossary
- GPIO
- General-purpose input/output: a chip pin your firmware can set as an input or an output.
- Digital output
- A pin the firmware drives HIGH (joined to the supply, 5 V on an Uno) or LOW (joined to 0 V, ground).
- Register
- A tiny memory cell wired to the hardware. Writing a bit into it changes what the chip does, such as a pin's direction.
- DDRB and PORTB
- The ATmega328P's port B direction register (1 = output) and output register (1 = high, 0 = low). Arduino pin 8 is bit 0 of both.
- LED
- Light-emitting diode: it glows when current flows from its anode (long leg) to its cathode (short leg).
- Forward voltage
- The roughly fixed voltage an LED takes while lit, about 1.9 V for this red LED.
- Current-limiting resistor
- A resistor in series with an LED that sets the current to a safe value, sized with Ohm's law: R = (supply − forward voltage) ÷ current.
- Absolute maximum rating
- A datasheet limit that can damage the part if exceeded, even briefly. It is not a value to work at.
Notes and sources
- On the Arduino Uno R3, Arduino pin 8 is bit 0 of port B (PB0) of the ATmega328P. The "switch" is really a pair of transistors in the output driver; the Hardware lesson "Transistors: the switch that changed everything" shows how a transistor switches.
- 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.
- 40 mA is the datasheet's absolute maximum per pin: a limit that can damage the chip, not a working value. The datasheet only ensures the output voltage up to 20 mA, and the Arduino docs suggest 470 Ω or 1 kΩ on outputs unless you need the full current. The next lesson on pin current limits shows how to drive bigger loads with a transistor.
- Microchip ATmega48A/PA/88A/PA/168A/PA/328/P datasheet (DS40002061B): I/O ports, absolute maximum ratings, DC characteristics
- Arduino docs: Digital Pins (source of docs.arduino.cc/learn/microcontrollers/digital-pins)
- Arduino language reference: digitalWrite() (source)
- Arduino language reference: pinMode() (source)
- Arduino AVR core: wiring_digital.c (pinMode and digitalWrite)
- Arduino AVR core: variants/standard/pins_arduino.h (Uno pin to port map)
- Kingbright WP7113ID 5 mm red LED datasheet
- J. W. Valvano, Embedded Systems: Shape the World, ch. 8: Switches and LEDs (UT Austin)
Quiz Unlocks after the last step.