Arduino Projects
Traffic LIght simulator
Introduction:
In this project, we'll create a traffic light simulation using an Arduino. This project mimics the typical traffic light sequence of red, yellow, and green lights.
What Does the Circuit Do?
This circuit controls three LEDs to simulate a traffic light:
Red LED: This represents the "stop" signal and lights up for 2 seconds.
Yellow LED: This represents the "prepare to stop" signal and lights up for 1 second.
Green LED: This represents the "go" signal and lights up for 2 seconds.
The sequence repeats, creating a mini traffic light that you can observe.
Getting Started:
If you're new to Arduino programming and want to learn how to use the Arduino IDE, I recommend starting with the "How to Blink an LED" tutorial. This tutorial will teach you the basics of uploading code to an Arduino and controlling an LED. How to Blink an LED Tutorial
Circuit Diagram:
You can visualize and interact with this project on Tinkercad by following this link: Traffic Light Simulation on Tinkercad.
const int redPin = 7; // Pin for the red LEDconst int yellowPin = 8; // Pin for the yellow LEDconst int greenPin = 9; // Pin for the green LED
void setup() { // Configure the specified pins as OUTPUTs to control the LEDs pinMode(redPin, OUTPUT); pinMode(yellowPin, OUTPUT); pinMode(greenPin, OUTPUT);}
void loop() { // Red light digitalWrite(redPin, HIGH); // Turn on the red LED digitalWrite(yellowPin, LOW); // Turn off the yellow LED digitalWrite(greenPin, LOW); // Turn off the green LED delay(2000); // Red light for 2 seconds
// Yellow light digitalWrite(redPin, LOW); // Turn off the red LED digitalWrite(yellowPin, HIGH); // Turn on the yellow LED digitalWrite(greenPin, LOW); // Turn off the green LED delay(1000); // Yellow light for 1 second
// Green light digitalWrite(redPin, LOW); // Turn off the red LED digitalWrite(yellowPin, LOW); // Turn off the yellow LED digitalWrite(greenPin, HIGH); // Turn on the green LED delay(2000); // Green light for 2 seconds}
break down Of the code for the traffic light simulation
1. Constants and Pin Definitions:
const int redPin = 7;
const int yellowPin = 8;
const int greenPin = 9;
- These lines define three constants named `redPin`, `yellowPin`, and `greenPin` and set them to the pin numbers where we have connected the three LEDs on the Arduino board.
2. `setup()` Function:
void setup() {
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
- In the `setup()` function, we configure the pins connected to the LEDs as OUTPUT pins. This tells the Arduino that we want to control these pins to send electrical signals to the LEDs.
3. `loop()` Function:
void loop() {
// Red light
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
delay(2000); // Red light for 2 seconds
// Yellow light
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
delay(1000); // Yellow light for 1 second
// Green light
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
delay(2000); // Green light for 2 seconds
}
- The `loop()` function contains a sequence of instructions that are executed in a loop, over and over again.
- In the first part, it turns on the red LED by making the `redPin` HIGH and turning off the other two LEDs by making their pins LOW. Then, it waits for 2 seconds using the `delay()` function, creating a 2-second red light.
- Next, it switches to the yellow light. It turns off the red LED, turns on the yellow LED, and waits for 1 second. This represents the 1-second yellow light.
- Finally, it activates the green light. It turns off the yellow and red LEDs, turns on the green LED, and waits for 2 seconds. This creates a 2-second green light.
After these steps, it goes back to the red light and continues the loop.
Donate
If you've enjoyed exploring my Arduino projects and want to see more amazing creations, your support can make a big difference! By contributing, you're helping me continue to innovate and bring even more exciting projects to life. Together, we can explore the endless possibilities of DIY electronics! Don't forget to like, subscribe, and follow for updates on the latest developments. Thank you for being a part of this journey!
Click here to make a difference with your donation today!