First output: an LED and a resistor
Ohm's law, how much current a GPIO pin can supply, and why every LED needs a series resistor.
Explanation
A GPIO pin (general purpose input/output) is a pin your program can set high or low. High is 3.3 V on most modern boards and 5 V on an Arduino Uno; low is 0 V. Every light, relay and motor driver you control works this way: a pin goes high or low, and a circuit turns that into a physical action.
The formula you need is Ohm's law: V = I x R, with voltage in volts, current in amps and resistance in ohms. Rearranged, it gives I = V / R. If 3.3 V is across a 470 ohm resistor, the current is 3.3 / 470 = 0.007 A, which is 7 mA.
An LED does not behave like a resistor. It drops a roughly fixed voltage, called the forward voltage: about 2.0 V for red and about 3.0 V for blue and white. The rest 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 draws far too much current, and the LED or the pin can be damaged.
A pin can supply only a small current, typically 8 to 20 mA. This is enough for an LED, but not for a motor, a buzzer or a relay coil. Those loads need a transistor or a driver chip.
A pin can be configured in 2 output modes. Push-pull is the normal mode: the pin actively drives both high and low. Open-drain drives only low. When it is set to 1, it releases the line, and another component, usually a pull-up resistor, has to bring the line high. I2C uses open-drain, and a later lesson covers it.
Key points
- V = I x R. The 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) / required current.
- A GPIO pin supplies only a few milliamps. Larger loads need a transistor or a driver.
- Push-pull drives high and low. Open-drain drives only low and needs a pull-up for the high level.
Check your understanding
3 questions on this lesson. An explanation follows each answer.
Sign in to answer the questions. Your answers count towards your level.
You can read the lesson and the project without an account.
Project
Traffic light on a breadboard
What you need
- Your board from lesson 1.
- A breadboard and a few jumper wires.
- 3 LEDs (red, yellow and green) and 3 resistors between 220 and 330 ohms.
Steps
- 1Calculate the resistor value for an LED at about 10 mA, using the formula above. On a 3.3 V board the result is about 130 ohms. A 220 to 330 ohm resistor gives only 4 to 6 mA, which is enough for an indicator LED and safer for the pin. On a 5 V Uno, the same resistors give about 10 mA.
- 2Wire the first LED: pin to resistor, resistor to the LED anode (long leg), LED cathode (short leg) to ground. Make it blink from your code.
- 3Add the other 2 LEDs on 2 more pins.
- 4Program the sequence: green for 3 s, yellow for 1 s, red for 3 s, then repeat.
Done when
The 3 LEDs cycle like a traffic light, and you can state how much current each LED draws.