Aim. To blink an LED connected to an Arduino digital pin.
Components. Arduino UNO R3, USB cable, 1 LED, 1 × 220 Ω resistor, breadboard, 2 jumper wires.
Concepts used. Digital output, pinMode(), digitalWrite(), delay(), Ohm's law (resistor value from Chapter 1).
Wiring.
| From | To |
|---|---|
| Arduino pin 8 | One end of the 220 Ω resistor |
| Other end of the resistor | LED anode (long leg) |
| LED cathode (short leg) | Arduino GND |
Resistor check. (5 V − 2 V) / 220 Ω ≈ 13.6 mA, safe for the LED and the pin.
Program.
Arduino C/C++
const int ledPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // LED ON
delay(500); // wait 0.5 s
digitalWrite(ledPin, LOW); // LED OFF
delay(500);
}Expected result. The LED blinks on for half a second and off for half a second.
Try these changes.
- Make it blink faster (100 ms) and slower (2000 ms).
- Blink an SOS pattern in Morse code (three short, three long, three short).
- Use the internal LED (pin 13) and the external LED together.
Troubleshooting. LED off all the time: LED is reversed, wire loose, or wrong pin. LED always on: check the code uploaded properly.
Viva questions
- Why is the resistor needed?
- What does
pinModedo? - What happens if
delay()is removed?
Show viva answers
- To limit current and protect the LED and the pin.
- It configures a pin as input or output.
- The LED switches on and off so quickly (thousands of times per second) that it looks constantly dim.