Arduino - Traffic Light | Arduino Tutorial (2024)

Ads by ArduinoGetStarted.com

Arduino - Traffic Light | Arduino Tutorial (1)

In this tutorial, we are going to learn how to use Arduino control the traffic light module. In detail, we will learn:

  • How to connect the traffic light module to Arduino

  • How to program Arduino to control RGB traffic light module

  • How to program Arduino to control RGB traffic light module without using delay() function

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Traffic Light Module
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units

Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Traffic Light Module

Pinout

A traffic light module includes 4 pins:

  • GND pin: The ground pin, connect this pin to GND of Arduino.

  • R pin: The pin to control the red light, connect this pin to a digital output of Arduino.

  • Y pin: The pin to control the yellow light, connect this pin to a digital output of Arduino.

  • G pin: The pin to control the green light, connect this pin to a digital output of Arduino.

Arduino - Traffic Light | Arduino Tutorial (2)

How It Works

Wiring Diagram

Arduino - Traffic Light | Arduino Tutorial (3)

This image is created using Fritzing. Click to enlarge image

How To Program For Traffic Light module

  • Configure an Arduino's pins to the digital output mode by using pinMode() function

pinMode(PIN_RED, OUTPUT);pinMode(PIN_YELLOW, OUTPUT);pinMode(PIN_GREEN, OUTPUT);

  • Program to turn ON red light by using digitalWrite() function:

digitalWrite(PIN_RED, HIGH); // turn on REDdigitalWrite(PIN_YELLOW, LOW); //digitalWrite(PIN_GREEN, LOW);delay(RED_TIME); // keep red led on during a period of time

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-traffic-light */#define PIN_RED 2 // The Arduino pin connected to R pin of traffic light module#define PIN_YELLOW 3 // The Arduino pin connected to Y pin of traffic light module#define PIN_GREEN 4 // The Arduino pin connected to G pin of traffic light module#define RED_TIME 4000 // RED time in millisecond#define YELLOW_TIME 4000 // YELLOW time in millisecond#define GREEN_TIME 4000 // GREEN time in millisecondvoid setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT);}// the loop function runs over and over again forevervoid loop() { // red light on digitalWrite(PIN_RED, HIGH); // turn on digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, LOW); // turn off delay(RED_TIME); // keep red light on during a period of time // yellow light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, HIGH); // turn on digitalWrite(PIN_GREEN, LOW); // turn off delay(YELLOW_TIME); // keep yellow light on during a period of time // green light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, HIGH); // turn on delay(GREEN_TIME); // keep green light on during a period of time}

Quick Steps

  • Copy the above code and open with Arduino IDE

  • Click Upload button on Arduino IDE to upload code to Arduino

  • Check out the traffic light module

Arduino - Traffic Light | Arduino Tutorial (4)

image source: diyables.io

It's important to note that the exact workings of a traffic light can vary depending on the specific design and technology used in different regions and intersections. The principles described above provide a general understanding of how traffic lights operate to manage traffic and enhance safety on the roads.

The code above demonstrates individual light control. Now, let's enhance the code for better optimization.

