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

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:
Click
and choose Load from your computer

We have already downloaded the codes on computer desktop, so open the file and choose
3-17-trafficLight.sb3
Manually build blocks:
In
, find
block.In
, find and place
block under
.

In
, find and put
into
and set pin to IO27 and output HIGH.

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

In
, find
and set repeating times to 3, place it under 

Build code in
to make yellow LED blink with an interval of 0.5s:

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

Complete Test Code

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.