How to Connect & Blink an LED with Arduino

In the last lesson we uploaded our first Arduino sketch, the Blink Example included in the Arduino IDE, and watched the onboard LED of the Arduino Uno board blink on and off. Now that we’re familiar with the different parts of a sketch by looking at a pre-written example, it’s time to learn about Arduino functions, what they do and how to modify them to suit our needs! And why not hook up our very first component while we’re at it – an external LED!

Don’t let its small size fool you. Programming an external LED with an Arduino is an important launch pad into working with other larger components. That’s because the programming technique used to control an LED is the same you’ll use for a lot of other modules including motors, relays, buzzers, and many sensors. You’ll also find that adding an external LED to your project is a great way to see what’s happening out the output pins. If the LED lights up, then you know the pin it’s connected to is getting power.

In this Arduino tutorial we’ll learn how to blink an external LED and control the rate of the blink while using our first Arduino functions!

Here’s what you’ll need to build along with this Arduino tutorial:

How LEDs Work

Diodes and LEDs share the same fundamental characteristic – they only allow electrical current to flow in one direction. This makes them a great choice for regulating current within circuits. An LED, or Light Emitting Diode, takes this concept further – when an electric current passes through, it will also emit light!

To ensure your LED works correctly, it’s important to determine the polarity because if you hook it up backwards it won’t work. The longer lead is the anode and needs to be connected to a positive voltage while the shorter lead is the cathode, which goes to ground. Another clue to look out for is a flattened spot around the rim of the plastic housing. The flat spot always aligns with the cathode (negative or ground side).

Anode and cathode leads of an LED

Using Resistors with LEDs

An LED’s brightness is determined by the amount of current it receives. Too much current can burn out an LED quickly so a resistor must be used in series with the power source. The value of that resistor affects how bright or dim your light will appear – higher value resistors reduce current flow making your LED dimmer while lower values allow more current to pass so your LED glows brighter without endangering its longevity. Resistors can be connected to either the anode or cathode and can usually be anything from 200 Ohms up to about 1K Ohms. The most popular values are 220, 330 and 470 Ohms.

How to Connect an LED to the Arduino Uno

Connecting an LED to an Arduino Uno is not as complicated as it seems! It’s always a good idea to unplug the Arduino from your computer or power source when you make connections. To begin the process, insert each lead of the LED into a different column in your breadboard and keep track of which lead is the anode and which is the cathode. I often have to pull mine out to remind myself! If you are unsure of how to use a breadboard, check out our How to Use a Breadboard Tutorial.

Here’s a circuit diagram you can use in the meantime:

How to connect an LED to the Arduino Uno with a resistor and jumper wires.

With the LED in place, let’s add the resistor next – 330 Ohms is a popular choice for LEDs. For this example I’ve added it to the anode (positive long lead). Insert one leg of the resistor in the same column as the anode and the other leg into another column of your choice, making sure you don’t use the column with the cathode (negative short lead). From this end of the resistor, use a jumper wire to connect to pin 8 of the Arduino Uno. Finally, take another jumper wire and connect the cathode of the LED to a ground pin (GND) of the Arduino Uno. Any time you connect to ground, it’s common to use a black or brown wire so when your projects get bigger with more components, it makes it easier to identify where all the grounds are.

Here’s how my LED circuit looks like with the Arduino Uno:

Circuit showing one LED connected to the Arduino Uno with a 330 Ohm resistor.

Where getting closer to seeing our LED come alive in just a few more steps!

How to Blink an LED with Arduino

The best way to get started writing your first programs is to modify existing code and see what happens when you upload it. Let’s use the Blink Example included with the Arduino IDE from the previous Arduino tutorial.

Scroll past the comments at the top to find the setup function:

Setup function in an Arduino sketch.

There’s only one line of code in the setup function in this example:

pinMode(LED_BUILTIN, OUTPUT);

