Arduino Blink LED Tutorial – Circuit and Code Example

Arduino Blink LED is the first and most important project for anyone starting with Arduino. It is often called the “Hello World” program of Arduino programming.

In this tutorial, you will learn how to build a simple Arduino Blink LED circuit and write a basic Arduino program to blink an LED. This project helps beginners understand how Arduino works, how to connect components, and how to control an LED using code.

The Arduino Blink LED project is very easy because the circuit connections are simple and the code is short. Even if you have no background in electronics or programming, you can follow this guide step by step. By the end of this tutorial, you will successfully blink an LED using Arduino.

What You Will Learn:

  • What is the Arduino Blink LED example
  • How to connect an LED to Arduino
  • How to write Arduino Blink LED code
  • How pinMode(), digitalWrite(), and delay() work.

Once you understand this, you can easily move to buttons, sensors, motors, and displays.

 

Components Required:

To blink an LED using Arduino, you need the following components:

Component Quantity
Arduino board (Uno / Nano / Mega) 1
LED (any color) 1
Resistor (220Ω or 330Ω) 1
Breadboard 1
Jumper wires As required
USB cable 1

 

Note: Most Arduino Mega boards come with a built-in LED connected to Pin 13. You can use it without any external circuit.

 

Understanding LED and Switch (Arduino Mega / ATmega2560):

In this blog post, we will use the built-in LED on the Arduino Mega 2560. However, before working with an LED or a switch, it is important to understand how these components work and how they behave electrically. This basic knowledge helps you avoid common mistakes and prevent hardware damage.

Light Emitting Diode (LED):

An LED (Light Emitting Diode) is a semiconductor device that produces light when current flows through it. Just like a normal diode, an LED allows current to flow in only one direction:

From Anode (+) to Cathode ()

When the LED is forward-biased (anode at higher voltage and cathode at lower voltage), electrons and holes recombine inside the semiconductor material. This process releases energy in the form of visible light.
The color of the LED depends on the semiconductor material used during manufacturing.

 

How to Identify LED Polarity:

Since in LED current only flows one direction, you must connect the LED correctly.

  • Check the Legs: The longer leg is usually the Anode (+), and the shorter leg is the Cathode (-).
  • Check the Inside: If the legs are trimmed, look inside the plastic casing. The larger, flag-shaped metal piece is the Cathode (-), while the smaller piece is the Anode (+).

LED Image

 

How to Connect an LED:

You should never connect an LED directly to a power source or an Arduino pin without a resistor. LEDs have very low internal resistance; without protection, they will draw too much current and burn out instantly. To prevent this, we always place a resistor in series with the LED. This resistor (current-limiting resistor) “limits” the current to a safe level.

Recommended Resistor Values for Arduino LED:

For a standard 5V Arduino circuit, here are the safe ranges:

Desired Brightness Recommended Resistor Note
High Brightness 220 Ω Safe limit for most 5mm LEDs
Standard 330 Ω Best balance of brightness and safety
Low Power / Dim 1 kΩ (1000 Ω) Good for status indicators

 

Note: Avoid using resistors lower than 220Ω with a 5V supply, as this can exceed the standard 20mA current limit of many LEDs.

 

Calculating the Exact Value (Ohm’s Law):

If you want to be more precise, you can calculate the exact resistor value required for the LED using Ohm’s Law.

The Scenario:

  • Supply Voltage (𝑉𝑠𝑢𝑝𝑝𝑙𝑦): 5V (from Arduino).
  • LED Forward Voltage (𝑉𝐿𝐸𝐷𝐶𝑢𝑟𝑟𝑒𝑛𝑡 ): ~2V (for a Red LED)
  • Target Current (I): 10mA (0.01 A)

 

To find the right resistor, use this simple formula:

Resistor (R) = (𝑉𝑠𝑢𝑝𝑝𝑙𝑦  − 𝑉𝐿𝐸𝐷𝐶𝑢𝑟𝑟𝑒𝑛𝑡 ) / I;

 

So, calculation is:

𝑅=(5𝑉−2𝑉)/0.01𝐴 

R = 300Ω

Since 300Ω is not a standard resistor value, you would round up to the nearest standard size, which is 330Ω.

 

Arduino Blink LED Circuit Diagram:

On the Arduino Mega 2560, the built-in LED (marked L) is connected to digital pin 13. Internally, this pin maps to PB7 of the ATmega2560 microcontroller.

  • See the below image for Pin Mapping:
    Arduino Pin: Digital Pin 13
  • ATmega2560 Chip Pin: Port B, Bit 7 (PB7)

