Introduction:
Arduino LED Blinking Patterns is one of the fundamental projects in the world of Arduino. It’s a simple yet effective way to understand the basics of digital output and timing control. In this blog post, we will explore how to create a captivating LED blinking sequence using the Arduino platform. We will walk through the code explain its functionality, and provide step-by-step instructions on how to recreate this project.
Hardware Requirements:
To follow along with this project, you will need the following components:
1 | Arduino board (e.g., Arduino Uno) |
2 | 12 LEDs |
3 | Jumper wires |
4 | Resistor (220 ohms) |
5 | Breadboard |
Setting Up the Circuit:
1 | Start by connecting the Arduino board to your computer via a USB cable. |
2 | Place the Arduino on the breadboard, ensuring that the USB port is facing outwards. |
3 | Connect the ground (GND) pin on the Arduino to the negative (-) rail of the breadboard. |
4 | Insert the LEDs into the breadboard, arranging them in a row. |
5 | Connect the longer leg (anode) of each LED to digital pins 2 to 13 on the Arduino, using individual jumper wires. |
6 | Connect the shorter leg (cathode) of each LED to the resistor (220 ohms), and then connect the other end of the resistor to the negative (-) rail of the breadboard. |
7 | Finally, connect the positive (+) rail of the breadboard to the 5V pin on the Arduino. |
Writing the Code:
int del = 100; // defining delay value (0.1 second)
void setup() {
pinMode(2, OUTPUT); // defining pins (2-13) as Outputs
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
// First loop to turn ON and OFF every single pin from 2 to 13
for (int i = 2; i <= 13; i++) {
digitalWrite(i, HIGH); // Turning ON current pin (LED)
delay(del); // LED is ON for the specified delay
digitalWrite(i, LOW); // Turning OFF current pin (LED)
delay(del); // Waiting before running the next iteration
}
// Second loop to turn ON and OFF every single pin from 13 to 2
for (int i = 13; i >= 2; i--) {
digitalWrite(i, HIGH); // Turning ON current pin (LED)
delay(del); // LED is ON for the specified delay
digitalWrite(i, LOW); // Turning OFF current pin (LED)
delay(del); // Waiting before running the next iteration
}
}
Feel free to try out these patterns with your setup of 13 LEDs connected to digital pins 2 to 13 on the Arduino board. Have fun experimenting and enjoy the mesmerizing LED running effects!
Forward Running Lights
int delayTime = 100; // Delay between LED changes (100 milliseconds)
void setup() {
for (int i = 2; i <= 13; i++) {
pinMode(i, OUTPUT); // Set digital pins 2-13 as outputs
}
}
void loop() {
for (int i = 2; i <= 13; i++) {
digitalWrite(i, HIGH); // Turn on current LED
delay(delayTime); // Delay for the specified time
digitalWrite(i, LOW); // Turn off current LED
}
}
Backward Running Lights
int delayTime = 100; // Delay between LED changes (100 milliseconds)
void setup() {
for (int i = 2; i <= 13; i++) {
pinMode(i, OUTPUT); // Set digital pins 2-13 as outputs
}
}
void loop() {
for (int i = 13; i >= 2; i--) {
digitalWrite(i, HIGH); // Turn on current LED
delay(delayTime); // Delay for the specified time
digitalWrite(i, LOW); // Turn off current LED
}
}
Running Lights with Bounce Effect
int delayTime = 100; // Delay between LED changes (100 milliseconds)
void setup() {
for (int i = 2; i <= 13; i++) {
pinMode(i, OUTPUT); // Set digital pins 2-13 as outputs
}
}
void loop() {
for (int i = 2; i <= 13; i++) {
digitalWrite(i, HIGH); // Turn on current LED
delay(delayTime); // Delay for the specified time
digitalWrite(i, LOW); // Turn off current LED
}
for (int i = 12; i >= 3; i--) {
digitalWrite(i, HIGH); // Turn on current LED
delay(delayTime); // Delay for the specified time
digitalWrite(i, LOW); // Turn off current LED
}
}
Understanding the Code:
The code begins by defining a delay value (del) of 100 milliseconds (0.1 second). This value determines how long each LED will remain on before being turned off.
In the setup() function, we set the digital pins 2 to 13 as outputs, enabling us to control the LEDs connected to those pins.
The loop() function is where the LED blinking sequence takes place. The code uses two for loops to iterate through the pins and turn the LEDs on and off.
The first for loop starts at pin 2 and goes up to pin 13. For each
Uploading the Code:
Before uploading the code to your Arduino board make sure it is properly connected to your computer via the USB cable (Not detected Arduino ?). Also, double-check the code for any errors or typos. Once you’re ready click the “Upload Button” (Not Uploading ?) in the Arduino IDE. The IDE will compile the code and upload it to the Arduino board. You should see the LED start blinking according to the specified pattern.
Arduino board is not being detected by your computer ?
- Check USB: Secure connections, try different cable/port.
- Restart computer: Resolve issues with a simple restart.
- Install Arduino drivers: Get specific drivers from official website. (How to install Arduino Drivers?)
- Select correct board/port in IDE: Tools > Board > Port. Disconnect/reconnect for new port.
- Try different computer/port: Test on another system to isolate the problem.
- Check for faulty components: Examine board, connections, try another Arduino board if available.
To connect your Arduino board to your computer, follow these steps:
Select the correct board: In the Arduino IDE click on the “Tools” menu. From the “Board” submenu, select the appropriate Arduino board model you are using (e.g., Arduino Uno, Arduino Nano, etc.).
Select the port: In the same “Tools” menu navigate to the “Port” submenu. Here, you should see a list of available ports. Look for the port that corresponds to your Arduino board. If you’re unsure which port to select, you can disconnect the Arduino board, check the available ports, reconnect the board, and see which new port appears. Select that port.