Utility Bar

Arduino LED: How to Blink an LED with Code

Ethan Zaitchik |

Most Arduino boards, including the Uno, have a built-in LED wired to pin 13, and if you followed our Arduino IDE setup guide, you've already seen it blink. This time we're wiring up an external LED instead. It's still the simplest circuit you can build, but it introduces the two components you'll use in almost every future project: a resistor and a breadboard. By the end of this guide you'll understand LED polarity, why the resistor is there, and how to wire and code a basic digital output.

Table of Contents

    What You'll Need

    Want everything for this project in one box?

    Our Arduino Super Starter Kit includes the board, LEDs, resistors, breadboard and jumper wires used in this guide, so you can just open the box and start building.

    Shop the Arduino Starter Kit →

    Understanding LED Polarity

    An LED only allows current to flow in one direction, so it matters which way round you wire it. The two legs are different lengths: the longer leg is the anode (positive) and the shorter leg is the cathode (negative). If your LED's legs have been trimmed to the same length, check the body instead, there's usually a flat edge on the cathode side.

    Wiring an LED backwards won't damage it or your Arduino, it just won't light up. If your circuit doesn't work later, this is the first thing to check.

    LED polarity diagram showing anode and cathode legs

    Why You Need a Resistor

    An LED has very little internal resistance, so connecting it directly to a digital pin would let too much current flow through, burning out the LED almost instantly. A 220Ω resistor placed in series limits the current to a safe level, typically somewhere around 15-20mA for a standard 5mm LED at 5V. It doesn't matter whether the resistor sits before or after the LED in the circuit, as long as it's somewhere in that current path.

    Wiring the Circuit

    With the Arduino unplugged from USB, build the circuit as follows:

    1. Place the LED across the centre gap of the breadboard, so the anode (long leg) and cathode (short leg) sit in separate rows.
    2. Insert the resistor so one end shares a row with the LED's anode leg, and the other end sits in an empty row.
    3. Run a jumper wire from that empty row to digital pin 5 on the Arduino.
    4. Run a second jumper wire from the LED's cathode row to a GND pin on the Arduino.

    Double check the resistor and LED aren't bridging the centre gutter incorrectly, and that nothing else is touching before you plug the board in.

    Breadboard wiring diagram for Arduino external LED blink circuit

    Upload the Blink Code

    Connect your Arduino to your computer with the USB cable, open the Arduino IDE, and make sure the correct board and port are selected under Tools (see the IDE setup guide if you need a refresher). Paste in the sketch below and click Upload.

    Arduino IDE Upload Button
    /*
     * Ethan Zaitchik
     * Arduino External LED Blink Example
     * Full guide: https://zaitronics.com.au/blogs/arduino/led-blink-on-arduino
    */
    
    void setup() {
      pinMode(5, OUTPUT);
    }
    
    void loop() {
      digitalWrite(5, HIGH);
      delay(500);
      digitalWrite(5, LOW);
      delay(500);
    }

    Code Explanation

    void setup() {
      pinMode(5, OUTPUT);
    }
    • setup() runs once when the board powers on. pinMode(5, OUTPUT) tells the Arduino that pin 5 will be used to send a signal out, rather than read one in.
    void loop() {
      digitalWrite(5, HIGH);
      delay(500);
      digitalWrite(5, LOW);
      delay(500);
    }
    • loop() runs continuously after setup finishes. digitalWrite(5, HIGH) sends 5V to pin 5, lighting the LED. delay(500) pauses execution for 500 milliseconds. digitalWrite(5, LOW) then cuts the voltage, turning the LED off, before the second delay pauses again and the loop repeats.

    If you want to change the blink speed, adjust the numbers inside delay(). Lower numbers blink faster, higher numbers blink slower.

    Expected Outcome

    Once uploaded, your external LED should turn on and off every half second, exactly like the onboard LED did in the IDE setup guide, just now on a component you wired yourself.

    Troubleshooting

    LED doesn't light up at all

    Check the LED is the right way round, the longer leg (anode) should be on the resistor side. Reversed LEDs simply won't conduct.

    LED is very dim or flickers

    Confirm the resistor is actually 220Ω and sitting in the current path, and check every jumper wire is fully seated in the breadboard.

    Upload succeeds but nothing happens

    Make sure the pin number in your code (5) matches the pin your jumper wire is actually connected to on the board.

    LED worked once, now doesn't

    Loose breadboard connections are the most common cause. Reseat the LED, resistor, and both jumper wires.

    Frequently Asked Questions

    Do I need a resistor for the LED?

    Yes. Without one, the LED draws too much current and will burn out, and it can also damage the Arduino's output pin.

    Can I use a different resistor value?

    Anything from around 220Ω to 1kΩ will work safely for a standard 5mm LED at 5V. Lower values make the LED brighter, higher values make it dimmer.

    Which pin should I use?

    Any digital pin works for this example. We used pin 5, but pins 2 through 13 are all suitable (avoid 0 and 1, which are used for serial communication).

    Can I use a different colour LED?

    Yes, colour doesn't affect the code. Different colours have slightly different forward voltages, but a 220Ω resistor is a safe value across all common 5mm LEDs.

    What's the difference between this and the built-in LED example?

    The built-in LED example in our IDE setup guide uses the LED already soldered to the board on pin 13, so there's nothing to wire. This guide uses an external LED so you can practice the resistor and breadboard skills you'll need for almost every other Arduino project.

    Resources

    Leave a comment