Led Connection Arduino

Unlike a basic LED circuit where an LED connects directly to a pin, the Mega uses a small buffer circuit (op-amp) between PB7 and the LED. This buffer allows the microcontroller to control the LED without putting extra electrical load on the pin, helping the board work reliably even when timers or PWM are used. Because of this design, the built-in LED is safe to use for learning and debugging.

 

Arduino Blink LED Program:

Open the Arduino IDE and write the following code:

/*
 * Created by Aticleworld.com
 *
 * Tutorial page: https://aticleworld.com/arduino-blink-led
 */

// the setup function runs once when you press reset or power the board
void setup()
{
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop()
{
  // turn the LED on (HIGH is the voltage level)
  digitalWrite(13, HIGH);

  // wait for 500 milliseconds
  delay(500);

  // turn the LED off by making the voltage LOW
  digitalWrite(13, LOW);

  // wait for 500 milliseconds
  delay(500);
}

Code Explanation:

Writing Arduino code is easy, especially if you have some basic programming knowledge.

But if you are a complete beginner, don’t worry. Here, I will explain the code step by step in simple language, so you can understand how it works and start writing your own Arduino programs with confidence.

setup() Function:

void setup()
{
    pinMode(13, OUTPUT);
}
  • Runs only once when Arduino starts
  • Configures the LED pin as an output

loop() Function:

void loop()
{
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    delay(500);
}
  • Runs continuously
  • Turns LED ON
  • Waits 500ms (0.5 second)
  • Turns LED OFF

 

Uploading the Program:

Once your code is ready, follow these steps to upload it to the ATmega2560 (Arduino Mega 2560):

Connect the Board:

Plug your Arduino Mega 2560 into your computer using a USB cable.

Open the Arduino IDE:

Launch the Arduino IDE on your PC or laptop.

Select the Board:

Go to Tools → Board → Arduino AVR Boards and select Arduino Mega or Mega 2560.

Select arduino board

Select the Port:

Go to Tools → Port and choose the port that appears after connecting the board (for example, COM4 on Windows or /dev/tty… on macOS/Linux).

Upload the Code:

Click the Upload button (right-arrow icon) or press Ctrl + U.

Upload Firmware in Arduino

 

After the upload is complete, the built-in LED will start blinking according to your program.

 

Register-Level (Bare-Metal) Control in Arduino:

If you want to write efficient firmware or truly understand the ATmega2560 architecture, you need to work closer to the hardware. In this approach, we move a little closer to the hardware while still working inside the Arduino environment. We are not replacing the Arduino IDE or toolchain instead, we are simply bypassing some high-level Arduino functions and accessing the ATmega2560 registers directly.

On the ATmega2560, Arduino digital pin 13 maps to PB7 (Port B, bit 7). By configuring and toggling this register bit, we can blink the LED with minimal overhead and full control over the hardware.

Now, let’s see how to write the code.

/*
 * Created by Aticleworld.com
 *
 * Tutorial page: https://aticleworld.com/arduino-blink-led
 */

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  // 1. Data Direction Register B (DDRB)
  // Set PB7 to 1 to configure it as Output.
  // Logic: 1000 0000 (Binary) = 0x80 (Hex)
  DDRB |= (1 << DDB7);

  while (1) 
  {
    // 2. Port B Data Register (PORTB)
    // Set PB7 High to turn LED ON
    PORTB |= (1 << PORTB7);

    _delay_ms(500); // Built-in AVR delay function

    // Set PB7 Low to turn LED OFF
    // Use bitwise AND with NOT to clear the bit
    PORTB &= ~(1 << PORTB7);

    _delay_ms(500);
  }
  return 0;
}

Code Explanation:

Now let’s take a moment to understand what the code is doing. We will go through it step by step, so everything makes sense.

Configure as output:

DDRB |= (1 << DDB7);

This line configures PB7 as an output pin. DDRB is the Data Direction Register for Port B. By setting bit 7 to 1, we tell the ATmega2560 that this pin will drive an output signal (instead of acting as an input).

LED ON:

PORTB |= (1 << PORTB7);

This line turns the LED ON. Setting bit 7 of the PORTB register to 1 drives the pin HIGH (5V), which lights the built-in LED.

LED OFF:

PORTB &= ~(1 << PORTB7);

This line turns the LED OFF. Here, we clear bit 7 of PORTB by using a bit mask. The ~ operator inverts the mask so only bit 7 is cleared, while all other bits in Port B remain unchanged.

 

You May Also Like