The first word in this function, pinMode, configures a pin to behave either as an input or an output. It takes 2 arguments within the parenthesis – the pin you’re working with and its mode (INPUT or OUTPUT). In our case we’re working with pin 8 and we want the pin to act as an OUTPUT because it will be outputting a voltage to our LED.

Update the pinMode function to reflect our scenario like this:

pinMode(8, OUTPUT);

Double check your capitalization – it matters. If you type in “output” (lowercase) you’ll notice that the green text color goes away. The same is true with function names like pinMode.

Next, find the loop function:

Loop function of an Arduino sketch.

The first line of code within the loop function is a digitalWrite:

digitalWrite(LED_BUILTIN, HIGH);

The digitalWrite function sets the voltage state of a pin and also takes 2 arguments within the parenthesis – the pin we’re working with and what state you want it to be in (HIGH or LOW). Setting the argument to HIGH (as it currently appears in the code) tells the Arduino to send 5V to the pin and therefore whatever is connected to it. In our case it’s our LED and it would cause it to turn on. Setting the argument to LOW causes the Arduino to send out 0V, effectively turning our LED off.

Our objective is to blink our LED on and off so let’s start it at the ON state, which is HIGH in Arduino language. In this case all we have to do is update the pin we’re working with so that line would look like this:

digitalWrite(8, HIGH);

The next line of code is the delay function:

delay(1000);

At this point we’ve told the Arduino to turn our LED on. But how long do we want it to stay lit before turning off? That’s where the delay function comes in. It pauses the program for the amount of time in milliseconds you specify in the parenthesis. In this case, the pause is for 1000 milliseconds, which is equal to 1 second. Let’s leave this line alone for now.

Next we have to turn the LED off so it’s no surprise the next line of code is another digitalWrite:

digitalWrite(LED_BUILTIN, LOW);

Since we want the voltage state to be LOW, all we have to change here is the pin:

digitalWrite(8, LOW);

Now that the LED is turned off, the next line has another delay function that specifies how long the program should pause with the LED in the LOW state:

delay(1000);

Let’s leave it at 1000 milliseconds, or 1 seconds so we get an even blink. After making our modifications your code should look like this:

Plug in your Arduino and upload the code! Your LED should start blinking at a rate of about once per second. If nothing is happening, double check your connections and that your board and COM port are correctly set in the Arduino IDE. Also look for any error message(s) that pop up during the upload process. The most common errors are incorrect syntax, capitalization and forgetting the semicolon (;) at the end of a function.

Here’s my LED in mid-blink:

Blinking an LED with Arduino

Practice Changing the LED Blink Rate

When you experiment with code examples from the IDE or projects you find online, you’ll often have to update the pins to suit your project like we did in the Blink Example.

Next, try changing the delay to 500 (half a second). The LED should blink faster. See how fast you can get it to blink and still be able to see the different states. At some point it will become a blur because our eyes can’t process the information as fast as the Arduino!

The delays for the HIGH and LOW states don’t have to be the same. You can use your LED for morse code or to create light patterns by setting different times for the HIGH and LOW states. For instance, setting the LED on (HIGH) for 100 milliseconds and then off (LOW) for 900 milliseconds makes for a useful once-per-second timer.

Now that you know how to connect an LED to your Arduino Uno and write code to control it, you’re on your way to building amazing projects! The same functions you learned in this Arduino tutorial are used to control a variety of other components. So go ahead – get creative and have fun building something awesome with LEDs!

Learn Arduino for Beginners

BEGINNER’S CRASH COURSE TO THE ARDUINO


Go from absolute beginner to building and coding your own projects from scratch using the most popular Arduino components, even if you’ve never coded or built a circuit before.

Make real progress on your creative electronics goals by building and coding 30+ Arduino projects in real-time with me. Access our entire library of past course broadcasts plus a new LIVE course every month!

Some links in this post are to affiliate sites. If you purchase something through them, I may earn a small commission — which costs you nothing! I am very grateful when you use my links to make a purchase.

Stop Scrolling. Start Creating.

Find out about new courses and live events!
Get my latest guides and reference materials.
Unsubscribe anytime!