Sub topic 9
Blinking an external LED on a chosen pin
This chapter joins the sketch and the circuit. You move the blink from the board's built-in LED to a separate LED that you wire yourself and drive from a pin you pick. It assumes you have already covered these topics:
setup()andloop()pinMode()anddigitalWrite()delay()- LED polarity (anode and cathode)
- Sizing a series resistor
- The basic layout of a breadboard
Examples assume a 5 V board such as an Arduino Uno. Where a 3.3 V board changes the numbers, the text says so.
What changes from the built-in blink
The built-in blink uses LED_BUILTIN. That constant names a pin that is already wired to an LED and a resistor on the board. With an external LED, you take over both jobs:
- You choose the pin number in software.
- You build the path from that pin, through the LED and a resistor, to ground in hardware.
The sketch barely changes. The circuit carries most of the new work. A blink that fails now has two possible causes, the code or the wiring, so build the two parts deliberately.
The circuit
The pin acts as a switchable 5 V source. When the sketch sets it HIGH, current flows from the pin through the resistor and the LED, then to GND. When the sketch sets it LOW, the pin sits at 0 V and the current stops.
| From | To | Note |
|---|---|---|
| Digital pin 8 | One leg of the 220 Ω resistor | Any row on the breadboard |
| Other leg of the resistor | LED anode (longer leg) | Same breadboard row, different column strip |
| LED cathode (shorter leg, flat edge) | GND rail or GND pin | Completes the path |
The resistor can sit on either side of the LED, because current is the same everywhere in a series path. Putting it between the pin and the anode is the common habit. A bare LED leg never touches the pin directly, even if you move wires around later.
The sketch
This sketch is the external-LED version of the blink:
const uint8_t LED_PIN = 8;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}Three details are worth understanding.
- The named constant.
LED_PINappears three times in the sketch. If you move the wire to pin 9, you edit one line.constalso makes the compiler reject accidental assignments to it. - The type.
uint8_tis an unsigned 8-bit integer, which holds 0 to 255. That range is more than enough for any pin number, and it takes one byte of RAM instead of the two bytes ofint. For a single constant the saving is trivial. The choice documents that the value is a small, non-negative identifier. - The timing. Each
delay(500)holds the LED in one state for 500 ms. The full cycle is 1000 ms, so the LED blinks at about 1 Hz. The real period is slightly longer than 1 s, becausedigitalWrite()and the loop jump take a few microseconds. You will not see the difference.
Why `pinMode()` matters
Pins start as inputs. If you skip pinMode(LED_PIN, OUTPUT) on an AVR-based board like the Uno, digitalWrite(LED_PIN, HIGH) does not drive the pin. It turns on the internal pull-up resistor, which is a weak connection to 5 V through roughly 20 kΩ to 50 kΩ. The LED then glows very dimly and never blinks properly. A faint, constant glow is a classic sign of a missing pinMode().
Choosing the pin
Pin 8 is a plain digital pin on the Uno, which is why this tutorial uses it. Many pins would work, but some have other duties. Check your board's pinout before choosing.
| Pin (Uno) | Suitable for a simple LED? | Reason |
|---|---|---|
| 0 and 1 | Avoid | Shared with the USB serial connection, so uploads and Serial Monitor can conflict |
| 2 and 3 | Works, but save them | The only external-interrupt pins, which you may want later for buttons or sensors |
| 4 to 12 | Yes | General-purpose digital pins; some also do PWM or SPI |
| 13 | Works | Also drives the built-in LED, so the two LEDs blink together |
| A0 to A5 | Yes | Usable as digital pins by number (14 to 19) or by name |
Other boards differ. A Mega, an ESP32 and an RP2040 board all have different restrictions. On an ESP32, for example, some pins are tied to the boot process, and the logic level is 3.3 V.
Current limits
An AVR pin is not a power supply. The Uno's datasheet allows 40 mA absolute maximum per pin, and 20 mA is the recommended working limit. A typical red LED with a 220 Ω resistor draws about 14 mA, which sits comfortably below that. If you size the resistor yourself, keep the current under about 20 mA. To drive several LEDs or a brighter load, use a transistor between the pin and the load instead of connecting it straight to the pin.
Building and verifying in order
Work in a sequence that isolates faults:
- Prove the board and upload first. Upload the built-in blink and confirm the on-board LED blinks. This shows the toolchain, board selection and port are correct.
- Wire with the board unplugged. Disconnect USB while you move wires. A resistor leg touching the wrong strip while powered can short a pin.
- Check polarity. The longer leg and the non-flat side are the anode. The anode faces the resistor and pin. The cathode goes to GND.
- Upload the external LED sketch. The new LED should blink, and the built-in LED should stay dark, unless you used pin 13.
- Compare with a known-good test. If nothing happens, change
LED_PINtoLED_BUILTINand re-upload. When the on-board LED blinks, the code is fine and the fault is in the wiring.
Wiring the LED the other way: active-low
You can also wire the LED between the 5 V supply and the pin, with the resistor in series. The anode goes to 5 V and the cathode goes toward the pin. Current then flows into the pin when it is LOW, so the LED lights when the pin is LOW. This is called active-low wiring. It is common on boards where the pin can sink more current than it can source.
Named constants stop the inverted logic from spreading through the code:
const uint8_t LED_PIN = 8;
const uint8_t LED_ON = LOW;
const uint8_t LED_OFF = HIGH;
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LED_OFF);
}
void loop() {
digitalWrite(LED_PIN, LED_ON);
delay(500);
digitalWrite(LED_PIN, LED_OFF);
delay(500);
}Writing LED_OFF in setup() sets a known starting state. Without that line, the pin starts LOW after pinMode(), which lights the LED for the first moments before loop() runs its first statement. Most people never notice, but in a real device this kind of glitch can matter.
Common errors in this step
| Symptom | Likely cause | Fix |
|---|---|---|
| LED never lights | LED reversed | Swap the two legs |
| LED never lights | Resistor and LED are in different breadboard rows with no connection | Move legs so they share a strip |
| LED never lights | Cathode not connected to GND | Add the return wire to a GND pin or the GND rail |
| LED glows faintly, does not blink | Missing `pinMode()` | Add `pinMode(LED_PIN, OUTPUT);` to `setup()` |
| Wrong LED blinks, or nothing | Wire on pin 7 or 9, code uses 8 | Match the wire to `LED_PIN` |
| Blinks twice as fast or slow as expected | `delay()` values differ from intent | Check each `delay()` argument, in milliseconds |
| Upload fails with the LED wired to pin 0 or 1 | Serial lines are in use | Move the LED to another pin, or unplug it during upload |
| Board resets or LED is very bright and hot | Resistor missing or too small | Add or increase the resistor, then check for damage |
What this approach does not give you
delay() stops the sketch for the whole wait. While the program sleeps, it cannot read a button, update a display or respond to serial data. For a single LED that is fine. The next chapters cover diagnosing failures and replacing delay() with timing based on millis() when other work must continue.
Exercise
You move the LED from pin 8 to pin 10 by changing the wire. The LED stays dark, and the sketch still reads const uint8_t LED_PIN = 8;. What do you change, and why must the change happen in the code and not only in the wiring?
Show answer
Change the constant to const uint8_t LED_PIN = 10; and upload again. The sketch drives only the pin number it names. The board does not detect which pin the LED is wired to, so the wire and the constant must match.
Exercise
A 5 V board drives a green LED with a 2.1 V forward voltage through a 330 Ω resistor. What current flows, and is it within the 20 mA recommended per-pin limit?
Show answer
The resistor drops 5 V - 2.1 V = 2.9 V. Ohm's law gives I = 2.9 V / 330 Ω ≈ 8.8 mA. That is well below 20 mA, so the pin is not overloaded. The LED will be dimmer than with a 220 Ω resistor.
Exercise
You wire an LED active-low, with the anode at 5 V and the cathode connected through a resistor to pin 8. In the sketch, which value of digitalWrite(8, ...) lights the LED, and which constant names make the sketch easier to read?
Show answer
LOW lights the LED, because current flows into the pin only when the pin sits at 0 V. Define const uint8_t LED_ON = LOW; and const uint8_t LED_OFF = HIGH;, then call digitalWrite(LED_PIN, LED_ON). The inverted logic then lives in one place.