To make 3 LEDs blink like traffic lights (Red, Yellow, Green), you’ll need to follow a simple sequence of turning each LED on and off in a timed pattern. Below is an example of how to implement this using a basic microcontroller (such as an Arduino), assuming you have three LEDs connected to three pins on the microcontroller.
Components Needed:
- 3 LEDs (Red, Yellow, Green)
- 3 resistors (typically 220Ω to 330Ω for each LED)
- Arduino or any microcontroller
- Jumper wires and breadboard
Circuit Setup:
- Connect each LED’s anode (long leg) to the respective pin on the microcontroller (e.g., pin 2 for Red, pin 3 for Yellow, pin 4 for Green).
- Connect the cathode (short leg) of each LED to the ground (GND) of the microcontroller through a current-limiting resistor.
- Ensure that the pins are set as output in your code.
Arduino Code:
// Define the LED pins
int redPin = 2;
int yellowPin = 3;
int greenPin = 4;
void setup() {
// Set the LED pins as output
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
// Red LED ON, Yellow and Green OFF
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
delay(1000); // Wait for 1 second
// Yellow LED ON, Red and Green OFF
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
delay(1000); // Wait for 1 second
// Green LED ON, Red and Yellow OFF
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
delay(1000); // Wait for 1 second
}
How it works:
- The code sets each LED pin as an output.
- In the
loop()
function, the LEDs are turned on one at a time to simulate the traffic light sequence:- Red LED stays on for 1 second.
- Yellow LED stays on for 1 second (this mimics the “warning” light).
- Green LED stays on for 1 second.
- This sequence repeats indefinitely.
Traffic Light Timing:
- Red Light: 1 second
- Yellow Light: 1 second
- Green Light: 1 second
You can adjust the timing (using the delay()
function) depending on how fast or slow you want the traffic lights to change.
This simple code creates a basic traffic light pattern for the LEDs, simulating the transitions of red, yellow, and green lights.