Raspberry Pi Traffic Light Circuit with GPIO Part 2

It continues with the second part and the actual Traffic Light Circuit. Let’s take a look at the various Traffic Light Signals on Wikipedia. We want to recreate them at the push of a Button.

(Source: Wikipedia)

First of all, we build the Circuit:


So we have the following Phases:

  1. no Button pressed – permanently red (yellow and green off)
  2. Button is pressed – red and yellow light up (green off), duration: 3 seconds
  3. Change to green (red and yellow off), duration: 15 seconds
  4. Change to yellow (red and green off), duration: 3 seconds
  5. Back to Step 1.

This now has to be converted into Code. For example, I created a Script for this:

sudo nano ampel_skript2.py

This time, instead of the Pin Numbers, I use the GPIO Numbers.

It is called again with

sudo python ampel_skript2.py

The red LED lights up and pressing the Button calls up the Function and changes the Traffic Lights. Execution of the Script terminated with CTRL + C.

The Functions can be changed and extended. Here is a Picture of my Circuit.


Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!