Chapter 9 · Arduino Practicals and Programming Projects

Practical 1Blinking LED

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.

FromTo
Arduino pin 8One end of the 220 Ω resistor
Other end of the resistorLED anode (long leg)
LED cathode (short leg)Arduino GND
Arduino UNOPin 8GND220 ΩLEDanode (+)cathode
Practical 1 circuit

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.

  1. Make it blink faster (100 ms) and slower (2000 ms).
  2. Blink an SOS pattern in Morse code (three short, three long, three short).
  3. 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

  1. Why is the resistor needed?
  2. What does pinMode do?
  3. What happens if delay() is removed?
Show viva answers
  1. To limit current and protect the LED and the pin.
  2. It configures a pin as input or output.
  3. The LED switches on and off so quickly (thousands of times per second) that it looks constantly dim.