Sub topic 9
Blinking an External LED on a Chosen Pin
This chapter assumes that you understand Arduino sketches, setup() and loop(), digital output pins, LED polarity, and basic voltage, current, and resistance. You also need an Arduino-compatible board, a USB cable, a breadboard, an LED, and a resistor.
The built-in LED is convenient, but an external LED lets you choose the output pin and build the circuit yourself. In this example, the LED connects to digital pin 8. The LED must connect to ground through a series resistor. A series component shares the same current path as the LED.
Wiring the external LED
An LED has two leads with different roles:
- The anode is the positive side.
- The cathode is the negative side.
- The longer lead is usually the anode.
- The shorter lead, or the flat edge on the LED body, usually marks the cathode.
Connect the circuit in this order:
- Arduino digital pin 8 to one end of the resistor.
- The other end of the resistor to the LED anode.
- The LED cathode to an Arduino ground pin.
The resistor can be placed before or after the LED in the same series path. Its position does not change the circuit's behavior. The circuit must include the resistor somewhere between pin 8 and ground.
A breadboard's connected holes may not be obvious at first. On many breadboards, each group of five holes in the center is connected internally. The two long power rails are often connected along their length, but some boards split each rail in the middle. Check the breadboard markings and keep the LED leads in separate connected groups.
The sketch
Store the pin number in a named constant. A constant is a value that the program does not change while it runs. This makes the chosen pin easy to find and change.
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);
}In setup(), pinMode() configures pin 8 as an output. An output pin is controlled by the board rather than used to measure an external signal.
In loop(), digitalWrite() sets the output level:
HIGHdrives the pin near the board's logic voltage.LOWdrives the pin near ground.
When the pin is HIGH, current flows through the resistor and LED to ground, so the LED emits light. When the pin is LOW, there is little voltage across the LED, so it turns off.
Each delay(500) pauses for 500 milliseconds, or half a second. The LED remains on for half a second and off for half a second. One complete on-and-off cycle takes about one second, so the blink frequency is about one hertz.
Upload and test
Before uploading, connect the board by USB. In the Arduino IDE, select the board model under the board-selection menu. Select the port that appears when the board is connected. Then verify or compile the sketch and upload it.
The selected board determines how the program is compiled and which upload process is used. The selected port identifies the connected board. Choosing the wrong board or port can prevent uploading even when the sketch is correct.
After uploading, the sketch starts automatically. If the LED does not blink, disconnect power before changing the wiring.
Troubleshooting the circuit
Check the electrical path from pin 8 to ground:
- Confirm that the resistor, LED, and ground are connected in one continuous path.
- Reverse the LED if the anode and cathode are swapped.
- Confirm that the jumper connected to the resistor really reaches pin 8.
- Check that the board's ground pin is used, not a disconnected breadboard rail.
- Verify that the IDE shows the intended board and port.
- Make sure the sketch uses the same pin number as the wire.
If the LED stays on, the circuit may be connected to a different pin, or the LED may be wired across the supply instead of through the controlled pin. If the LED is very dim, inspect the resistor value and the breadboard connections.
Exercise
You move the wire from digital pin 8 to digital pin 7, but the LED remains controlled by the program only when the code is changed. What line must change?
Show answer
Change const uint8_t LED_PIN = 8; to const uint8_t LED_PIN = 7;. The pinMode() and digitalWrite() calls use the constant, so the rest of the sketch stays unchanged.