Electricity flows in a loop
Electricity only flows round a complete loop, called a circuit. Break the loop anywhere and it stops.
On an Arduino, 5V is the + side. GND (ground) is the − side, at 0 volts.
★RememberCurrent flows from +, round the loop, back to −. Every part must sit in that loop.
The parts
- Arduino Uno: a small board with a microcontroller, a tiny computer on one chip. Wires join it at its numbered pins.
- Breadboard: a board for building circuits without solder. The long + and − rails carry power along its edges.
- LED (light-emitting diode): a small light that lets current through one way only.
- Resistor: limits the current. It works either way round.
- Pushbutton: a switch. Pressed, it closes the loop. Let go, it breaks the loop.
An LED's anode (+) must face the + side. Its cathode (−) faces the − side.
On a real LED the anode is the longer leg. In Tinkercad it is the bent leg.
!Watch outThe 5 holes in one short column of the breadboard are joined inside. If a part has both legs in one column, the current skips it.
Why an LED needs a resistor
With no resistor, too much current flows. The LED burns out, and the Arduino pin can be damaged.
Ohm's law links the three words: V = I × R. Turned round, it tells you the resistor you need.
- Take the LED's share away from the supply: 5 − 2 = 3 V.
- Divide by the current: 3 ÷ 0.02 = 150 Ω.
- Use 150 Ω, or the next size up. Kits use 220 Ω.
?Why?Why is 220 Ω also safe?
A bigger resistor lets less current through: 3 ÷ 220 ≈ 0.014 A, or 14 mA. The LED is a little dimmer, and safer.
★RememberKeep the current from each Arduino pin at about 20 mA or less.
Blink: the first program
Wire pin 13 to the resistor instead of the button. Now the Arduino flips the switch.
const int LED_PIN = 13; // the LED is wired to pin 13
void setup() {
pinMode(LED_PIN, OUTPUT); // pin 13 will send electricity out
}
void loop() {
digitalWrite(LED_PIN, HIGH); // switch ON: 5 volts, a 1
delay(500); // wait 500 milliseconds
digitalWrite(LED_PIN, LOW); // switch OFF: 0 volts, a 0
delay(500); // wait again, then loop() starts over
}setup() runs once, when the board switches on. pinMode makes pin 13 an output.loop() runs again and again, for ever.digitalWrite(LED_PIN, HIGH) puts 5 V on the pin. That is a 1, so the LED lights.delay(500) waits 500 milliseconds: half a second.LOW puts 0 V on the pin. That is a 0, so the LED goes off.
With a micro:bit, the same idea in MicroPython uses the screen of lights:
from microbit import *
while True:
display.show(Image.HEART)
sleep(500)
display.clear()
sleep(500)e.g.Worked exampleA blue LED uses about 3 V. You want 20 mA through it from the Arduino's 5 V. Which resistor do you need?
- Write what you know: Vs = 5 V, Vf = 3 V, I = 20 mA.
- Change the current to amps: 20 ÷ 1000 = 0.02 A.
- Take the LED's share away: 5 − 3 = 2 V.
- Divide by the current: 2 ÷ 0.02 = 100 Ω.
- 100 Ω is a standard size, so use it.
- A 220 Ω resistor also works: 2 ÷ 220 ≈ 0.009 A, or 9 mA. The LED is just dimmer.