ESP8266 - Traffic Light | ESP8266 Tutorial (2024)

This tutorial instructs you how to use an ESP8266 to control a traffic light module. Specifically, we will cover the following aspects:

  • Establishing the connection between the traffic light module and ESP8266

  • Programming ESP8266 to oversee the RGB traffic light module

  • Implementing ESP8266 programming to manage the RGB traffic light module without relying on the delay() function

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Traffic Light Module
1×Jumper Wires
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units

Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Traffic Light Module

Pinout

A traffic light module is equipped with four pins:

  • GND pin: Connect this pin to the GND of the ESP8266.

  • R pin: Manages the red light; connect this pin to a digital output of the ESP8266.

  • Y pin: Controls the yellow light; connect this pin to a digital output of the ESP8266.

  • G pin: Governs the green light; connect this pin to a digital output of the ESP8266.

ESP8266 - Traffic Light | ESP8266 Tutorial (1)

How It Works

Wiring Diagram

ESP8266 - Traffic Light | ESP8266 Tutorial (2)

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For Traffic Light module

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

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

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

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-traffic-light */#define PIN_RED D7 // The ESP8266 pin connected to R pin of traffic light module#define PIN_YELLOW D6 // The ESP8266 pin connected to Y pin of traffic light module#define PIN_GREEN D5 // The ESP8266 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 repeats indefinitelyvoid 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}

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.

  • Wire the components as shown in the diagram.

  • Connect the ESP8266 board to your computer using a USB cable.

  • Open Arduino IDE on your computer.

  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.

  • Copy the above code and open with Arduino IDE

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

  • 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.

ESP8266 Code Optimization

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

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-traffic-light */#define PIN_RED D7 // The ESP8266 pin connected to R pin of traffic light module#define PIN_YELLOW D6 // The ESP8266 pin connected to Y pin of traffic light module#define PIN_GREEN D5 // The ESP8266 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 repeats indefinitelyvoid 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 ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-traffic-light */#define PIN_RED D7 // The ESP8266 pin connected to R pin of traffic light module#define PIN_YELLOW D6 // The ESP8266 pin connected to Y pin of traffic light module#define PIN_GREEN D5 // The ESP8266 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 repeats indefinitelyvoid 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 ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-traffic-light */#define PIN_RED D7 // The ESP8266 pin connected to R pin of traffic light module#define PIN_YELLOW D6 // The ESP8266 pin connected to Y pin of traffic light module#define PIN_GREEN D5 // The ESP8266 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 repeats indefinitelyvoid 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

Learn More

  • ESP8266 - LED

  • ESP8266 - LED - Blink Without Delay

  • ESP8266 - Blink multiple LED

  • ESP8266 - LED - Fade

  • ESP8266 - LED RGB

  • ESP8266 - Button - LED

  • ESP8266 - Potentiometer fade LED

  • ESP8266 - Potentiometer LED

  • ESP8266 - Light Sensor LED

  • ESP8266 - Ultrasonic Sensor - LED

  • ESP8266 - Motion Sensor - LED

  • ESP8266 - Touch Sensor - LED

  • ESP8266 - Door Sensor - LED

  • ESP8266 - LED Strip

  • ESP8266 - NeoPixel LED Strip

  • ESP8266 - WS2812B LED Strip

  • ESP8266 - DotStar LED Strip

  • ESP8266 Control LED via Bluetooth

  • ESP8266 - Controls LED via Web

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!

PREVIOUS

NEXT

DISCLOSURE

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

Email: newbiely.com@gmail.com

ESP8266 - Traffic Light | ESP8266 Tutorial (2024)
Top Articles
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6235

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.