LED guides · On this page

Blink an LED with an Arduino

Blink an LED with an Arduino: on for a second, off for a second. It's Arduino's own first example, and here it runs in your browser before you build it. An LED lights only one way round, and it always needs a resistor in series to limit its current.

Left to right: a 5 mm red LED with a diffused red lens and two leads, the right one longer; a smaller 3 mm red LED, also with two leads, the right one longer; and a 5 mm RGB LED with a clear lens and four leads, the second from the left the longest.
5 mm red LEDTwo leads, the long one +3 mm LEDSmaller lens, same two leads5 mm RGB LEDFour leads, the longest shared

Try it first, no parts needed

This is the whole project, wired and running: an LED on pin 9 through a 220 Ω resistor. Press Run simulation. The LED turns on for a second and off for a second, and the Serial Monitor prints LED on and LED off.

Ready
Open in Cirkit Designer

The LED, blinking

Its glow, enlarged.

Lights up when the simulation runs.

Arduino Uno serial monitor115200 baud

Output appears here when the simulation runs.

What the code does

The sketch switches pin 9 on, waits a second, switches it off, waits a second, and repeats.

blink_led.ino
  1. const int LED_PIN = 9;  // a PWM pin: marked ~ on the Uno

    The LED is on pin 9. Any digital pin can blink it; pin 9 can also dim it, as in dim it with a knob.

  2. pinMode(LED_PIN, OUTPUT);

    Makes pin 9 an output, so the sketch can switch it between 0 V and 5 V.

  3. digitalWrite(LED_PIN, HIGH);  // on

    Pin 9 to 5 V: current flows through the resistor and the LED, and it lights. delay(1000) then waits a second.

  4. Serial.println("LED on");

    Says so on the Serial Monitor, so you can follow it without watching the LED.

  5. digitalWrite(LED_PIN, LOW);   // off

    Pin 9 to 0 V: no current, so the LED goes dark for another second, and loop() starts again.

Build it on an Arduino

Now with real parts: check which leg is which, wire it, get the code, then upload it and watch it blink. Using an ESP32 board? It has its own guide →

Which leg is which: the LED pinout

An LED has two leads. The longer one is the anode (+): it goes to the Arduino pin, through the resistor. The shorter one is the cathode (−): it goes to GND. Wired the other way round, the LED stays dark. If the legs have been trimmed, look at the rim at the bottom of the lens: it is flat on the cathode's side.

LED pinout: two leads, 2.54 mm apart. The anode (+) is the long leg: it goes through a resistor, 220 ohms on an Uno, to the pin that drives the LED. The cathode (−) is the short leg, on the side where the rim is cut flat: it goes to GND. Backwards, the LED stays dark.
LeadsTwo: the anode (+, the long leg) and the cathode (−, the short leg)
Lights whenCurrent flows from anode to cathode
Forward voltage1.9 V typical, 2.3 V max, at 10 mA (Kingbright WP7113ID, a 5 mm red LED)
Current30 mA max for the WP7113ID; 220 Ω from a 5 V pin gives about 14 mA
BrightnessanalogWrite(pin, 0 to 255) on a PWM pin, marked ~ on the Uno
LibraryNone: digitalWrite() and analogWrite() are built in

