Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 29: Traffic Light Module

In this lesson, you will learn to simulate traffic lights using a Raspberry Pi. You’ll program the Raspberry Pi to control these LEDs in a sequence that resembles traffic lights: the red LED will be active for 3 seconds, the yellow LED will blink in a specific pattern, and then the green LED will turn on for 3 seconds. This project is a practical way to get started with GPIO interfacing and Python programming, suitable for those new to combining hardware and software with the Raspberry Pi.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Raspberry Pi 5

BUY

Traffic Light Module

BUY

Breadboard

BUY

Wiring

../_images/Lesson_29_Traffic_Light_Module_Pi_bb.png

Code

from gpiozero import LED
from time import sleep

# Initialize LED pins
red = LED(22)    # Red LED connected to GPIO pin 22
yellow = LED(27) # Yellow LED connected to GPIO pin 27
green = LED(17)  # Green LED connected to GPIO pin 17

# LED control in a continuous loop
try:
    while True:
        # Red LED cycle
        red.on()     # Turn on red LED
        sleep(3)     # Red LED on for 3 seconds
        red.off()    # Turn off red LED

        # Yellow LED blinking pattern
        yellow.on()  # Turn on yellow LED
        sleep(0.5)   # Yellow LED on for 0.5 second
        yellow.off() # Turn off yellow LED
        sleep(0.5)   # Off for 0.5 second
        yellow.on()  # Repeat blinking
        sleep(0.5)   # Yellow LED on for 0.5 second
        yellow.off() # Turn off yellow LED
        sleep(0.5)   # Off for 0.5 second
        yellow.on()  # Repeat blinking
        sleep(0.5)   # Yellow LED on for 0.5 second
        yellow.off() # Turn off yellow LED
        sleep(0.5)   # Off for 0.5 second

        # Green LED cycle
        green.on()   # Turn on green LED
        sleep(3)     # Green LED on for 3 seconds
        green.off()  # Turn off green LED

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

Code Analysis

  1. Import Libraries

    The gpiozero library is imported to control the GPIO pins, and the time library’s sleep function is used for timing delays.

    from gpiozero import LED
    from time import sleep
    
  2. Initialize LED pins

    Here, each LED is associated with a specific GPIO pin on the Raspberry Pi using the LED class from the gpiozero library.

    red = LED(22)    # Red LED connected to GPIO pin 22
    yellow = LED(27) # Yellow LED connected to GPIO pin 27
    green = LED(17)  # Green LED connected to GPIO pin 17
    
  3. LED Control Loop

    The while True: loop runs continuously, cycling through each LED. It turns each LED on and off in a specific pattern, using on(), off(), and sleep() functions.

    • Red LED is turned on for 3 seconds.

    • Yellow LED blinks: 0.5 seconds on, 0.5 seconds off, repeated three times.

    • Green LED is turned on for 3 seconds.

    try:
        while True:
            # Red LED cycle
            red.on()
            sleep(3)
            red.off()
    
            # Yellow LED blinking pattern
            # [The pattern is repeated three times]
    
            # Green LED cycle
            green.on()
            sleep(3)
            green.off()
    
  4. Exception Handling

    The except block catches a KeyboardInterrupt (usually generated by pressing Ctrl+C). It ensures all LEDs are turned off before the program exits, preventing the LEDs from being left in an undefined state.

    except KeyboardInterrupt:
        red.off()
        yellow.off()
        green.off()