Raspberry Pi Traffic Light Controller

Table of Contents


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

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.

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.

LED Option

Real Traffic Light Option

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

Demostrations and Video Discusssions

These demostration videos show the traffic in one of several operating modes.

Raspberry PI Traffic Light Controller Demonstration

Discussing the Traffic Light Code

Initial Setup

Install Raspbian

You will need to install Raspbian 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 Raspbian using Ubuntu, I made a video tutorial which you can watch at https://www.youtube.com/watch?v=Wy1_MWWlkNI.

Install Script

In the scripts directory, run the install.sh script as root user. This will install of the required software and python packages.

Update Apache Configuration

Search for the file containing "PrivateTmp=true". This file should be in your /etc directory. Change this value to PrivateTmp=false. Then restart Apache. You may use

cd /etc/
grep -R "PrivateTmp=true" *

to search for the file that contains this value. Once grep returns the file name, edit the file and make the stated change.

Running The Scripts

To control the traffic light, run the raspitraffic.py script via command line.

python raspitraffic.py

Then visit the webpage to your TrafficPi in a web browser. A form will be presented with a list of programs to select from. Select the program you wish to run and click the "Submit" button.

If a program is already running, the newly selected program will start once the end of the current program has been reached. If no program has been selected, the newly selected program will start immediately.

Pseudocode Program

The Pseudocode Program allows you to write your own program for controlling the traffic light. On the Control Panel webpage, enter each command that you want the light to perform on a line by itself in the "Pseudocode Commands" textbox. The list of commands are listed on the Control Panel webpage below the textbox.

Move and Run Code on the Pi

Software to Install on Pi

Run these commands to install the necessary software on the Pi. You only need to do them once.

sudo apt-get update
sudo apt-get install curl libunwind8 gettext apt-transport-https

Publish Your App

Then run the publish command on your app

dotnet publish -r linux-arm

Copy the Files

Then copy the files from your computer to the Pi. I used SSH for this.

scp -pr bin/Debug/netcoreapp3.1/linux-arm/publish/* pi@trafficpi://home/pi/rpidotnet

Run App On Pi

Run the program by calling the executable. This filename will be the name of your project.



  • If your app does not run, make sure that execute permissions have been set for all roles (755).



See LICENSE for more details.