row of 3 clear LED's

LED control sequencer using Arduino board

After programming a microcontroller to blink an LED, the natural curiosity is to do something more exciting. So, in this tutorial we will use several LED’s to make a more dramatic light show. Again we will use an Arduino board to do this project. But first of all we’ll go through some background before starting.

Each of the ATMega328 microcontroller’s output pins are capable of sourcing  enough current to power up an LED so no driver circuitry is required apart from a current limiting resistor. Most LED’s have a typical maximum current of about 20mA although their voltage requirements vary depending on the color of the LED. In order to not overload the LED we will use a current limiting resistor in series with it, which limits the current to prevent it from being damaged. The term “in series” means the components are wired in a line one after the other.

LED current limiting resistors

As we are using USB to power the microcontroller, the pin outputs will each be 5V.  We will use 3 high intensity LED’s (red, yellow and blue). Each one of these have different voltage requirements. Now we’ll calculate the resistor values for each LED, but first we need to determine the the voltage and current requirements by looking at the manufacturer’s product datasheet. Datasheets are usually available from the manufacturers website.


Red LED resistor value

The red LED we are using requires a typical voltage of 2V and a current of 20 mA. However we will limit the current to 15mA while using a supply voltage of 5V.

Using ohm’s law V=IR or in word terms voltage equals current multiplied by resistance. We want to calculate resistance so we rearrange the formula to suit. If your mathematics knowledge is not that great then don’t worry as we have done the calculations for you.

Feeding the red LED values into the Ohm’s Law formula:

R=V (voltage across resistor)/I(current through the resistor).

R = V(supply voltage – LED voltage)/I(current through the resistor).

R = (5V – 2V) / 0.015 A

R = 200 ohm’s

As the resistors only come in certain values, we will choose the closest value which is 220 Ohm’s.

Using 220 Ohm’s drops the current slightly to 13.6 mA which will still work very well.

Yellow LED resistor value

As the yellow LED also requires 2V and the current we wish to use is 15mA the resistor will be the same value as for the red LED above of 200 ohms. So we choose the closest value of 220 ohms.

Blue LED resistor value

The blue LED uses the highest voltage of 3.3V at 15mA. Still using a supply voltage of 5V, we now put these values in the ohm’s law formula to give:

R = (5V-3.3V)/0.015

R = 113 Ohms

We will use a 100 ohm resistor which is the closed practical value we have to 113 ohms.

Wiring up the Circuit.

 

circuit-diagram-of-LED-setup

We’ll now wire up the board and as we are using external LED’s and resistors we need to use an additional prototype breadboard. Both boards will be wired up as in the diagram above. Remember the LED is polarized and will work when connected in one direction only, unlike the resistor which does not have a polarity and so can be connected in any direction.

led-sign

Parts required

  • Microcontroller Board (Arduino Uno or compatible board) with USB cable
  • Prototyping breadboard
  • 7 Dupont Wires (plug-to-plug type) or solid core hook up wire
  • 5mm high intensity Red LED
  • 5mm high intensity yellow LED
  • 5mm high intensity Blue LED
  • 2x 220 ohm resistor (red-red-black)
  • 100 ohm resistor (brown-black-black)

LED breadboard layout.

 

Once the boards are wired up we need to write a program to make the LED’s do something. We will blink the LED’s once each and cycle through that twice, then blink each LED 4 times quickly in sequence before looping back to the beginning. Even though the sequence is simple it does look quite dramatic. Watch the video to see the LED’s in operation.


 

Step 1 – Connect the microcontroller board to your PC using a USB cable.

Step 2 – Start a blank project in the IDE.

Step 3 – Copy the following code into the blank project.

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

void loop() {             //continuously loop the program
digitalWrite(1, HIGH);   //Turn pins 1,2&3 on & off in sequnce for two rounds
delay(100);
digitalWrite(1, LOW);
delay(100);
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
digitalWrite(3, HIGH);
delay(100);
digitalWrite(3, LOW);
delay(100);
digitalWrite(1, HIGH);
delay(100);
digitalWrite(1, LOW);
delay(100);
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
digitalWrite(3, HIGH);
delay(100);
digitalWrite(3, LOW);
digitalWrite(1, HIGH);   //Flash pin 1 (Blue LED) 4 times
delay(50);
digitalWrite(1, LOW);
delay(50);
digitalWrite(1, HIGH);
delay(50);
digitalWrite(1, LOW);
delay(50);
digitalWrite(1, HIGH);
delay(50);
digitalWrite(1, LOW);
delay(50);
digitalWrite(1, HIGH);
delay(50);
digitalWrite(1, LOW);
delay(50);
digitalWrite(2, HIGH);   //Flash pin 2 (yellow LED) 4 times
delay(50);
digitalWrite(2, LOW);
delay(50);
digitalWrite(2, HIGH);
delay(50);
digitalWrite(2, LOW);
delay(50);
digitalWrite(2, HIGH);
delay(50);
digitalWrite(2, LOW);
delay(50);
digitalWrite(2, HIGH);
delay(50);
digitalWrite(2, LOW);
delay(50);
digitalWrite(3, HIGH);   //Flash pin 3 (Red LED) 4 times
delay(50);
digitalWrite(3, LOW);
delay(50);
digitalWrite(3, HIGH);
delay(50);
digitalWrite(3, LOW);
delay(50);
digitalWrite(3, HIGH);
delay(50);
digitalWrite(3, LOW);
delay(50);
digitalWrite(3, HIGH);
delay(50);
digitalWrite(3, LOW);
delay(50);
}

Please note, the code above can be made more efficient by using several loops, but so you can understand easier we have left out possible loops apart from the main loop that is essential. In this way you can go through the code line by line and easily understand it without getting lost in more complicated code. But there is nothing stopping you from experimenting with loops and more efficient coding which may result in less use of the microcontroller’s  memory.

Step 4 – Upload the code to your Board.

A few seconds after the code has been uploaded the LED’s flashing loop should start.

What do you think ? Impressive for a few components and a few lines of code ?

If you liked this post, please share it with your friends and see what they think.

If you can’t wait for next week’s project post please visit our blog page for other regular updates and information on the world of microcontrollers and electronics.

Before you move on, I would love for you to suggest some topics you would like to see covered on the Smartmicrocontroller.com website this year by leaving a comment in the comments section in the youtube channel, or send us a message through one of the social media links on the about page.