Raspberry Pi Traffic Light Controller (TrafficPi)

Traffic light

Table of Contents

Purpose

The purpose of this project is to educate children about the STEM (Science, Technology, Engineering, and Mathematics) fields. Through the use of low cost devices and effective teaching, children are able to associate what they are learning with interactions with everyday items. For example, most future engineers are aware of the purpose of a traffic light, but most are not aware of a traffic lights' internal workings. This project is designed to educate future engineers about the impact that computers and computer programming has with society. This project targets the "T, E, and M" of "STEM" by using electronic circuits for controlling the lights, software for controlling the electronic circuits, and mathematical calculations for making timing decisions.

Teach Software Versioning

One of the goals of this project is to be able to teach the next generation of engineers about the programming, software versioning, and how valuable engineers and technologists are to society and the impact that they have. A game was developed to be combined with this project to teach about software versioning, how sometimes Software Developers don't get it right the first time, and the negative impact that can occur if they don't get it right the first time.

Below is the image of a group of future engineers working their way to developing the psuedocode for a normal traffic cycle. As shown in the first version, all of the lights turned on but never turned off. In the final version, they figured out that they had to turn on and turn off each light as well as include a delay in between each light change.

Image of Versioning

Back to Top

Parts List

Below are the list of parts used for this project. You'll need to choose one of the mentioned options below depending on your budget. Using LEDs is cheaper than getting an actual traffic light.

  • Raspiberry Pi 3 Kit (May work on other models, but has only been tested on Raspberry Pi 3)
  • Micro SD card (to run the OS and store project files)
  • USB keyboard (mouse optional)
  • HDMI display
  • Breadboard Jumper Cables
  • Breadboard (optional)
  • Python 2.7.9 (May work with later versions, but has only been tested with 2.7.9)
  • Raspberry Pi OS Jessie, Stretch, or Buster (May work on other OSs, but has only been tested with Raspberry Pi OS Jessie)

LED Option

Real Traffic Light Option

Back to Top

Installation and Setup

Install Raspberry Pi OS

You will need to install Raspberry Pi OS on your SD. Once you have completed this install, Then you can insert the SD card into the Raspberry Pi and power it on.

To install Raspberry Pi OS using Ubuntu, I made a video tutorial which you can watch at https://www.youtube.com/watch?v=Wy1_MWWlkNI.

Pin Setup

Below is the mapping for the connections to the Raspberry Pi. The Pin numbers listed are the physical pin numbers on the board, not the GPIO pin numbers. If you are not using a relay board, the connections can be made directly to a breadboard with LEDs connected.

Pi Pin (Board) GPIO Device Connection
2 -- LCD Display VCC (+5V)
3 -- LCD Display SDA
4 -- Relay Board VCC (+5V)
5 -- LCD Display SLC
19 11 Red Signal
21 9 Yellow Signal
23 10 Green Signal
30 -- LCD Display GND
34 -- Relay Board GND

Visual of Pin Connections to Relay Board

Image of connections on Raspberry Pi board

System Service

To set up the application as a service, run the below commands. If you see error messages when running the commands, you may need to run them with "sudo" privileges. See the System Service page for details on how to add or remove the application as a system service.

Create System Service

sudo cp almostengrtrafficpiweb.service /lib/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable almostengrtrafficpiweb.service
sudo systemctl start almostengrtrafficpiweb.service
sudo systemctl status almostengrtrafficpiweb.service

Run App On Pi

To run the applicatoin via the command line (not using the system service), then you can run the commands below.

cd trafficpi
./Almostengr.TrafficPi.Web

To exit the application after running it via command line, press Ctrl+C.

Back to Top

Video Demonstration

Video demonstration of version 1.0 of the project is available to be watched at https://www.youtube.com/watch?v=lr_ZJNX0viM. This version of the demonstration of the traffic light working with an LCD screen connected.

Further discussion of the traffic light code can be watched at https://www.youtube.com/watch?v=ZyBnWOX3wGE.

Back to Top

Controller Technology

The controller currently uses .NET Core 3.1. Tt is a multi-project application.

User Interface (UI) / Front End

The user interface is a .NET Core MVC application. When you select the program that you want to run from the application home page, the application then runs a linux command that runs the back end program.

When the program is changed on the front end, the back end program is then terminated and the newly select program is started.

Relay Control / Back End

The back end of the application is a .NET Core Worker Service. The program modes are defined within this worker service. The worker service will continue running until it is terminated by the front end application or by a user via the command line or SSH.

Source Code

The source code for this project can be downloaded from GitHub at https://github.com/almostengr/trafficpi.

Back to Top

Uninstall Traffic Pi

Remove System Service

To remove the application as a system service, run each of the commands below.

sudo systemctl disable almostengrtrafficpiweb.service
sudo systemctl stop almostengrtrafficpiweb.service
sudo systemctl status almostengrtrafficpiweb.service
sudo rm /lib/systemd/system/almostengrtrafficpiweb.service

After running all of the commands above, then reboot the system.

Application Files

To remove the application files, you can remove the entire trafficpi directory.

rm -rf trafficpi

Back to Top

Acknowledgements

Back to Top

Troubleshooting

System Service Output / Log

To see the logged output from the system service, run the command:

journalctl -u almostengrtrafficpiweb.service -b

or

journalctl -u almostengrtrafficpiweb.service -b -f

If an error occurs in the application, the exception message will show here.

Back to Top

Back To Top