Publications

Blink LED Arduino

Blink LED Code Example For Arduino

By Timothy Coyle

arduino

Every hardware development platform starts off with the blink LED example or also sometimes called flash the LED example where you turn an LED on and off. This is equivalent to the Hello World example used in learning programming languages where you print Hello World to your computer screen. So here's my take on it for the Arduino Uno.

Hardware Setup

The Arduino Uno board has an LED connected to pin 13 so you can use the code to blink this LED but it's good to understand how LEDs work so we are going to connect an external LED in our hardware setup.

arduino

In the schematic above hopefully you can undestand what's going on and how everything connects together. You can take the 3.3V or 5V output pin from the Arduino and a Ground pin and connect them to the breadboard power and ground. In this case we won't be using Power from the breadboard since the output pin will provide the power but it's good practice to setup your breadboard like this. Then we take pin 12 (this can be any output pin you define) and connect it to the load resistor. Then the load resistor connects to the LED. Finally the LED connects back to Ground to close the circuit.

led current limiting resistor

Remember that in order to use an LED with a microcontroller digital output you need to have a load resistor connected to the LED. The LED (like any electrical component) has an allowable range of current and voltage it can handle before it will burn out and get destroyed. You can look at the table above and see the voltage drop across the LED (about 2V) and the allowable current (about 20mA). You can download the sample LED datasheet here. So we need to add a resistor to "limit" the current flowing through the LED so it's not too much. We use Ohm's Law (V=I * R) to calculate an acceptable resistor value.

led current limit resistor

Blink LED Sketch

Below is the Sketch for the Arduino Uno to blink an external LED:

//
//Date: 5/31/15
//Version: 1.0
//
//Comments:
//This sketch defines a digital output and blinks the LED on and off. The delay can be used to set 
//how fast the LED flashes. 
//This is based on an Uno board with a LED attached to pin 13 but the pin_led variable can be used 
//to assign a different output for an external LED.

// Global Variables
int delay_led = 50;  // Delay in msec (smaller number flashes LED faster)
int pin_led = 12;    // Define which digital output pin to use that is attached to an LED


// The setup runs once when you press reset or power on the board
void setup() {
  // initialize digital pin as an output
  pinMode(pin_led, OUTPUT);

}

// Main code for Arduino goes in the loop function to be run over and over again
void loop() {
  // write to digital pin output
  // turn it on and off with a delay which will flash the LED on the Uno board
  digitalWrite(pin_led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(delay_led);              // wait for a defined period of time
  digitalWrite(pin_led, LOW);    // turn the LED off by making the voltage LOW
  delay(delay_led);              // wait for a defined period of time

}
                            

You can get the example code on Github

GITHUB

Newsletter

Sign up for my monthly email newsletter Signal Bytes to get the latest on embedded hardware design