Lesson 98-10: Arduino Traffic Light
In this lesson, we learn how to use an LED and push button in this 10-LED project. The wiring diagram is shown; the code is fully explained and demonstrated with multiple changes of settings for each project. We read the potentiometer's voltage and display the voltage by turning each LED on to represent the voltage. A simple traffic light project.
This code is part of a video lecture. This is project 10's code. We have three LEDs: red, green, and yellow.
We will set the time for each LED to be turned on. Of course, the timing can be changed, and in some countries, the sequence may vary; for example, yellow and green might be on at the same time for a short period. But this is a very simple, basic traffic light.
/*
* Arduino Step by Step Course by Robojax
* Lesson 98 https://robojax.com/course1/?vid=lecture98
* Project 10 of 10
*
* This code is a basic traffic light.
* We have three LEDs, Red, Green, and Yellow.
* We will set the time for each LED to be turned ON.
* Of course, the timing can be changed, and in some countries
* the sequence may vary; for example, yellow and green might be ON
* at the same time for a short period. But this is a very simple, basic
* traffic light.
*
* Watch full details and demonstration: https://youtu.be/OSwleCBlkuI
*
* Start a full Arduino Step By Step course: https://youtu.be/-6qSrDUA5a8
*
****** 10 Projects explained in this video
* 1-LED ON OFF
* 2-LED ON/OFF with push button
* 3-LED ON/OFF toggle with push button
* 4-Fading light of LED
* 5-Fading light of LED using push button
* 6-See Saw with LED
* 7-Walking Light
* 8-Walking light with push button
* 9-LED voltage level meter
* 10- Arduino Traffic Light
*
* What parts we need:
* -Of course, an Arduino board (UNO, Nano, Mega, or any)
* -RED, GREEN, and YELLOW LEDs, either 5mm or 3mm
* -200ohm to 1000 ohm resistors for each LED
* -Some Dupont wires to connect
* -If you don't want to solder, then use a breadboard
*
* written by Ahmad Shamshiri on Jan 30, 2022
* in Ajax, Ontario, Canada
* www.Robojax.com
If you found this tutorial helpful, please support me so I can continue creating
content like this. Make a donation using PayPal or a credit card: https://bit.ly/donate-robojax
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
const int greenLEDPin = 4;
const int yellowLEDPin = 3;
const int redLEDPin = 2;
const int greenONTime = 10000;
const int redONTime = 10000;
const int yellowONTime = 3000;
void setup() {
Serial.begin(9600);
Serial.println("Robojax Traffic Light");
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
}
void loop() {
digitalWrite(greenLEDPin, HIGH);
Serial.println("GREEN ON");
delay(greenONTime);
Serial.println("\tGREEN OFF");
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
Serial.println("\t\tYELLOW ON");
delay(yellowONTime);
Serial.println("\t\t\tYELLOW OFF");
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
Serial.println("\t\t\t\tRED ON");
delay(redONTime);
Serial.println("\t\t\t\t\tRED OFF");
digitalWrite(redLEDPin, LOW);
// Watch full details and demonstration: https://youtu.be/7xTHaFdCyaI
}// loop end
Resources & references
No resources yet.
Files📁
No files available.