Although we come across traffic lights almost every time we step out of our homes, we rarely stop to think about how they work. However, Gunnar Pelpman has done just that, and he has put the hugely popular single board computer, Raspberry Pi to good use. While most of the tutorials introduce turning on and off LEDs, he has prepared a somewhat more complex tutorial, one that teaches how to program traffic lights. Moreover, he has done this with the Raspberry Pi (RBPi) running the Windows 10 IoT Core.
Traffic Lights may look very complicated installations, but they are rather simple in operation. They mostly comprise a controller, the signal head, and the detection mechanism. The controller acts as the brains behind the installation and controls the information required to light up the lights through their various sequences. Depending on location and time of the day, traffic signals run under a variety of modes, of which two are the fixed time mode and the vehicle actuation mode.
Under the fixed time mode, the traffic signal will repeatedly display the three colors in fixed cycles, regardless of the traffic conditions. Although adequate in areas with heavy traffic congestion, this mode is very wasteful for a side road with light traffic—if for some cycles there are no waiting vehicles, the time could be more efficiently allocated to a busier approach.
The second most common mode of operation of the traffic signal is the vehicle actuation. As its name suggests, the traffic signal adjusts the cycle time according to the demands of vehicles on all approaches.
Sensors, installed in the carriageway or above the signal heads, register the demands of the traffic. After processing these demands, the controller allocates the cycle time accordingly. However, the controller has a preset minimum and maximum cycle time, and it cannot violate them.
The hardware for the project could not be simpler. Gunnar has used three LEDs—red, orange, and green—to represent the three in a traffic light. The LEDs have an appropriate resistor in series for current limiting, and three ports of the RBPi drive them on and off. The rest of the project is the software, for which Gunnar uses the UWP application.
According to Gunnar, there are two options for writing UWP applications—the first a blank UWP application and the second a background application for IoT—depending on your requirement. The blank UWP is good for trying things out as a start, as, at a later point of time, you can build a User Interface for your application.
After creating the project with the blank UWP application, Gunnar added a reference to Windows IoT Extensions for the UWP. Next, he opened the file MainPage.xaml and added his own code, which begins with a test for the wiring. He uses the init() function to initialize the GPIO pins and stop() to turn all LEDs off. Then the code turns on all LEDs for 10 seconds to signal everything is working fine.
According to Gunnar, the primitive code mimics the traffic lights. He uses a separate code for the cycling of the traffic lights, and another for blinking them on and off. He uses the play() function for running ten cycles of the traffic light.