How to use Traffic Light Module with Raspberry PI and Python

0
(0)

Last Updated on 7th September 2024 by peppe8o

In this tutorial I will show you how to connect and use a Traffic Light Module with Raspberry PI, showing the required wiring and an example with Python.

For my tests, I will use a Raspberry PI 5 Model B computer board, but this should work with any Raspberry PI computer board.

About the Traffic Light Module

The traffic light module combines 3 LEDs, having the same colors and order of real traffic lights:

traffic-light-module-picture

It simplifies LEDs management because it adds by default the required resistors attached to every LED, so you don’t have to worry about protecting the LEDs from burning. You can see these resistors below each LED, as shown in the following picture:

traffic-light-module-led-resistor

The module can be interfaced with 4 simple PINs, each one having an intuitive naming:

traffic-light-module-pinout

The PINs function is described below:

  • GND: connects to the Ground
  • R: controls the red LED
  • Y: controls the yellow LED
  • G: controls the green LED

Once you have connected the GND PIN with any ground reference of your board, you can pull up one or more of the other PINs to switch on the related LED(s).

What We Need

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Raspberry PI 4 model B 8GB

Step-by-Step Procedure

Traffic Light Module and Raspberry PI Wiring Diagram

Please arrange the wiring as shown in the following picture, according to the Raspberry PI pinout (all the most recent Raspberry PI computer boards have the same pinout of Raspberry PI Zero):

traffic-light-module-raspberry-pi-wiring-diagram

Please find below some pictures from my home lab:

traffic-light-module-raspberry-pi-wiring-01
traffic-light-module-raspberry-pi-wiring-02
traffic-light-module-raspberry-pi-wiring-03

Prepare the Raspberry PI Operating System

The first step is installing the Raspberry PI OS Lite (I suggest the 64-bit version, for boards supporting it) to get a fast and light operating system (headless). If you need a desktop environment, you can also use the Raspberry PI OS Desktop, in this case working from its terminal app. Please find the differences between the 2 OS versions in my Raspberry PI OS Lite vs Desktop article.

Please make sure that your OS is up to date. From your terminal, use the following command::

sudo apt update -y && sudo apt upgrade -y

Get the p8o Library

I’ve created a library that will help you keep your Python code simpler to read and use. You can get the p8o-pi library in your Raspberry PI from my GitHub repository with the following command:

wget https://raw.githubusercontent.com/peppe8o/p8o_pi/main/p8o_pi.py

This will download a “p8o_pi.py” file, that must stay in the same folder of the file with the test example (that we’ll see below) or in the default Python path.

Now, please download a simple test. You can get it from my download area in your Raspberry PI with the following command:

wget https://peppe8o.com/download/python/trafficlight/traffic_light_test.py

The following paragraphs will explain this test script.

In the beginning, we import the required modules. The sleep function from the time module allows us to add a specific delay (in seconds) with the traffic light module fixed in a state (red or green).

from time import sleep
from p8o_pi import traffic_light

Then, we must set the LED variables where we tell the script the used GPIOs. If you used my same connection schema, you can leave them unchanged, if you used different GPIOs, please update the numbers here:

green_LED = 14
yellow_LED = 15
red_LED = 18

Now, we initialize the traffic_light object with the following line:

tl = traffic_light(red_LED, yellow_LED, green_LED)

This object has 3 main methods:

  • go(): this will activate the green LED, switching off all the other LEDs
  • stop(): this method will make the yellow LED blink with the green LED fixed on. After 3 seconds, the red will activate and the other 2 LEDs will be switched off. You can also change the duration of the yellow blinking stage by specifying the number of seconds (for example, “stop(10)” will make it blink for 10 seconds)
  • off(): switches off all the 3 LEDs

The main program runs a loop where the green light activates for 3 seconds, then the yellow blinks (with the green LED fixed) for 3 seconds, and finally the red LED activates for 3 seconds:

try:
    while True:
        tl.go()
        sleep(3)
        tl.stop()
        sleep(3)

The final except statement will allow you to stop the loop by pressing the CTRL+C combination from your keyboard, switching off all the LEDs:

except KeyboardInterrupt:
    # Turn off all LEDs and exit safely on keyboard interrupt
    tl.off()

Test the Traffic Light Module Raspberry PI

You can run this test with the following command:

python3 traffic_light_test.py

What’s Next

Interested in more cool projects for your Raspberry PI? Take a look at peppe8o Raspberry PI tutorials.

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?