Traffic light using Arduino Uno
Here's a simple guide to create a traffic light project using Arduino:
Components Needed
- Arduino Board (e.g., Arduino Uno)
- 3 LEDs (Red, Yellow, Green)
- 3 Resistors (220-ohm or suitable for your LEDs)
- Breadboard and jumper wires
Circuit Setup
1. Connect the LEDs to digital pins on the Arduino:
- Red LED to pin 9
- Yellow LED to pin 10
- Green LED to pin 11
2. Connect each LED's cathode (short leg) to a resistor, and then connect the other end of the resistor to a GND pin on the Arduino.
Code
const int redPin = 9;
const int yellowPin = 10;
const int greenPin = 11;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
// Red light
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
delay(5000); // Wait 5 seconds
// Green light
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
delay(5000); // Wait 5 seconds
// Yellow light
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
delay(2000); // Wait 2 seconds
}
How it Works
1. The `setup()` function sets the LED pins as outputs.
2. The `loop()` function simulates a traffic light sequence:
- Red light for 5 seconds
- Green light for 5 seconds
- Yellow light for 2 seconds
Upload and Enjoy!
Upload the code to your Arduino board, and the traffic light sequence should start! 🚦
Do you have any questions about this project or would you like to add more features (e.g., pedestrian signals, timer displays)?