Arduino: blink an LED (the classic first project)
The minimum Arduino sketch. Blink the onboard LED on pin 13. If you have never programmed an Arduino, start here.
If you have an Arduino and you have not done anything with it yet, this is the project. It is also the project I do whenever I want to confirm a board works before I trust it with anything more interesting.
The blink sketch is short on purpose. You learn:
- How to write a sketch
- How the
setup()andloop()functions work - How to set a pin as an output
- How to use
delay()to pause
What you need
- Any Arduino board (Uno, Nano, Mega, MKR)
- USB cable
You do not need an external LED or resistor. Every Arduino has an LED soldered to pin 13 (on some boards it is a different pin, but it is always on).
Install the Arduino IDE
Download from https://www.arduino.cc/en/software. The regular IDE, not the Web Editor.
For the Uno and Nano, no additional board installation is needed. Plug in the USB cable, the IDE should see it.
For the Nano Every, Nano 33, MKR, or Portenta boards, you will need to install the corresponding board package. The IDE will offer to install it when you select the board.
The sketch
File >> Examples >> 01.Basics >> Blink
That opens the canonical version:
Arduino (Uno, Nano, Mega)
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
LED_BUILTIN is a constant that points to whatever pin the onboard LED is
on. For the Uno and Nano, that is pin 13. For other boards it might be
different, but LED_BUILTIN is always right.
ESP32 (Arduino)
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
Same code, same LED_BUILTIN. The ESP32’s onboard LED is on GPIO 2
(most dev boards), and the core defines LED_BUILTIN to point to it.
MicroPython (ESP32 or Pico)
from machine import Pin
import time
led = Pin(2, Pin.OUT) # ESP32: GPIO 2. Pico: Pin(25, Pin.OUT)
while True:
led.toggle()
time.sleep(0.5)
On the Pico, use Pin("LED", Pin.OUT) (the original Pico: GPIO 25;
the Pico W: the onboard LED on the Wi-Fi chip).
Raspberry Pi Python
The Raspberry Pi does not have an onboard blinkable LED in the same sense as a microcontroller (the activity LED on the Pi is wired to the SoC, not a GPIO pin you can drive). The closest equivalent is wiring an external LED to a GPIO pin:
from gpiozero import LED
import time
led = LED(17) # BCM pin 17 (physical pin 11)
while True:
led.toggle()
time.sleep(0.5)
pip3 install gpiozero. The Pi’s GPIO library is solid; this is the
simplest pattern. If you want a hardware-PWM-dimmed LED, that is
covered in the Pi LED dimming tutorial.
Upload it
- Select your board:
Tools>>Board>>Arduino Uno(or whatever you have). - Select the port:
Tools>>Port>>COM3(Windows) or/dev/cu.usbserial-1410(macOS). - Click Upload (or
Ctrl+U).
The IDE compiles, uploads, and the LED on the board starts blinking.
What you learned
setup()runs once when the board powers up or resets.loop()runs forever, as fast as it can.pinMode(pin, OUTPUT)configures a pin as an output.digitalWrite(pin, HIGH)sets a pin to 5V (or 3.3V on 3.3V boards).digitalWrite(pin, LOW)sets it to 0V.delay(1000)pauses for 1000 milliseconds.
Adding an external LED
The onboard LED is pin 13, but you can use any digital pin. Try pin 9 with an external LED and a 220 ohm resistor in series:
Arduino pin 9 --[ 220R ]-- LED anode (+) -- LED cathode (-) -- GND
LEDs are polarized. The longer leg (anode) goes to the resistor, the shorter leg (cathode) goes to ground. If your LED has a flat spot on the plastic, that is the cathode side.
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
}
Making it fade instead of blink
Pin 9 supports PWM (Pulse Width Modulation), which lets you dim LEDs:
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(9, brightness);
delay(5);
}
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(9, brightness);
delay(5);
}
}
analogWrite() is not actually analog. It is PWM: a fast on/off signal
where the duty cycle changes. For an LED, your eye sees it as dimming.
For a motor, it sees it as slower speed.
PWM pins on the Uno: 3, 5, 6, 9, 10, 11. The pin number has a tilde (~)
next to it on the board silkscreen.
When the upload fails
- Port not found. Drivers. For the Uno, the CH340 or CP2102 driver may need installing. On modern Windows 10/11 it usually works automatically.
avrdude: stk500_recv(): programmer is not responding. The board is not in the right port, or the bootloader is corrupted. Try a different USB cable (yes, really; this fixes it more often than I want to admit), then try holding the reset button while clicking upload.Sketch uses too much program storage space. You went over 32 KB. Pick a smaller sketch.
What to build next
- A button-controlled LED (combines with the button debounce tutorial).
- A traffic light sequence on three LEDs.
- A “morse code blinker” that flashes your name in SOS.
The morse code version is one of the next tutorials on this site. The traffic light version is in the book Arduino Basics.