Arduino Code Optimization

  • Let's improve the code by implementing a function for light control.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-traffic-light */#define PIN_RED 2 // The Arduino pin connected to R pin of traffic light module#define PIN_YELLOW 3 // The Arduino pin connected to Y pin of traffic light module#define PIN_GREEN 4 // The Arduino pin connected to G pin of traffic light module#define RED_TIME 2000 // RED time in millisecond#define YELLOW_TIME 1000 // YELLOW time in millisecond#define GREEN_TIME 2000 // GREEN time in millisecond#define RED 0 // Index in array#define YELLOW 1 // Index in array#define GREEN 2 // Index in arrayconst int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN };const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME };void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT);}// the loop function runs over and over again forevervoid loop() { // red light on trafic_light_on(RED); delay(times[RED]); // keep red light on during a period of time // yellow light on trafic_light_on(YELLOW); delay(times[YELLOW]); // keep yellow light on during a period of time // green light on trafic_light_on(GREEN); delay(times[GREEN]); // keep green light on during a period of time}void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off }}

  • Let's improve the code by using a for loop.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-traffic-light */#define PIN_RED 2 // The Arduino pin connected to R pin of traffic light module#define PIN_YELLOW 3 // The Arduino pin connected to Y pin of traffic light module#define PIN_GREEN 4 // The Arduino pin connected to G pin of traffic light module#define RED_TIME 2000 // RED time in millisecond#define YELLOW_TIME 1000 // YELLOW time in millisecond#define GREEN_TIME 2000 // GREEN time in millisecond#define RED 0 // Index in array#define YELLOW 1 // Index in array#define GREEN 2 // Index in arrayconst int pins[] = {PIN_RED, PIN_YELLOW, PIN_GREEN};const int times[] = {RED_TIME, YELLOW_TIME, GREEN_TIME};void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT);}// the loop function runs over and over again forevervoid loop() { for (int light = RED; light <= GREEN; light ++) { trafic_light_on(light); delay(times[light]); // keep light on during a period of time }}void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i ++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off }}

  • Let's improve the code by using millis() function intead of delay().

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-traffic-light */#define PIN_RED 2 // The Arduino pin connected to R pin of traffic light module#define PIN_YELLOW 3 // The Arduino pin connected to Y pin of traffic light module#define PIN_GREEN 4 // The Arduino pin connected to G pin of traffic light module#define RED_TIME 2000 // RED time in millisecond#define YELLOW_TIME 1000 // YELLOW time in millisecond#define GREEN_TIME 2000 // GREEN time in millisecond#define RED 0 // Index in array#define YELLOW 1 // Index in array#define GREEN 2 // Index in arrayconst int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN };const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME };unsigned long last_time = 0;int light = RED; // start with RED lightvoid setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); trafic_light_on(light); last_time = millis();}// the loop function runs over and over again forevervoid loop() { if ((millis() - last_time) > times[light]) { light++; if (light >= 3) light = RED; // new circle trafic_light_on(light); last_time = millis(); } // TO DO: your other code}void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off }}

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

  • pinMode()

  • digitalWrite()

  • delay()

  • millis()

The Best Arduino Starter Kit

  • See the best Arduino kit for beginner

See Also

  • Arduino - LED - Blink

  • Arduino - LED - Blink Without Delay

  • Arduino - LED - Fade

  • Arduino - RGB LED

  • Arduino - Button - LED

  • Arduino - Button Toggle LED

  • Arduino - Potentiometer fade LED

  • Arduino - Potentiometer Triggers LED

  • Arduino - Light Sensor Triggers LED

  • Arduino - Ultrasonic Sensor - LED

  • Arduino - Motion Sensor - LED

  • Arduino - Touch Sensor - LED

  • Arduino - Touch Sensor Toggle LED

  • Arduino - Door Sensor - LED

  • Arduino - Door Sensor Toggle LED

  • Arduino - Rain Sensor - LED

  • Arduino - Sound Sensor - LED

  • Arduino - LED Strip

  • Arduino - NeoPixel LED Strip

  • Arduino - WS2812B LED Strip

  • Arduino - Dotstar Led Strip

  • Arduino controls LED via Bluetooth

  • Arduino Uno R4 WiFi controls LED via Web

※ OUR MESSAGES

  • We are AVAILABLE for HIRE. See how to hire us to build your project

  • If this tutorial is useful for you, please give us motivation to make more tutorials.

  • You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!

PREVIOUS

NEXT

DISCLOSURE

ArduinoGetStarted.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com, Amazon.it, Amazon.fr, Amazon.co.uk, Amazon.ca, Amazon.de, Amazon.es, Amazon.nl, Amazon.pl and Amazon.se

Copyright © 2018 - 2024 ArduinoGetStarted.com. All rights reserved.
Terms and Conditions | Privacy Policy

Email: ArduinoGetStarted@gmail.com

Arduino - Traffic Light | Arduino Tutorial (2024)

FAQs

What is the algorithm used for traffic lights? ›

Traffic signal control algorithms of a traffic network are presented using a hierarchical control concept. The traffic congestion mechanism is described quantitatively based on the traffic volume balance at each signalized intersection.

How to make a simple traffic light? ›

Traffic Light Project
  1. Step 1: Take All Components As Shown Below. ...
  2. Step 2: Fold Pins of Transistors Like This. ...
  3. Step 3: Connect Red LED to Transistor. ...
  4. Step 4: Connect Green LED to Transistor. ...
  5. Step 5: Connect Yellow LED to Transistor. ...
  6. Step 6: Connect Emmiter of Transistors. ...
  7. Step 7: Connect 1K Resistors.

What are traffic lights programmed in? ›

Traffic light systems are designed using software such as LINSIG, TRANSYT, CORSIM/TRANSYT-7F or VISSIM.

Do traffic lights use AI? ›

Smart traffic lights or Intelligent traffic lights are a vehicle traffic control system that combines traditional traffic lights with an array of sensors and artificial intelligence to intelligently route vehicle and pedestrian traffic.

Are traffic lights programmable? ›

