Arduino LED Blinking – Complete Tutorial

Got an Arduino in hand? The very first thing you can do is blink an LED with it. LED blinking is a very basic way to get started with Arduino.

Though Arduino LED blinking is very basic, it can be used in later stages to create various types of interesting projects like traffic light signals, LED chasers, VU meters, home decoration, etc. Projects with LEDs can be as simple as LED blinking or as challenging as sound/music-reactive lights or an 8x8x8 LED cube.

In this tutorial, we will learn how to blink an LED with Arduino. We’ll cover three different examples:

  • Arduino onboard LED blinking.
  • Blinking an External LED
  • Controll multiple LEDs with Arduino

By the end of this post, you will have a clear understanding of how LEDs work with Arduino, and you’ll be ready to create your own Arduino LED projects.

You will need :

For this tutorial you will need the following things:

  1. LED 
  2. Cables
  3. 330 Ω Resistor
  4. Arduino board (it can be of any kind, but I used the UNO board).
  5. Breadboard/Circuit-board Holder.
  6. USB cable
  7. Computer with Arduino software installed (you can download the Arduino IDE from here..)

Before starting, you should know the basics of an Arduino and an LED.

What is Arduino?

Arduino is a microcontroller development board. It has a microcontroller as its heart and some digital and analog Input Output pins. We can give some information/data in the form of voltage to Arduino via digital or analog input pins. And in the output pin, Arduino generates some control signal.

Ads

Arduino also has a nice and user-friendly GUI for programming the Arduino board. We can control the output signal according to the input signal by programming the Arduino board using the Arduino GUI.

We can download the Arduino GUI from Arduino’s official website and learn more about Arduino from there.

LED (Light Emitting Diode)

A light-emitting diode (LED) is a semiconductor light source. It emits light when current flows through it.

LEDs have two legs, one leg is shorter than the other. The shorter leg is the cathode(-ve), and the longer leg is the anode(+ve). You can also identify the cathode by spotting the flat side of the LED.

LEDs are diodes, so current flows in one direction only. The LED will not glow if you don’t connect it in the proper direction. In the following section, we will learn how to connect an LED with the arduino properly.

Arduino onboard LED Blinking

Almost every classic Arduino board (Uno, Leonardo, Mega, Nano, Micro, etc.) has a small, surface-mount LED connected to one of its digital pins. We can control that LED using a simple Arduino sketch.

In this section, we will learn how to control that LED. For this example, you only need an Arduino board and a PC or Mac.

Ads

Here, I am using an Arduino Uno board. But you can use any Arduino board of your choice. On the Arduino Uno, Mega, and Nano, this LED is connected to pin 13 and is often labeled “L” on the board.

LED Blinking Code

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

As we know that the built-in LED is connected to pin 13, we can write the program as below:

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(13, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

Explaining the code

The Arduino program has two main parts: a setup section and a loop section. What goes inside the setup section runs only once, after powering on the Arduino, and the program inside the loop runs continuously until the power goes off.

A bare minimum Arduino programming structure is given below.

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:
}

Now, coming back to our example, an LED blink requires two steps in the program. First, we need to set the Arduino pin as an output pin, and then we have to toggle the pin to blink the LED.

We only need to set the pinMode (INPUT/OUTPUT) once in an Arduino program, so it goes inside the setup section.

void setup() {
   pinMode(13, OUTPUT);
}

Now, we have to toggle the LED pin on and off. We will do this inside the loop section so that it runs continuously.

We will use Arduino’s digitalWrite() function to turn the LED on and off. The basic syntax of the function is:

digitalWrite(pin, value);

It has two parameters inside the parentheses:

  • pin: The number of the digital pin you want to control (e.g., 234, …, 13, or a constant like LED_BUILTINledPin etc.).
  • value: What you want to set the pin to. It must be either HIGH or LOW.

So, digitalWrite(LED_BUILTIN, HIGH); or digitalWrite(13, HIGH); will turn on the onboard LED and digitalWrite(LED_BUILTIN, LOW); will turn off the LED.

If we only use these two lines of code, the LED will blink so fast that we can’t see it with the naked eye. So we have to put some delay in between to slow things down.

We are using the delay() function for that. It works with milliseconds, so delay(1000); means one second delay.

The complete code inside the loop works like this:

