Lesson 10 - Traffic Light


In this lesson, we will conduct an interesting experiment – simulate a traffic light. Traffic light is a road signal for directing vehicular traffic by means of colored lights, typically red for stop, green for go, and yellow for proceed with caution.


- 1 * SunFounder Uno board

- 1 * Breadboard

- 1 * USB cable

- Jumper wires

- 1 * Rotary encoder

- 3 * LED

- 3 * Resistor (220Ω)


For a real traffic light, the time length for displaying red and green is much longer than yellow. As a result, we define two cycles with code: a short cycle and a long cycle. In a short cycle, the traffic light changes its order at a rate of roughly once per second. On the other hand, the long cycle is changed by the rotary encoder which determines the time length of red and green.

Experimental Procedures

Step 1: Build the circuit

Step 2: Program.

Use the code in the document below and copy/paste it into the Arduino Program on your computer:

Arduino Code lesson 10

Step 3: Compile the code

Step 4:  Upload the sketch to the SunFounder Uno board

Here you should see the red LED light up first, then the red LED and yellow LED, followed by the green LED, and finally the yellow LED again.