This project demonstrates how to control a sequence of LEDs using an Arduino. The LEDs will light up in a pattern, first from the lowest pin to the highest and then back from the highest pin to the lowest.
- Arduino board (e.g., Uno, Nano)
- 6 LEDs
- 6 resistors (220 ohms each)
- Breadboard
- Jumper wires
- Connect each LED to one of the following digital pins: 2, 3, 4, 5, 6, and 7 on the Arduino.
- Connect the longer leg (anode) of each LED to the corresponding pin.
- Connect the shorter leg (cathode) of each LED to one end of a 220-ohm resistor.
- Connect the other end of each resistor to the GND (ground) pin on the Arduino.
int timer = 100;
: This variable controls the delay time between LED transitions. A higher value results in slower LED sequences.int ledPins[] = {2, 7, 4, 6, 5, 3};
: This array stores the pin numbers where each LED is connected.int pinCount = 6;
: This variable holds the number of pins (i.e., the number of LEDs).
void setup()
: Thesetup()
function runs once when the program starts. Inside this function:- A
for
loop is used to iterate through each pin in theledPins
array, setting each as an output usingpinMode()
.
- A
void loop()
: Theloop()
function runs continuously aftersetup()
. Inside this function:- First Loop:
- A
for
loop iterates from the lowest to the highest pin in theledPins
array. digitalWrite(ledPins[thisPin], HIGH);
: This turns on the current LED.delay(timer);
: This pauses the program for the duration specified by thetimer
variable.digitalWrite(ledPins[thisPin], LOW);
: This turns off the current LED.
- A
- Second Loop:
- Another
for
loop iterates from the highest to the lowest pin in theledPins
array, reversing the LED sequence. - The same steps as in the first loop are repeated to light up and turn off the LEDs in reverse order.
- Another
- First Loop:
- Connect your Arduino board to your computer.
- Open the Arduino IDE.
- Copy and paste the provided code into a new sketch.
- Upload the sketch to your Arduino board.
- Watch the LEDs light up sequentially from one end to the other and then back again.
- Adjust the
timer
variable to speed up or slow down the LED sequence. - Modify the
ledPins
array to change the order or number of LEDs in the sequence.
This project is open-source and available under the MIT License.
Enjoy creating dynamic LED sequences with Arduino!