Creating a Stoplight with a Raspberry Pi

Online Link:


The purpose of this guide is to walk through that steps of creating a simple Raspberry Pi controlled stoplight. It is a great place to start if you are a looking to learn the following:

  • Basic python coding
  • Becoming familiar with the GPIO interface
  • Wiring and creating circuits

Needed Materials

The needed materials for this lab are:

  • Raspberry Pi
  • Jumper Wires
  • 1 x breadboard
  • 1 x red LED
  • 1 x yellow LED
  • 1 x green LED
  • 3 x 220 ohm resistors (RRBLBLBR)
  • Computer
  • Access to Raspberry Pi (SSH or Monitor + Keyboard + mouse)


I used the following references in the creation of the guide


  1. Setup Raspberry pi with a Linux based operating system. If you haven’t already done so follow this guide
  2. Ensure the following dependencies are met:
sudo apt install python3 python3-pip 
sudo pip3 install flask
  1. Here is a simple diagram of the elements needed to complete this project: diagram
  2. Create a simple flask web server using this guide found in the references above. Create 5 buttons that when clicked send a post request back to the server which allows us to execute different code depending on which button was pressed.
  3. Next create another file that can be called by the web server that uses the information in this guide to turn on and off the GPIO pins.
  4. Finally wire up the GPIO pins according to the lower picture. In the python code we set each pin to be an out switch so when activated by the code they will output 3.3v. This means we need to put 1 220 ohm resister in series with it and the light bulb before ending the circuit on one of the ground pins.




finished rpi

  1. The trickiest part for me to get working was how to get the stoplight to run in a loop indefinitely until you it the off button. To do this I researched a couple of option: multi threading and then multiprocessing. Originally I used multi threading which appeared to work but unfortunately I ran into a couple issues with it pulling the correct process ID (pid) of the stoplight thread. This meant that it wasn’t successfully being terminated. Eventually I did some research on the python multi processing library which ended up being a much easier route to solving the problem. The code needed to multi process is fairly simple and is contained in the file.

Thought Questions

1. What language did you choose for implementing this project? Why?

For this project I chose to use python because I already have some experience with it. It also is able to access the GPIO pins just as easily as in bash plus you can run a web server using the flask package making it really easy to incorporate all elements of the lab in as few lines of code as possible.

2. What is the purpose of the resister in the simple circuit? What would happen if you omitted it?

The purpose of the resister is to reduce the the flow of electricity in this circuit to the point that it is safe to power the LED to its specifications. We were told in class that the LED would still work but that you would risk burning it out.

3. What are the practical applications of this device? What enhancements or modifications would you make?

Well I guess in practice it is a stoplight. I’m sure it would make a great kids toy letting them play stoplight without needing a person to call out the colors. It also would be fun to use in miniature design to create a working set piece. Something that I would change is variable sleep timers that are adjustable from the web interface.

4. How much time did you spend on the lab and lab report?

Time spent on lab: 4 hours. Time spent on report 2 hours

Posted in IOT

Leave a Reply

Your email address will not be published. Required fields are marked *