#5Intermediate

Walking Led

4.6(83)
186 completed
Walking Led
Project that creates a walking light effect by lighting 6 LEDs in sequence. Teaches the use of array and multiple pin control with for loop.

Video

Circuit Diagram

Walking Led - Circuit

Source Code

Arduino (C++)
1int ledPins[] = {2, 3, 4, 5, 6, 7};
2
3void setup() {
4  for(int i = 0; i < 6; i++) {
5    pinMode(ledPins[i], OUTPUT); 
6  }
7}
8
9void loop() {
10  for(int i = 0; i < 6; i++) {
11    digitalWrite(ledPins[i], HIGH);
12    delay(150);
13    digitalWrite(ledPins[i], LOW);  
14  }
15}

FAQ

Why is it better to use an array?▾

If you store the pin numbers in an array, you can use a for loop to cycle through all the LEDs in a single line of code. To change the number of LEDs, you just need to update the array; you don't have to write a separate line for each LED.

If 6 LEDs light up at the same time, will the Arduino be damaged?▾

With a 330 Ω resistor, each LED draws approximately 9–10 mA; 6 LEDs total about 60 mA, which is within the Uno’s safe limits. The recommended upper limit for a single pin is 20 mA, and the board’s total I/O current is 200 mA.

How do I reverse the walking direction?▾

Set up the `for` loop in reverse order: `for (int i = 5; i >= 0; i--)`. This way, the light moves in the opposite direction. If you place the two loops one after the other, you’ll get a lightning bolt effect.

One of the LEDs isn't lit—what could be the reason?▾

The LED might be installed upside down, its leg might not be seated properly on the breadboard, or the resistor might be in a different row. You can determine whether the LED is defective or if there’s a connection issue by replacing it with a working LED.

Walking Led | BlueGrays