At signalized intersections without separate pedestrian signal heads, the traffic signals may be programmed to turn red in all directions, followed by a steady display of amber lights simultaneously with the red indications.

How do traffic lights use PLC? ›

The PLC checks the status of the sensors. The system resolution is depend on the output provided by the sensors, Then PLC checks the priorities and then provide output signal to the traffic lights poles for ON or OFF the Red, yellow or Green lights and ON time is depend on the specific priorities.

How to make a traffic light CIrcuit in Tinkercad? ›

Drag in two more LEDs, set one to be yellow and one to be green. Match the order of a traffic light by placing the yellow light below the red one, then the green light at the bottom. Like the red LED, we need to run part of our circuit through a resistor to prevent our LED from failing under too much current.

How many LEDs are used in a 7-segment display? ›

A 7-segment display is a form of electronic display device that consists of seven LEDs arranged in a rectangular fashion. Each LED is called a segment that maps to one of the terminals A through G.

How does a traffic light circuit work? ›

The primary, reliable and most common traffic light sensors are induction loops. Induction loops are coils of wire that have been embedded in the surface of the road to detect changes in inductance, then conveying them to the sensor circuitry in order to produce signals.

How to make a traffic model? ›

Muhammad Bhatti
  1. Data Collection: Traffic Counts: Measuring the number of vehicles passing through intersections, highways, or other points over a set period. ...
  2. Development of the Base Model: ...
  3. Scenario Modelling: ...
  4. Calibration and Validation: ...
  5. Analysis & Recommendations:
Sep 10, 2023

How does the traffic light controller work with Arduino? ›

Once the density is calculated, the glowing time of green light is assigned by the help of the microcontroller (Arduino), which will in-turn determine how long a flank will be open and when to change over the signal lights. Hence, reducing traffic congestion without any manual intervention.

How to make a traffic light in Tinkercad? ›

Drag in two more LEDs, set one to be yellow and one to be green. Match the order of a traffic light by placing the yellow light below the red one, then the green light at the bottom. Like the red LED, we need to run part of our circuit through a resistor to prevent our LED from failing under too much current.

Can you control lights with Arduino? ›

By simply attaching a relay module and an IoT enabled Arduino board you can easily control the lights in your home. Turn them on remotely to throw burglars off or set a schedule to turn them on or off automatically.

References

Top Articles
17 Phenomenal Things To Do in Fort Walton Beach
Of Blood and Bonds - Assassin's Creed Valhalla Guide - IGN
Happel Real Estate
The Ultimate Guide To Jelly Bean Brain Leaks: Causes, Symptoms, And Solutions
Nizhoni Massage Gun
Equipment Hypixel Skyblock
Champion Enchant Skyblock
Dr Paul Memorial Medical Center
What Was D-Day Weegy
Biz Buzz Inquirer
Busted Newspaper Randolph County
American Airlines Companion Certificate Blackout Dates 2023
University Of Toledo Email
Thomas the Tank Engine
Craigslist Hutchinson Ks
Babylon Alligator
T33N Leaks 5 17
Violent Night Showtimes Near The Grand 16 - Lafayette
Pixel Speedrun Unblocked Games 76
Dyi Urban Dictionary
Uca Cheerleading Nationals 2023
Amanda Balionis makes announcement as Erica Stoll strides fairways with Rory McIlroy
Hdmovie 2
Journal articles: 'Mark P. Herschede Trust' – Grafiati
Irish DNA | Irish Origenes: Use your DNA to rediscover your Irish origin
Mexi Unblocked Games
Point After Salon
New Homes in Waterleigh | Winter Garden, FL | D.R. Horton
Pwc Transparency Report
Nike Factory Store - Howell Photos
Provo Craigslist
Why Zero Raised to the Zero Power is defined to be One « Mathematical Science & Technologies
Liveops Nation Okta Com Sign In
Nikki Porsche Girl Head
Mo Craiglist
Fanart Tv
Rainfall Map Oklahoma
Shs Games 1V1 Lol
Owen Roeder Tim Dillon
Famous Church Sermons
Riscap Attorney Registration
Craigslist Free Stuff Columbus Ga
Eurorack Cases & Skiffs
600 Aviator Court Vandalia Oh 45377
Ultipro Fleet Farm
Busty Young Cheerleaders
Six Broadway Wiki
Where To Find Mega Ring In Pokemon Radical Red
Katia Uriarte Husband
Sam Smith Lpsg
The Ultimate Guide to Newquay Surf - Surf Atlas
Never Would Have Made It Movie 123Movies
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 6184

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.