A blinking LED is useful as a heartbeat: it tells you a controller is alive even when no screen is attached. This exercise teaches output configuration, polarity, current limiting, and timing. The same pattern can later signal a waiting sensor, a lost connection, or an unacknowledged alert.
Wire the indicator
Unplug USB before changing the circuit. Current must pass through the resistor and the LED; the resistor can be on either side of the LED.
| Connection | Destination or purpose |
|---|---|
| D2 | One end of the 1 kΩ resistor. |
| Other resistor end | LED anode, usually the longer lead. |
| LED cathode | GND; the package’s flat side commonly marks the cathode. |
For a typical red LED with about 2 V forward drop, a 1 kΩ resistor on a 5 V output gives roughly (5 − 2) / 1000 = 0.003 A, or 3 mA. This is a visible, modest indicator current. Different LED colors have different voltage drops; use the actual LED and controller ratings when changing the design.
Upload and observe
Download the complete Arduino sketch
const byte ledPin = 2;
const unsigned long intervalMs = 500;
unsigned long changedAt = 0;
bool lit = false;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
const unsigned long now = millis();
if (now - changedAt >= intervalMs) {
changedAt = now;
lit = !lit;
digitalWrite(ledPin, lit ? HIGH : LOW);
}
}
The LED should stay on for half a second and off for half a second: one full cycle each second. The setup explicitly makes D2 an output. Without that configuration, writing HIGH can enable an input pull-up instead of driving the intended output.
Keep time without stopping the loop
millis() supplies an elapsed-time counter. Subtracting the previous timestamp tells us whether the interval has passed. Unsigned subtraction also handles the counter wrapping around, provided the interval is within the counter’s practical range and the loop continues running regularly.
Unlike a long delay(), this loop remains available for other short tasks between changes. That matters when a status light shares a controller with buttons, sensors, or networking.
Troubleshooting
- Always dark: check polarity, the resistor value, and the breadboard rows. Reversing an LED is a common first mistake.
- Always on: check that the resistor is connected to D2, not the 5 V rail.
- Very dim: inspect the resistor markings and verify that the sketch configured the correct output pin.
- Several LEDs: give each LED its own resistor. Respect both per-pin and total board current limits; a strip or high-power lamp needs a separate driver.
Next experiment
Make the interval a variable and change it when a button is pressed. For brightness control, use PWM on a supported pin. The official Arduino Blink example is another reference for basic wiring and output setup.