Arduino - LED - Blink

In this tutorial, we learn how to control LED with using Arduino, how to program for Arduino to turn LED on/off, and how to blink LED

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×LED
1×220 ohm resistor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Optional) Screw Terminal Block Shield for Arduino

One of the following breadboard:

1×Full-sized 830-Point Breadboard
1×Half-sized 400-point Breadboard
1×Mini-sized 170-point Breadboard
1×All-sized Breadboard Kit
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About LED

Pinout

LED includes two pins:

  • Cathode(-) pin: needs to be connected to GND (0V)
  • Anode(+) pin: is used to control LED's state
LED Pinout

How It Works

After connecting the cathode(-) to GND:

  • If connecting GND to the anode(+), LED is OFF.
  • If connecting VCC to the anode(+), LED is ON.
How LED Works

Besides, if generating a PWM signal to the anode(+), the brightness of LED is changed according to PWM value ( described in detail in this tutorial)

※ NOTE THAT:

  • For most of LED, we need to use a resistor. The resistor can be placed between the anode(+) and VCC or between the cathode(-) and GND. The value of the resistor depends on the specification of the LED.
  • Some kinds of LEDs have a built-in resistor. We may not need to use a resistor for those kinds of LEDs.

Arduino - LED

When an Arduino's pin is configured as a digital output, the pin's voltage can be programmatically set to GND or VCC value.

By connecting the Arduino's pin to LED's anode(+) pin (via a resistor), we can programmatically control LED's state.

Wiring Diagram

We are going to run through two examples:

  • Example code controls the built-in LED on Arduino/Genuino UNO.
  • Modifying Arduino Code controls the external LED.
Arduino LED Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How To Program

  • Configure an Arduino's pin to the digital output mode by using pinMode() function. For example, pin 9:
pinMode(9, OUTPUT);
  • Program the pin to GND to turn OFF led by using digitalWrite() function:
digitalWrite(9, LOW);
  • Program the pin to VCC to turn ON led by using digitalWrite() function:
digitalWrite(9, HIGH);

Arduino Code

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • On Arduino IDE, Go to File Examples 01.Basics Blink example
/* Blink Turns an LED on for one second, then off for one second, repeatedly. Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to the correct LED pin independent of which board is used. If you want to know what pin the on-board LED is connected to on your Arduino model, check the Technical Specs of your board at: https://www.arduino.cc/en/Main/Products modified 8 May 2014 by Scott Fitzgerald modified 2 Sep 2016 by Arturo Guadalupi modified 8 Sep 2016 by Colby Newman This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Blink */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever 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 }
  • Click Upload button on Arduino IDE to upload code to Arduino
Arduino IDE - How to Upload Code
  • See the result: The built-in LED toggles between ON/OFF periodically every second.

Code Explanation

Read the line-by-line explanation in comment lines of code!

※ NOTE THAT:

The above code uses the delay(). This function blocks Arduino from doing other tasks during the delay time. If your project requires to do some tasks, avoid blocking Arduino by using the non-blocking method for Arduino.

Modifying Arduino Code

Modifying code to control the external LED

Quick Steps

  • Connect LED to Arduino via the above wiring diagram.
  • Modify code:
    • Change LED_BUILTIN to 9
    • Change the delay from 1000 ms to 500 ms
    // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 9 as an output. pinMode(9, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level) delay(500); // wait for 500 milliseconds digitalWrite(9, LOW); // turn the LED off by making the voltage LOW delay(500); // wait for 500 milliseconds }
    • Click Upload button on Arduino IDE to upload code to Arduino
    • See the result: LED switches between ON/OFF periodically. The LED blinks faster.

    ※ NOTE THAT:

    This tutorial provides in-depth knowledge that helps you understand the working principle. To make it easy, you can use Arduino - LED library.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Challenge Yourself

  • Connect LED to another pin of Arduino and change the blink time.
  • Turn on/off LED by button. Hint: Refer to Arduino - Button.

Additional Knowledge

Which pins on Arduino UNO can be used as an output pin to control LED?
  • Pin 0 to pin 13
  • Pin A0 to pin A5

※ NOTE THAT:

At a time, one pin can take only one task. If you already used a pin for another task (e.g, digital input, analog input, PWM, UART...), you should NOT use it as digital output to control LED. For example, if we use Serial.println() function, we should NOT use pin 0 and 1 for any other purpose because these pins are used for Serial.

Extendability

This tutorial shows how to use the output pin of Arduino to control an LED. We can apply this code to control ON/OFF any devices, even big machines.

※ NOTE THAT:

for devices/machines that use a high power supply ( > 5v) and/or high-current consumption, we need to use a relay between output pin and devices/machines - see Arduino - Relay.

LED on Commercial Products

Small LEDs usually are used to indicate the status of devices. For examples:

Big LEDs usually are used for lighting. They can be combined into groups. For examples:

Function References

The Best Arduino Starter Kit

※ OUR MESSAGES