Blink an LEDš
Beginner
This article is in the Microcontrollers topic. It puts Digital Pins into practice and uses arduino-cli to load the code. If you've never wired a breadboard, read Breadboards first, and if you've never seen an Arduino before, What Is an Arduino? covers the board itself.
Blinking an LED is the first thing nearly everyone builds on a microcontroller, and for a practical reason: one blinking light proves the entire chain works. The circuit is wired correctly, the board is talking to the computer, and the code you compiled is running on the chip. It's also the first time code you wrote makes something happen in the physical world.
It happens in two stages. First you blink a light that's already on the board, with no wiring at all, to confirm the toolchain works. Then you wire your own LED on a breadboard and blink that.
What You'll Needš
Everything here comes in any starter kit:
- An Arduino Uno (or compatible board) and its USB cable
- A breadboard
- One LED (any colour)
- One 220 Ī© resistor: its bands read red-red-brown-gold (Resistor Color Codes explains how to read them)
- Two jumper wires
You'll also need arduino-cli installed with the AVR core added, and your board's port and FQBN (fully qualified board name) to hand. arduino-cli covers both terms and the one-time setup. This article assumes you can already compile and upload.
Stage 1: Blink Without Wiring Anythingš
Every Arduino Uno has a small LED already wired to pin 13, labelled L on the board. The code can reach it by the name LED_BUILTIN. Blinking it needs no breadboard and no resistor, which makes it the perfect first test: if it blinks, the board and toolchain work, and any later problem is in the wiring.
Create a sketch folder called BlinkTest with a file BlinkTest.ino:
| BlinkTest.ino ā blink the built-in LED | |
|---|---|
If pinMode, digitalWrite, and the HIGH/LOW states are unfamiliar, Digital Pins explains exactly what they do. The one new piece is delay(1000), which pauses the program for 1000 milliseconds (one second) so each state lasts long enough to see. Without the pauses the LED would switch far too fast to notice.
From inside the BlinkTest folder, compile and upload:
| Compile and upload from the sketch folder | |
|---|---|
Within a few seconds the L LED on the board should start blinking: one second on, one second off. That's the whole toolchain proven. If it doesn't, see Troubleshooting and fix it here, where there's no wiring to blame.
Stage 2: Blink Your Own LEDš
Now make it real with an LED you place yourself. The circuit is one output pin, a current-limiting resistor, and an LED to ground:
Wire it on the breadboard:
- Put the LED across the centre gap. The longer leg is the anode (positive side) and goes toward the pin; the shorter leg (cathode) goes toward ground. If the legs have been trimmed to the same length, look at the rim around the base of the LED: it has a flat edge on the cathode side.
- Connect the 220 Ī© resistor from the LED's anode row to a separate row. It could go on either side of the LED: in a single loop the same current flows everywhere, so the resistor limits it wherever it sits.
- Run a jumper from the Arduino's pin 3 to the resistor.
- Run a jumper from the LED's cathode to a GND pin on the Arduino.
An LED always needs its resistor
Never wire the LED straight from the pin to ground with no resistor. Without it the LED draws far too much current: it can burn out in an instant and damage the pin driving it. One 220 Ī© resistor in the loop keeps both safe; Diodes and LEDs works out where the value comes from.
The code is the same as before, except that it drives pin 3 instead of the built-in LED:
| Blink.ino ā blink the external LED on pin 3 | |
|---|---|
Upload it the same way, and your own LED blinks in time. You're now running a circuit you designed and built, on code you wrote and flashed yourself.
How to Know It's Workingš
Three things confirm it:
- The LED blinks evenly, one second lit and one second dark. That's
delay(1000)doing its job. - The brightness is clear and steady, neither painfully bright nor flickering. The 220 Ī© resistor is holding the current at about 14 mA.
- The on-board
LLED has stopped blinking. It's wired to pin 13, and the new sketch only drives pin 3, so a darkLconfirms the new code replaced the old.
Troubleshootingš
The LED doesn't light at all
Most often the LED is in backwards. An LED only conducts one way: the longer leg (anode) must face the pin, the shorter leg (cathode) must face ground. Pull it out, flip it, and try again. At 5V with its resistor in place, a backwards LED simply doesn't light; it isn't damaged.
The upload fails before the LED ever blinks
This is a toolchain problem, not a wiring one. Check the port (arduino-cli board list), and on Linux make sure you have permission to use the serial device. arduino-cli covers both, including the dialout group fix.
The LED flares brightly, then dies
The resistor is missing or bypassed. Too much current flowed through the LED and burned it out. Add a 220 Ī© resistor in the loop and replace the LED.
Nothing happens, but Stage 1 worked
Your wiring and your code disagree about which pin. The jumper must run from the same pin number your code drives ā pin 3 in the example. Confirm the jumper is in pin 3 and the code says 3.
Practiceš
1. Blink faster
You want the LED to blink twice as fast ā on and off every half-second. What do you change?
Solution
Change both delay(1000) calls to delay(500). The delay is in milliseconds, so 500 ms is half a second. The LED now spends half a second on and half a second off, blinking twice as fast.
2. A heartbeat
How would you make the LED flash a quick double-blink and then pause ā on, off, on, off, then a longer rest ā like a heartbeat?
Solution
Inside loop(), write two short on/off pairs followed by a long delay:
digitalWrite(3, HIGH); delay(150);
digitalWrite(3, LOW); delay(150);
digitalWrite(3, HIGH); delay(150);
digitalWrite(3, LOW); delay(800);
Because loop() repeats forever, that pattern plays over and over ā two quick blinks, then a rest.
3. Why pin 3 and not pin 13?
Stage 1 used the built-in LED on pin 13; Stage 2 used pin 3 for the external one. Could you have used pin 13 for the external LED too?
Solution
Yes. Pin 13 is an ordinary output pin that also happens to be wired to the on-board L LED. You can drive an external LED from it just like pin 3, and both LEDs would blink together. Pin 3 was used only to keep the external circuit separate and clear.
Quick Recapš
-
Prove the Toolchain First
Blink
LED_BUILTINwith no wiring. If the on-boardLLED blinks, your board andarduino-cliwork, so any later problem is in your circuit. -
The Output Circuit
Pin ā 220 Ī© resistor ā LED ā ground. The longer LED leg (anode) faces the pin. The resistor is never optional.
-
The Code
pinMode(3, OUTPUT)once, thendigitalWrite()HIGH and LOW inloop(), withdelay(1000)between them to make the change visible. -
Upload
arduino-cli compile --upload -p PORT --fqbn arduino:avr:uno .from inside the sketch folder flashes it to the board.
What's Nextš
You can drive an output and watch it run. The other half of a microcontroller is reading the world, and reading reliably needs one more idea: Pull-up and Pull-down Resistors adds a button to this circuit and has the board respond to it. With that, the full circuit from the top of Digital Pins, a button controlling three LEDs, is within reach.
Further Readingš
Official Documentation
- Arduino Language Reference ā Digital I/O ā
pinMode(),digitalWrite(), anddelay()in full
Related Articles
- What Is an Arduino? ā the board itself, and how to read a sketch's
setup()/loop()structure - Digital Pins ā what
pinModeanddigitalWriteactually do to a pin - Resistor Color Codes ā confirm the 220 Ī© resistor in this circuit by its bands
- arduino-cli ā installing the toolchain and uploading sketches from the terminal
- Breadboards ā how the rows and rails you wired this circuit into connect