ESP32 - Traffic Light | ESP32 Tutorial (2024)

Ads by esp32io.com

ESP32 - Traffic Light | ESP32 Tutorial (1)

In this tutorial, we will explore how to utilize the ESP32 to control a traffic light module. In detail, we will learn:

  • How to connect the traffic light module to ESP32

  • How to program ESP32 to control RGB traffic light module

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

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Traffic Light Module
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units

Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Traffic Light Module

Pinout

A traffic light module includes 4 pins:

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

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

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

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

ESP32 - Traffic Light | ESP32 Tutorial (2)

How It Works

Wiring Diagram

ESP32 - Traffic Light | ESP32 Tutorial (3)

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

  • With breadboard

ESP32 - Traffic Light | ESP32 Tutorial (4)

This image is created using Fritzing. Click to enlarge image

How To Program For Traffic Light module

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

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

ESP32 Code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-traffic-light */#define PIN_RED 25 // The ESP32 pin GPIO25 connected to R pin of traffic light module#define PIN_YELLOW 26 // The ESP32 pin GPIO26 connected to Y pin of traffic light module#define PIN_GREEN 27 // The ESP32 pin GPIO27 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 Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.

  • Do the wiring as above image.

  • Connect the ESP32 board to your PC via a micro USB cable

  • Open Arduino IDE on your PC.

  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.

  • Copy the above code and open with Arduino IDE

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

  • Check out the traffic light module

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.

ESP32 Code Optimization

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

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-traffic-light */#define PIN_RED 25 // The ESP32 pin GPIO25 connected to R pin of traffic light module#define PIN_YELLOW 26 // The ESP32 pin GPIO26 connected to Y pin of traffic light module#define PIN_GREEN 27 // The ESP32 pin GPIO27 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.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-traffic-light */#define PIN_RED 25 // The ESP32 pin GPIO25 connected to R pin of traffic light module#define PIN_YELLOW 26 // The ESP32 pin GPIO26 connected to Y pin of traffic light module#define PIN_GREEN 27 // The ESP32 pin GPIO27 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().

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-traffic-light */#define PIN_RED 25 // The ESP32 pin GPIO25 connected to R pin of traffic light module#define PIN_YELLOW 26 // The ESP32 pin GPIO26 connected to Y pin of traffic light module#define PIN_GREEN 27 // The ESP32 pin GPIO27 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

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

Learn More

  • ESP32 - LED - Blink

  • ESP32 - LED - Blink Without Delay

  • ESP32 - Blink multiple LED

  • ESP32 - LED - Fade

  • ESP32 - RGB LED

  • ESP32 - Button - LED

  • ESP32 - Button Toggle LED

  • ESP32 - LED Matrix

  • ESP32 - Potentiometer fade LED

  • ESP32 - Potentiometer Triggers LED

  • ESP32 - Ultrasonic Sensor - LED

  • ESP32 - Light Sensor Triggers LED

  • ESP32 - Motion Sensor - LED

  • ESP32 - LED Strip

  • ESP32 - NeoPixel LED Strip

  • ESP32 - WS2812B LED Strip

  • ESP32 - Dotstar LED Strip

  • ESP32 - Door Sensor - LED

  • ESP32 - Door Sensor Toggle LED

  • ESP32 - Touch Sensor - LED

  • ESP32 - Touch Sensor Toggle LED

  • ESP32 - Rain Sensor - LED

  • ESP32 - Sound Sensor - LED

  • ESP32 - Controls LED via Web

※ OUR MESSAGES

PREVIOUS

NEXT

DISCLOSURE

ESP32IO.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 ESP32IO.com. All rights reserved.
Terms and Conditions | Privacy Policy

Email: ArduinoGetStarted@gmail.com

ESP32 - Traffic Light | ESP32 Tutorial (2024)

FAQs

What is the traffic light control system using ESP32? ›

The ESP32 is the central processing unit that manages all components, including traffic lights and ultrasonic sensors. It is connected to a power source, Wi-Fi network, and other peripherals for complete control. Each signal has three LEDs representing the Red, Yellow, and Green lights which are connected as follows.

How does the traffic light control system work? ›

Under the road, an inductive coil detects when there is a change in the magnetic field, such as when vehicles stop above it. Sensors embedded in the signal head work similarly, except they utilize lasers or cameras to detect vehicles.

Which sensor is used in traffic light controller? ›

Traffic Sensors (Doppler type)

These sensors use the ultrasonic Doppler effect. They detect vehicles travelling in a particular direction using a change in frequency (the Doppler effect) according to the speed of the vehicle.

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

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 to control traffic lights? ›

Fixed time control

Aside from movable parts, electrical relays are also used. In general, electro-mechanical signal controllers use dial timers that have fixed, signalized intersection time plans. Cycle lengths of signalized intersections are determined by small gears that are located within dial timers.

How does a 4 digit 7-segment display work? ›

The 4-digtal 7-segment display works independently. It uses the principle of human visual persistence to quickly display the characters of each 7-segment in a loop to form continuous strings. For example, when “1234” is displayed on the display, “1” is displayed on the first 7-segment, and “234” is not displayed.

How to make a traffic light in Python? ›

Logic:
  1. Define the function trafficLight().
  2. Prompt the user to enter the color of the traffic light.
  3. If the input is not “RED”, “YELLOW”, or “GREEN”, display an error message.
  4. If the input is valid, call the function light() with the input as the argument.

What are the components required for traffic light controller system? ›

Generally, they include a main controller, control circuit, timer, clock signal generator, decoder, decoder drive circuit and digital display decoder drive circuit.

What is the voltage of a traffic signal? ›

Typically traffic signals are operated at 120v. Even with the conversion to LED lights. The operating voltage is still the same, just stepped down to 12–24 volts at the device. There's is a (somewhat) new trend beginning where 48 volt DC is being used as the primary voltage.

How are traffic light sensors triggered? ›

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.

What is traffic light controller using microcontroller? ›

The microcontroller (AT89C51) is used as the main controlling element to automatically control the traffic lights by turning the LED indicators on and off according to a programmed schedule. It provides centralized control of traffic signals at an intersection to help traffic flow more smoothly and safely.

What is smart traffic light control system? ›

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. They can form part of a bigger intelligent transport system.

What is traffic light module? ›

The traffic light module is a small device that can display red, yellow and green lights, just like a real traffic light. It can be used to make a traffic light system model or to learn how to control LEDs with Arduino. It is featured with its small size, simple wiring, targeted, and custom installation.

What is traffic light system using FSM? ›

Traffic Lights

If, at the end of this interval there is a WALK request pending, the system goes to the Main yellow/side red for TYEL and then to WALK (all red and yellow lights on) for TEXT which we will take to be the length of the WALK interval. At the end of this the system goes to side green.

References

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6274

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.