Feb 172011
 

Creating a LED chaser with arduino:

In this activity we’ll use an array of LEDs to do some neat stuff! This is a variation of the simple LED blink activity, except that it uses 5 LED’s and we’ll time them so that it looks look like a light is moving between them.

To build this activity you’ll need:

  • 330 or 470 Ohm resistor
  • 5 LEDs (all the same color looks nicer)
  • jumper wires
  • breadboard
  • Arduino

Connecting the Arduino and building the circuit:

0) Connect your Arduino to your computer using a USB cable. The power light should come on.

1) Place your 5 LEDs in the breadboard so that the cathode side of each LED can be connected to the ground rail with small jumper wires (shown below). A longer jumper wire should also be connected (through a resistor) to the ground rail.

2) Connect the other leg of each LED to pins 12,11,10,9, and 8 on the digital side of the Arduino. The long jumper wire that you connected to the ressitor also gets connected to the Gnd pin on the Arduino.

3) Open the Arduino IDE and type in the following code. This code is really just the same as the LED blink code except that extra output pins have been added to the setup section (one pin for each LED), and the loop section now contains instructions to turn each pin on and off after a certain delay.

void setup(){
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
}

void loop(){
  digitalWrite(12,HIGH);
  delay(100);
  digitalWrite(12,LOW);
 delay(25);
  digitalWrite(11,HIGH);
  delay(100);
  digitalWrite(11,LOW);
 delay(25);
  digitalWrite(10,HIGH);
  delay(100);
  digitalWrite(10,LOW);
 delay(25);
  digitalWrite(9,HIGH);
  delay(100);
  digitalWrite(9,LOW);
 delay(25);
  digitalWrite(8, HIGH);
  delay(100);
  digitalWrite(8, LOW);
 delay(25);
}

4) Check the code by pressing the verify button and then upload to the Arduino using the upload button. It should run Automatically!

The code above will make the light “move” from the first LED to the last, but not back and forth. To do that we’ll need to add in a few more lines as shown below.

Arduino chaser variation -  “Knight Ride-uino”

These lines turn the middle LEDs on in a reverse order, making it look like the Light is moving back to its starting position. Putting a longer delay on the first and last LED seems to improve the “look” of the chaser.

void setup(){
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
}

void loop(){
//big pause on pin 12
  digitalWrite(12,HIGH);
  delay(200);
  digitalWrite(12,LOW);
  delay(25);
  digitalWrite(11,HIGH);
  delay(100);
  digitalWrite(11,LOW);
 delay(25);
  digitalWrite(10,HIGH);
  delay(100);
  digitalWrite(10,LOW);
 delay(25);
  digitalWrite(9,HIGH);
  delay(100);
  digitalWrite(9,LOW);
 delay(25);
// big pause on pin 8
  digitalWrite(8, HIGH);
  delay(200);
  digitalWrite(8, LOW);
 delay(25);
//new lines to make the light move
//in the opposite direction
  digitalWrite(9,HIGH);
  delay(100);
  digitalWrite(9,LOW);
 delay(25);
  digitalWrite(10,HIGH);
  delay(100);
  digitalWrite(10,LOW);
 delay(25);
  digitalWrite(11,HIGH);
  delay(100);
  digitalWrite(11,LOW);
 delay(25);

}

Challenge:

Try varying the delay times to get the rhythm you want and adding extra LEDs!

 Posted by at 1:16 am

  2 Responses to “Arduino: Sequencer – 5 LED chaser”

  1. Looks good but how to use the resistor in it?
    do we need each and every resistor to be Connected in LED??

    • The code is set to light only 1 led at a time, and each LED is grounded through the same pin. Therefore we can just put a properly sized resistor in series with the ground pin. You can see how it’s set up in the diagram.

Leave a Reply to cd Cancel reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>