Arduino - Traffic Light | Arduino Tutorial (2024)

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 (1)

How It Works

Wiring Diagram

Arduino - Traffic Light | Arduino Tutorial (2)

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

Arduino - Traffic Light | Arduino Tutorial (3)

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

How to make traffic light with LED? ›

Hookup Hook the GND pin (Negative Pin) of all led to Pin GND of Arduino. Connect Red LED VCC Pin (Positive Pin) to Pin 9 of Arduino. Connect Yellow LED VCC Pin (Positive Pin) to Pin 8 of Arduino. Connect Green LED VCC Pin (Positive Pin) to Pin 7 of Arduino.

Which IC is used in traffic signal? ›

We construct this circuit with the help of well-known IC i.e. NE555. Control signal in this circuit is mainly three LED i.e. red, green and yellow. This circuit is low cost circuit as it used minimum amount of components. Also assembling of the circuit is too easy to do.

What are traffic lights programmed in? ›

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

How many lights can an Arduino control? ›

There are only 6 ports with PWM, so that sets the limit for that. If you need only digital control you can also make a matrix of LEDs where 4 ports drive and 4 ports sink, so you can control 16 LEDs individually, by multiplexing.

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.

Can you connect an LED directly to an Arduino? ›

If you have a 5V supply the Arduino and LED strip can share the same power supply. That's the most common way to do it. Or, if you are powering via USB you can get about 1 Amp (1000mA) out from the 5V pin. But if you have a 12V power supply, you can't power the LED strip through the Arduino's 5V voltage regulator.

Can I connect LED to Arduino without resistor? ›

Arduino circuits will typically use LEDs with series resistors. This is because Arduino provides 3.3v or 5V voltage sources. These sources don't match the 1.5V (lets say) forward voltage or your LED. You must 'drop' the voltage across the series resistor.

What language does Arduino use? ›

Arduino uses a variant of the C++ programming language. The code is written in C++ with an addition of special methods and functions. Moreover, when you create a 'sketch' (the name given to code files in this language), it is processed and compiled to machine language.

What is Arduino traffic light simulator? ›

Arduino Traffic Light Circuit

It's a fairly simple setup with each pin controlling an LED. Pin 2 goes to the positive leg of the green LED. Pin 3 Goes to the positive leg of the yellow LED. Pin 4 goes to the positive leg of the red LED. Add a 100-ohm resistor to each of the negative LED legs and have it go to GND.

How do you do the traffic light experiment? ›

mix some glucose, sodium hydroxide and indigo carmine solutions together in a flask and get a nice deep yellow solution. Mix it up once and it will turn to red. Shake it up again and it turns green! Then simply let the flask settle and it will revert back to red and finally rest at yellow again.

References

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6462

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.