You'll need

  • An Arduino Uno (a Nano works the same way) and its USB cable
  • A red LED, 5 mm or 3 mm
  • A 220 Ω resistor (red, red, brown bands)
  • A breadboard and two jumper wires
  • No LED to hand? Blink works on the Uno's built-in LED too: use LED_BUILTIN, pin 13, instead of pin 9
  • Software to upload the sketch to the Uno: either Cirkit Designer, free in your browser, or the Arduino IDE, a free download for Windows, macOS and Linux
  1. Wire it

    The LED and its resistor in a row from pin 9 to GND: two wires.

    Tap a connection to see it on the circuit.

    PinConnect to the UnoShow
    LED anode (long leg)220 Ω resistor, then pin 9Show
    LED cathode (short leg)GNDShow

    The resistor can go on either side of the LED, as long as the two are in series between pin 9 and GND. From a 5 V pin, anything from 220 Ω to 1 kΩ works: the bigger the resistor, the dimmer the LED.

  2. Get the code

    I'm using:

    Steps 2 and 3 follow the tool you pick.

    Open in Cirkit Designer

    The project opens with the sketch already in it, so there's nothing to install.

    No Arduino IDE yet? Download it free for Windows, macOS or Linux.

    The sketch needs no library. Copy it into a new sketch:

    How it works line by line is in what the code does, above.

  3. Upload and check it

    Plug in your Uno. In Cirkit, open the Build tab, then Upload firmware: click Select Port and pick your Uno, then Upload to Device. The first time, Cirkit asks you to install Cirkit Create Agent, a small desktop helper that lets it reach the board over USB. Then open Upload output and serial monitor and click Start Serial Monitor.

    Show me where to click in Cirkit
    Blink on an Arduino UnoFileEditExportHelp
    DesignCodeSimulateBuild
    1Parts & purchasing2Assemble3Upload firmware
    Arduino UnoEdit Code

    Board

    Arduino Uno Change

    2

    Port

    Select PortCOM3 Connected

    3

    Upload

    Upload to DeviceUploading...Upload successful

    4

    Check that it works

    Your project should now run as designed.

    Upload output and serial monitor
    Compile / Upload Output
    Sketch uses 1992 bytes (6%) of program storage space. Maximum is 32256 bytes.
    Global variables use 202 bytes (9%) of dynamic memory, leaving 1846 bytes for local variables. Maximum is 2048 bytes.
    Device SerialStop

    Read serial data from your device.

    Start Serial Monitor
    Listening on COM3 at 115200 baud
    LED on
    LED off
    LED on
    LED off
    Select port

    Select a device port

    Select the device port for uploading code and serial communications

    COM3 (Arduino Uno)
    1. Open Build
    2. Choose Upload firmware
    3. Click Select Port
    4. Pick COM3 (Arduino Uno)
    5. Click Upload to Device
    6. Open Upload output and serial monitor
    7. Click Start Serial Monitor

    In the Arduino IDE, choose Arduino Uno and its port from the board menu, press Upload, then open the Serial Monitor and set it to 115200 baud.

    Show me where to click in the Arduino IDE
    Select BoardArduino Uno

    Select other board and port

    Arduino UnoCOM3
    blink_led.ino
    // Blink: the LED on pin 9 on for a second, off for a
    // second, and the Serial Monitor says which.
    const int LED_PIN = 9;  // a PWM pin: marked ~ on the Uno
    
    void setup() {
      Serial.begin(115200);
    OutputSerial Monitor115200 baud

    Compiling sketch...

    Sketch uses 1992 bytes (6%) of program storage space. Maximum is 32256 bytes.

    Global variables use 202 bytes (9%) of dynamic memory, leaving 1846 bytes for local variables. Maximum is 2048 bytes.

    Uploading...

    LED on

    LED off

    LED on

    LED off

    Done uploading.

    1. Click the board menu
    2. Pick Arduino Uno on COM3
    3. Click Upload
    4. Wait for Done uploading
    5. Click Serial Monitor
    6. Check it is set to 115200 baud

    It works when:

    • The LED blinks, on for a second and off for a second.
    • The Serial Monitor prints LED on and LED off in step with the LED.
    The LED twice: lit, with pin 9 HIGH and the Serial Monitor's line LED on; then dark, with pin 9 LOW and the line LED off.
    Left: pin 9 HIGH and the LED lit. Right: a second later, pin 9 LOW and the LED dark.

    Not working? See troubleshooting.

    Next guideNext: dim it with a knobThe same LED, plus a knob on A0: analogWrite() and PWM set its brightness.

Troubleshooting

Find your symptom; the checks under it run from the most common cause down.

The LED doesn't light, but the Serial Monitor prints LED on and LED off

The sketch runs, but no current flows through the LED. Check, in order:

  • The LED is the right way round. The long leg goes toward pin 9, the short leg, on the flat side of the rim, to GND. Backwards, it stays dark; turn it round.
  • The circuit is complete. Pin 9, the resistor, the LED and GND form one loop: on a breadboard, the resistor and the LED share a row, and nothing else sits in that row.
  • The wire is on pin 9. It must be the pin the sketch names.

The LED lights, but only faintly

  • The pin is set as an output. With digitalWrite() and no pinMode(LED_PIN, OUTPUT), HIGH only turns on the pin's internal pull-up resistor, which lights an LED faintly.
  • The resistor is 220 Ω to 1 kΩ. Bands red, red, brown are 220 Ω; brown, black, red are 1 kΩ. 10 kΩ (brown, black, orange) lets through only about 0.3 mA, too little to see well.

Nothing in the Serial Monitor, or garbled characters

  • The Serial Monitor is set to 115200 baud. It must match Serial.begin(115200) in the sketch.
  • The sketch prints a line a second. If nothing comes, check the board and port chosen for the upload.

How an LED works

An LED is a diode that gives out light: current flows one way only, from anode to cathode, and while it flows, the LED lights. A red LED needs about 2 V across it to light. Past that, its current climbs steeply, and without a resistor it would draw as much as the pin can give. The resistor limits the current. It takes the rest of the 5 V, and its value sets the current.

The red LED's current against the voltage across it, from the Kingbright WP7113ID's datasheet at 25 °C: almost no current below 1.5 V, then it climbs steeply, 10 mA at 1.9 V and 20 mA at 2.0 V. A dashed line shows what the 220 ohm resistor lets through from 5 V, (5 − V) ÷ 220; it crosses the curve at about 1.95 V and 14 mA, where the LED settles.
Where the 5 volts go: from pin 9 at 5 V, about 3 V across the 220 ohm resistor and about 2 V across the red LED, down to GND at 0 V. The resistor's 3 V over 220 ohms sets the current: about 14 mA, under the LED's 30 mA limit.

To pick a resistor for another LED or board: subtract the LED's forward voltage from the pin's voltage and divide by the current you want. For 10 mA from a 5 V pin: (5 − 1.9) ÷ 0.010 = 310 Ω, so 330 Ω.

Current, brightness and other limits

These are the limits that decide what you can wire, for a common 5 mm red LED on an Uno.

The Kingbright WP7113ID, a 5 mm red LED, at 25 °C, on an Uno
Current30 mA at most for the LED, continuously. 220 Ω from 5 V gives about 14 mA.
Uno pinRated at 20 mA. 40 mA is its absolute maximum, past which the chip can be damaged.
Uno, all pins200 mA at most through the chip's supply pins, and no more than 150 mA sourced from each of its two groups of pins.
Forward voltage1.9 V typical, 2.3 V at most, at 10 mA. Other colours can need more: the green and blue of an RGB LED such as the Fedy FD-5WSRGB-A, 3.2 V typical.
Reverse voltage5 V at most: see the pinout.
LightAbout 50 mcd at 10 mA, in a narrow 30° beam: brightest seen head on.

Other LEDs have their own limits, so check your LED's datasheet. Keep each at about 9 to 14 mA, which 330 Ω to 220 Ω gives from 5 V, and add up the LEDs that are on at once: ten at 14 mA is 140 mA, close to the 150 mA one group of pins can supply. For more LEDs, or brighter ones, let a transistor switch them from the 5 V supply, so the pin only drives the transistor.

Which LED, or something else?

An LED and a resistor is the simplest light; for colour, a strip or more current, pick one of these.

WiringCan dimChoose it for
The Uno's built-in LEDNone: on pin 13, LED_BUILTINNo: pin 13 has no PWMChecking a board and an upload work
One LED and a resistorTwo parts, two wires: this guideYes, on a PWM pin: dim it with a knobIndicators, status lights, your first circuit
RGB LEDFour leads, three resistors, three PWM pinsEach colour, to mix any colourA status light in any colour: the RGB guide
Addressable LEDs (WS2812B, NeoPixel)One data pin for a whole chain, and a 5 V supplyEach LED, any colour, through a libraryStrips and rings: many colours from one pin
A transistor and a brighter LED or stripThe pin drives the transistor; the supply drives the LEDsYes, with PWM on the transistorMore current than a pin's 20 mA

For one light, or a few, an LED and a resistor on a pin is all you need.

LED FAQ

Which leg of an LED is positive?

The longer leg, the anode, is positive: it goes toward the Arduino pin, through the resistor. The shorter leg, the cathode, goes to GND. With the legs cut, look at the rim at the bottom of the lens: it is flat on the cathode's side. See the pinout.

Can I connect an LED without a resistor?

No: nothing would limit the current, and the LED or the pin can be damaged. Always put a resistor in series; 220 Ω is safe for a red LED on a 5 V Uno.

What resistor does an LED need?

(Supply voltage − LED forward voltage) ÷ current. For a red LED at about 14 mA from 5 V, 220 Ω; for 10 mA, 330 Ω. Anything up to 1 kΩ still lights a red LED visibly on a 5 V board, just dimmer. See how an LED works.

How do I change an LED's brightness?

Put it on a PWM pin (3, 5, 6, 9, 10 or 11 on an Uno) and use analogWrite(pin, 0 to 255) instead of digitalWrite(). Dim it with a knob runs it live.

How many LEDs can an Arduino drive?

About ten at 14 mA each, one per pin with its own resistor. The chip can supply 150 mA from each of its two groups of pins and 200 mA in all, and that 200 mA also carries the chip's own current. A pin is rated at 20 mA, so one LED at about 14 mA is within it. For more, use a transistor or addressable LEDs; see the limits.

Simulating the LED in Cirkit Designer

Cirkit's red LED lights when its anode is at least 2 V above its cathode, so it stays dark backwards and lights from a 3.3 V or 5 V pin. Its brightness updates every 20 ms: a blink shows as on and off, and a PWM signal as the share of each pulse it's lit, a steady partial brightness, as on a real LED. It is tested on the Arduino Uno, the ESP32-S3 and the classic ESP32.

Where it differs from a real LED: Cirkit works with voltages, not current, so the resistor's value doesn't change the brightness, and an LED with no resistor still lights instead of overheating. On real hardware, always fit the resistor.

References