Introduction
In this project, we will learn how to blink two LEDs using an Arduino. Blinking an LED is one of the simplest projects to get started with Arduino, and in this case, we are using two LEDs to make the project more interesting. We will be using pins 9 and 13 on the Arduino board to control the LEDs.
Components Required
To complete this project, you will need the following components:
1 x Arduino board (Uno, Mega, or any compatible board)
2 x LEDs (any color)
2 x Resistors (220Ω or 330Ω recommended)
Jumper wires
Breadboard (optional but recommended)
Circuit Connections
Follow these steps to connect your components:
Connect LED 1:
The longer leg (positive/anode) of LED 1 connects to pin 9 of the Arduino through one side of a 220Ω resistor.
The shorter leg (negative/cathode) of LED 1 connects to GND (ground) on the Arduino.
Connect LED 2:
The longer leg of LED 2 connects to pin 13 of the Arduino.
The shorter leg connects to GND.
Arduino Code Explanation
The following code will make the two LEDs blink alternately, meaning one LED will turn on while the other turns off, and then they will switch states every second.
int ledPin1 = 9; // LED 1 connected to pin 9
int ledPin2 = 13; // LED 2 connected to pin 13
void setup() {
pinMode(ledPin1, OUTPUT); // Set pin 9 as output
pinMode(ledPin2, OUTPUT); // Set pin 13 as output
}
void loop() {
digitalWrite(ledPin1, HIGH); // Turn on LED 1
digitalWrite(ledPin2, LOW); // Turn off LED 2
delay(1000); // Wait 1000ms (1 second)
digitalWrite(ledPin1, LOW); // Turn off LED 1
digitalWrite(ledPin2, HIGH); // Turn on LED 2
delay(1000); // Wait 1000ms (1 second)
}
How the Code Works
Define the LED Pins: We declare variables
ledPin1
andledPin2
to store the pin numbers.Setup Function:
pinMode(ledPin1, OUTPUT);
sets pin 9 as an output.pinMode(ledPin2, OUTPUT);
sets pin 13 as an output.
Loop Function:
digitalWrite(ledPin1, HIGH);
turns LED 1 ON.digitalWrite(ledPin2, LOW);
turns LED 2 OFF.delay(1000);
waits for 1 second.digitalWrite(ledPin1, LOW);
turns LED 1 OFF.digitalWrite(ledPin2, HIGH);
turns LED 2 ON.delay(1000);
waits for 1 second before repeating the process.
Expected Outcome
When you upload and run this code on your Arduino:
LED 1 will turn on while LED 2 is off for 1 second.
Then, LED 1 will turn off and LED 2 will turn on for another second.
This process will repeat continuously, creating an alternating blinking effect.
Troubleshooting Tips
Ensure that the LEDs are correctly connected with their longer leg (anode) to the Arduino pin and shorter leg (cathode) to the ground.
If the LEDs do not blink, check if the correct pin numbers are used in the code.
Make sure your resistors are properly placed to limit the current and protect the LEDs.
Conclusion
This project is a great way to understand how to use digital pins to control LEDs. You can modify this project by changing the delay time or adding more LEDs for a more complex blinking pattern. Have fun experimenting with Arduino!