Arduino 4 led running leds

Arduino Running Leds Project, Arduino LEDs with Special Effects

I present a simple Arduino running lights project for beginners. This project uses four Light Emitting Diodes to display a to and fro pattern of LEDs. The first code produces a sweeping effect while the second code is for the Arduino running LEDs.

You will require the following apparatus:

  • 1 x Arduino Uno
  • 4 x LEDs
  • 4 x 1k ohm resistors
  • 5 x Jumper wires
  • A Mini breadboard

Connect the Circuit as shown in the diagram above or as shown in the schematic below;

Arduino Uno Running LEDS schematic

  • Connect the anodes of the LEDs to pins 7,8,9,10 of Arduino respectively.
  • Connect a 1k resistor from the cathode of LED to the ground (-) rail on the breadboard
  • Repeat by connecting the remaining resistors to the cathodes of the other LEDs and then to the ground rail of the breadboard
  • Finally, run a jumper wire from the ground rail of the breadboard to the GND on Arduino Uno.

Upload the following code to the Arduino;

/********************************************************************
 * Source: www.jitimu.com/learn-stem
 * Anodes of LEDs connected to pins 7, 8, 9 and 10
 * You can use any resistor value from 220 ohms onward<br ********************************************************************/
int led1 =7;
int led2 =8;
int led3 =9;
int led4 =10;

int ledspeed = 500;

void setup() {
 pinMode(led1,OUTPUT);   //Set pins as output
 pinMode(led2,OUTPUT);
 pinMode(led3,OUTPUT);
 pinMode(led4,OUTPUT);
}

void loop() {
 //Turns on each LED at a time
digitalWrite(led1, HIGH);     
delay(ledspeed);
digitalWrite(led2, HIGH);
delay(ledspeed);
digitalWrite(led3, HIGH);
delay(ledspeed);
digitalWrite(led4, HIGH);
delay(ledspeed);

//Turns off the LEDs one at a time then repeats the process
digitalWrite(led1, LOW); 
delay(ledspeed);
digitalWrite(led2, LOW);
delay(ledspeed);
digitalWrite(led3, LOW);
delay(ledspeed);
digitalWrite(led4, LOW);
delay(ledspeed);
}

 How Arduino Running LEDs code works

We start by declaring the pins we are going to use, i.e pins 7,8,9 and 10. We label these pins as led1, led2, led3, and led4 to recognize them down the code.

int led1=7,led2=8,led3=9,led4=10;

We will use this variable to set the on-time delay of the LEDs, or simply the speed of the run.

int ledspeed = 500;

In this case, each LED will turn on/off 0.5 seconds late.

This code programs Arduino to make pins 7,8,9, and 10 outputs;

void setup() {
//Set pins as output 
pinMode(led1,OUTPUT); 
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT); 
pinMode(led4,OUTPUT); 
}

The setup code only runs once when Arduino initiates the code.

To make running LEDs, we put our code in the loop section so that it runs repeatedly.

void loop() {
 //Turns on each LED at a time
digitalWrite(led1, HIGH);     
delay(ledspeed);
digitalWrite(led2, HIGH);
delay(ledspeed);
digitalWrite(led3, HIGH);
delay(ledspeed);
digitalWrite(led4, HIGH);
delay(ledspeed);

//Turns off the LEDs one at a time then repeats the process
digitalWrite(led1, LOW); 
delay(ledspeed);
digitalWrite(led2, LOW);
delay(ledspeed);
digitalWrite(led3, LOW);
delay(ledspeed);
digitalWrite(led4, LOW);
delay(ledspeed);
}

The function, digitalWrite(led, HIGH) turns on each LED one at a time after delay elapses.

DigitalWrite(led, LOW) will turn off the LEDs one at a time, before the cycle repeats.

We can reduce the memory used from 3% to 2%when we make use of the if statements to simplify the code.

The following code will turn off the previous LED before turning on the next one;

 

/********************************************************************
 *Source: www.jitimu.com/learn-stem
 *Anodes of LEDs connected to pins 7, 8, 9 and 10
 *You can use any resistor value from 220 ohms onward
 ********************************************************************/
int ledspeed = 100;

void setup() {
 //Set pins as output
for(int n = 7; n<=10; n++){
  pinMode(n, OUTPUT);
  }
}

void loop() {
 //Turns on the LEDs one at a time then repeats the process
for(int i = 7; i<=10; i++){
  digitalWrite(i,HIGH);
  delay(ledspeed);
  digitalWrite(i,LOW);
  }

Also read:

  1. Control an LED with a Button, Project for Beginners in Arduino Programming
  2. Arduino Motion Activated Intruder Alert, with Visual and Sound
  3. Arduino Binary Counter