3.17 Traffic Light

3.17.1 Overview

The traffic light module limits the pedestrian and vehicular thoroughfare. It includes a red, a yellow and a green light, which imply different instructions.

Red for Stop: Pedestrians and vehicles stop proceeding.

Yellow for Caution: Pedestrians and vehicles are ready for stopping. If the drive is already in process, the speed should be slow.

Green for Proceed: Pedestrians and vehicles keep going with the abidance of traffic regulations.

In this project, you can program on ESP32 Coding Boxto control a mini traffic light. For instance, set the duration of each lights and the interval time among them. Besides, you may also add a timer to alter light colors to schedule.

3.17.2 Test Code

3.17.2.1 Code Flow

6-17

3.17.2.2 Build Code

There are two ways to upload the code: directly open the code file we provide; or manually build blocks.

Directly open the code file we provide:

  1. Click and choose Load from your computer

  1. We have already downloaded the codes on computer desktop, so open the file and choose 3-17-trafficLight.sb3

Manually build blocks:

  1. In events, find j1 block.

  2. In control, find and place j2 block under j1.

6-1-4-1-1

  1. In image-led, find and put j3 into j2 and set pin to IO27 and output HIGH.

6-17

  1. In control, add a j4 and set to 5S. And set pin to IO27 to output LOW on block j3.

6-17

  1. In control, find j6 and set repeating times to 3, place it under 6-17

6-17

  1. Build code in 6-17 to make yellow LED blink with an interval of 0.5s:6-17

6-17

  1. Build code under 6-17 to make red LED light up for 5S then go off.

6-17

Complete Test Code

6-17-3-2

3.17.2.3 Test Result

After uploading code, the green LED lights up for 5s and goes off. Immediately, the yellow LED blinks for three times. After that, the red LED lights up for 5s and goes off. These actions are exactly a model of traffic light and will repeat.