How to make a Traffic Light?
1.Introduction
Traffic lights are closely related to people’s daily lives. Traffic lights generally show red, yellow, and green. Everyone should obey the traffic rules, which can avoid many traffic accidents. In this project, we will use a PLUS board and some LEDs (red, green and yellow) to simulate the traffic lights.
2.Components Required
|
|
|
|
---|---|---|---|
Keyestudio Plus Mainboard*1 |
Red LED*1 |
Yellow LED*1 |
Green LED*1 |
|
|
|
|
USB Cable*1 |
220Ω Resistor*3 |
Breadboard*1 |
Jumper Wires |
3.Circuit Diagram and Wiring Diagram
Note:
How to connect an LED
How to identify the 220Ω 5-band resistor
4.Code
The flashing time of each LED should be the same as the traffic lights. In this program, we use “Arduino delay ()” to control the delay time.
/*
Traffic_Light
http//www.keyestudio.com
*/
int redled =10; // initializes digital PIN 10
int yellowled =7; // initializes digital PIN 7
int greenled =4; // initializes digital PIN 4
void setup()
{
pinMode(redled, OUTPUT);// sets digital PIN 10 to “output”
pinMode(yellowled, OUTPUT); // sets digital PIN 7 to “output”
pinMode(greenled, OUTPUT); // sets digital PIN 4 to “output”
}
void loop()
{
digitalWrite(greenled, HIGH);// turns on LED
delay(5000);// delays 5 seconds
digitalWrite(greenled, LOW); // turns off LED
for(int i=0;i<3;i++)// flashes 3 times.
{
delay(500);// delays 0.5 second
digitalWrite(yellowled, HIGH);// turns on LED
delay(500);// delays 0.5 second
digitalWrite(yellowled, LOW);// turns off LED
}
delay(500);// delays 0.5 second
digitalWrite(redled, HIGH);// turns on LED
delay(5000);// delays 5 second
digitalWrite(redled, LOW);// turns off LED
}
5.Result
Upload the code and power on, the green LED will light up for 5s then go off. Next, the yellow one will blink for 3 times and red LED will be on for 5s then go off.