X
Home > Blog > STEM for ESP32 > HOW to Simulate Traffic Light System with ESP32

HOW to Simulate Traffic Light System with ESP32

By r December 19th, 2024 188 views

1. Overview

Actually, we also control LEDs, but it follows the traffic rules. For example, when the green LED changes to red, the yellow LED flashes. In this project, we adopt the traffic light module to simulate traffic light system.

2. Wiring Diagram

b1

3. Test Code

/*  
  Project name: 4_Traffic_Lights
  Function: Traffic light module simulates traffic light control
  Author: keyestudio    
  Hardware connection:  
    - Connect to port 9 
  Library:  
    - none (no need to import additional library)  
  Cautions:  
    - Ensure connect to port 9   
    - Before uploading test code, please correctly connect to the development board and port
*/

#define RED_LED_PIN 17     //Connect the red LED to port IO17
#define YELLOW_LED_PIN 18  //Connect the yellow LED to port IO18
#define GREEN_LED_PIN 19   //Connect the green LED to port IO19

void setup() {
  // Initialize the digital port to output
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(YELLOW_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
}

void loop() {
  // green LED on
  digitalWrite(GREEN_LED_PIN, HIGH);
  // Delay 5000ms
  delay(5000);
  // green LED off
  digitalWrite(GREEN_LED_PIN, LOW);
  // yellow LED blinks for three times
  for (int i = 0; i < 3; i++) {
    digitalWrite(YELLOW_LED_PIN, HIGH);
    delay(500);
    digitalWrite(YELLOW_LED_PIN, LOW);
    delay(500);
  }
  // red LED on
  digitalWrite(RED_LED_PIN, HIGH);
  // Delay 5000ms
  delay(5000);
  //red LED off
  digitalWrite(RED_LED_PIN, LOW);
}

4. Test Result

After uploading the test code, the green LED lights up for 5 seconds and then the yellow LED flashes for three times. At last, the red LED turns on for 5 seconds. These action repeats.

d4

HOW to Make LEDs Like Flowing Water with ESP32
Previous
HOW to Make LEDs Like Flowing Water with ESP32
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us