Lab set Program-1
Develop a program to blink 5 LEDs back and forth.
Objective
To write an Arduino program that makes 5 LEDs blink one after the other in a back-and-forth pattern, creating a "chasing light" effect.
Learning Outcomes
By the end of this experiment, students will be able to:
-
Connect multiple LEDs to Arduino digital pins.
-
Control LEDs sequentially using
for
loops. -
Understand the concept of array indexing for repetitive tasks.
-
Apply logic to reverse direction (back-and-forth movement).
Components (from Tinkercad Palette)
-
Arduino Uno board
-
Breadboard
-
5 LEDs (any color)
-
5 Resistors (220Ω each)
-
Jumper wires
-
USB cable
Circuit Wiring
-
Connect 5 LEDs in a row on the breadboard.
-
Connect each LED anode (+, long leg) to Arduino digital pins 2, 3, 4, 5, 6 (through a resistor).
-
Connect all LED cathodes (-, short leg) to Arduino GND.
-
Power Arduino using USB.
(In Tinkercad, drag 5 LEDs, align resistors, and connect them as shown below.)
Step-by-Step in Tinkercad
-
Open Tinkercad Circuits.
-
Create a new circuit.
-
Place Arduino Uno and a breadboard.
-
Place 5 LEDs in a row.
-
Connect each LED’s anode to pins 2–6 through resistors.
-
Connect cathodes to GND.
-
Paste the code below into the Arduino editor.
-
Start simulation → Observe LEDs blinking back and forth.
Arduino Program
// Blink 5 LEDs back and forth
int leds[] = {2, 3, 4, 5, 6}; // LED pins
int numLeds = 5;
void setup() {
for (int i = 0; i < numLeds; i++) {
pinMode(leds[i], OUTPUT);
}
}
void loop() {
// Forward direction
for (int i = 0; i < numLeds; i++) {
digitalWrite(leds[i], HIGH);
delay(200);
digitalWrite(leds[i], LOW);
}
// Backward direction
for (int i = numLeds - 2; i > 0; i--) {
digitalWrite(leds[i], HIGH);
delay(200);
digitalWrite(leds[i], LOW);
}
}
How It Works
-
The LED pins are stored in an array.
-
In the first
for
loop, LEDs turn on one by one from left to right, then turn off. -
In the second
for
loop, LEDs light up from right to left. -
The loops repeat endlessly, creating the back-and-forth “chaser” effect.
Common Mistakes & Quick Fixes
-
LED not glowing → Check polarity (long leg = anode).
-
All LEDs glow dimly → Missing resistors (always use 220Ω).
-
Wrong pin numbers → Verify array matches your wiring.
-
Too fast / too slow → Adjust
delay(200)
value.
Viva / Check-Your-Understanding
-
Why do we use resistors with LEDs?
-
What is the role of an array in this program?
-
How would the circuit change if we used pin 13 instead of pin 2?
-
Can we make LEDs glow without using
delay()
? (Hint:millis()
function)
Extensions (Mini-Tasks)
-
Add more LEDs (e.g., 8) for a longer chasing pattern.
-
Change delay dynamically using a potentiometer.
-
Make two LEDs chase each other in opposite directions.
-
Try random blinking instead of sequence.
Lab Record
-
Experiment Title
-
Objective
-
Circuit Diagram
-
Components Required
-
Program Code
-
Output
Comments
Post a Comment