Skip to content
Sign in
Theme

Your first output: an LED and a resistor

Ohm's law, what a GPIO pin can drive, and why every LED needs a series resistor.

Read this first

A GPIO pin (general purpose input/output) is a pin your program can set high (3.3 V on most modern boards, 5 V on an Arduino Uno) or low (0 V). That is the whole trick behind every light, relay and motor driver you will ever control: a pin goes high or low, and a circuit turns that into something physical.

The one formula you need is Ohm's law: V = I x R. Voltage in volts, current in amps, resistance in ohms. Rearranged: I = V / R. If 3.3 V sits across a 470 ohm resistor, the current is 3.3 / 470 = 0.007 A, which is 7 mA.

An LED is not a resistor. It drops a roughly fixed voltage, called the forward voltage: about 2.0 V for red, about 3.0 V for blue and white. Whatever is left of the supply voltage falls across the series resistor, and that resistor sets the current. For a red LED on a 3.3 V pin at 10 mA: (3.3 - 2.0) / 0.010 = 130 ohms. Without the resistor the LED takes far too much current and either it or the pin dies.

A pin can only supply a small current, typically around 8 to 20 mA. Enough for one LED. Not enough for a motor, a buzzer or a relay coil, which is why those get a transistor or a driver chip later.

A pin can be configured in two output styles. Push-pull is the normal one: the pin actively drives high and actively drives low. Open-drain only drives low; when it is set to 1 it lets go of the line and something else, usually a pull-up resistor, has to bring the line high. Open-drain is what I2C uses, and you will meet it again in a later lesson.

To remember

  • V = I x R. Current through a resistor is the voltage across it divided by its resistance.
  • An LED drops a fixed forward voltage. The series resistor takes the rest and sets the current.
  • Resistor = (supply - LED forward voltage) / wanted current.
  • A GPIO pin gives only a few milliamps. Bigger loads need a transistor or a driver.
  • Push-pull drives high and low. Open-drain only drives low and needs a pull-up for the high level.

Check what you read

3 questions from the question bank on the ideas above. Each one comes with an explanation after you answer.

Sign in to answer the questions. Your answers count towards your level.

Sign in

The lesson text and the project are free to read without an account.

Build this

Traffic light on a breadboard

You need

  • Your board from lesson 1.
  • A breadboard and a few jumper wires.
  • Three LEDs (red, yellow, green) and three resistors between 220 and 330 ohms.

Steps

  1. Work out the resistor for one LED at about 10 mA using the formula above. On a 3.3 V board you get about 130 ohms; a 220 to 330 ohm part gives only 4 to 6 mA, which is still plenty for an indicator LED and is safer for the pin. On a 5 V Uno the same parts give about 10 mA.
  2. Wire one LED: pin to resistor, resistor to LED anode (long leg), LED cathode (short leg) to ground. Make it blink from your code.
  3. Add the other two LEDs on two more pins.
  4. Program the sequence: green 3 s, yellow 1 s, red 3 s, repeat.

Done when

The three LEDs cycle like a traffic light, and you can say out loud how much current each LED draws.