digitalWrite(13, HIGH); // Turn LED On
delay(1000);            // Wait for 1 Second
digitalWrite(13, LOW);  // Turn LED Off
delay(1000);            // Wait for 1 Second

In this section we will see how to wire an LED with Arduino, and program it to blink. In this example I will use a standard 5mm LED.

Ads

So without wasting any time, let’s build the circuit.

Arduino LED Blinking Circuit

Wiring an LED with Arduino is very easy. Connect the positive leg of the LED to any digital pin (we will use pin 7) of the Arduino and the negative leg to Ground. Use a current limiting resistor (220 ohm) in series with the LED to prevent it from burning.

Look at the circuit below for reference.

Arduino LED Circuit

Arduino LED Blinking Code

The following code will blink the LED with a 1 second interval. You can increase or decrease the delay to adjust the blinking frequency.

const int ledPin = 7;
void setup() {
  pinMode(ledPin, OUTPUT);
}
void loop() { 
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
}

The program is almost the same as the previous one, with just one key difference: we use a constant integer named ledPin to store the Arduino pin number. This constant is then used throughout the code.

By doing this, we can connect the LED to any Arduino pin without having to edit multiple lines of code. If we change the PIN, we only need to update the ledPin declaration, and the program will work correctly.

Control Multiple LEDs using Arduino

In this section, we will take a look how we can control multiple LEDs. We will take 4 different color LED and blink them in sequence.

Lets build the circuit.

Circuit Diagram

The circuit is quite simple. You can add more LEDs to your existing setup in the same way you add a single LED.

Place 4 LEDs side by side on the breadboard. Connect their positive legs to four different digital pins on the Arduino (here we’ll use pins 2, 3, 4, and 5). Connect their negative legs to the common ground rail through a 220 Ω resistor. Finally, connect the Arduino ground pin to this common ground rail.

Follow the circuit diagram below for reference.

Arduino with Four LED Circuit

Arduino LED Code

const int redLED = 1;
const int greenLED = 2;
const int yellowLED = 3;
const int blueLED = 4;
void setup() {
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
}
void loop() {
  digitalWrite(redLED, HIGH);
  delay(1000);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, HIGH);
  delay(1000);
  digitalWrite(greenLED, LOW);
  digitalWrite(yellowLED, HIGH);
  delay(1000);
  digitalWrite(yellowLED, LOW);
  digitalWrite(blueLED, HIGH);
  delay(1000);
  digitalWrite(blueLED, LOW);
}

Explaining the Code

There is nothing new here. We have already done this for a single LED; now we simply repeat the same steps for four LEDs.

Ads

First, we create four integer constants to store the pin numbers we are using.

const int redLED = 1;
const int greenLED = 2;
const int yellowLED = 3;
const int blueLED = 4;

Then, in the setup section, we declare these pins as output pins.

pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(blueLED, OUTPUT);

In the loop section, we turn the LEDs on and off one after another.

digitalWrite(redLED, HIGH);
delay(1000);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
delay(1000);
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
delay(1000);
digitalWrite(yellowLED, LOW);
digitalWrite(blueLED, HIGH);
delay(1000);
digitalWrite(blueLED, LOW);

You can see that the above code contains many repetitive lines. We can reduce this repetition by using for loops and arrays.

The following code will perform the same task as the previous one, but in a more efficient way.

// assign LED pin to the array
const int ledPin[4] = { 1, 2, 3, 4 };
int delay_t = 500;

void setup() {
  // set pins 1,2,3,4 as output
  for (int i = 0; i <= 4; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop() {
  // iterate over the pins:
  for (int i = 0; i <= 3; i++) {
    digitalWrite(ledPin[i], HIGH);
    delay(delay_t);
    digitalWrite(ledPin[i], LOW);
    delay(delay_t);
  }
}

Help me to Build more Projects for You!

At CircuitGeeks, we're passionate about creating exciting electronics projects and sharing our knowledge with the world. Our projects are free and open to everyone, but we would need your support to keep the creativity flowing!

If you enjoy our work and find our projects valuable, please consider supporting us on Buymeacoffee. By buying us a coffee, you help us buy more components and keep our projects going strong.

We truly appreciate your contribution!

1 thought on “Arduino LED Blinking – Complete Tutorial”

Leave a Comment