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 create a traffic light system using the Raspberry Pi Pico W. You’ll program the Pico W to control three LEDs – red, yellow, and green – mimicking a real traffic light. This project offers a practical introduction to using Pulse Width Modulation (PWM) for LED brightness control and basic control structures in MicroPython. It’s ideal for beginners looking to explore digital signal processing and gain confidence in coding on the Raspberry Pi Pico W platform.
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 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi Pico W |
|
Wiring
Code
from machine import Pin, PWM
import time
# Initialize pins for LEDs
red = PWM(Pin(26), freq=1000) # red LED
yellow = PWM(Pin(27), freq=1000) # yellow LED
green = PWM(Pin(28), freq=1000) # green LED
# Function to set the brightness of an LED (0-100%)
def set_brightness(led, brightness):
if brightness < 0 or brightness > 100:
raise ValueError("Brightness should be between 0 and 100")
led.duty_u16(int(brightness / 100 * 65535))
try:
# Example sequence
while True:
# Green light for 5 seconds
set_brightness(green, 100)
time.sleep(5)
set_brightness(green, 0)
# Blink Yellow light
set_brightness(yellow, 100)
time.sleep(0.5)
set_brightness(yellow, 0)
time.sleep(0.5)
set_brightness(yellow, 100)
time.sleep(0.5)
set_brightness(yellow, 0)
time.sleep(0.5)
set_brightness(yellow, 100)
time.sleep(0.5)
set_brightness(yellow, 0)
time.sleep(0.5)
# Red light for 5 seconds
set_brightness(red, 100)
time.sleep(5)
set_brightness(red, 0)
except KeyboardInterrupt:
# Turn off RGB LED on interrupt
set_brightness(red, 0)
set_brightness(yellow, 0)
set_brightness(green, 0)
Code Analysis
Importing Libraries
The
machinelibrary is used for controlling hardware components, andtimeis used for creating delays.from machine import Pin, PWM import time
Initializing LED Pins
Here, we initialize the pins connected to the LEDs. PWM is used to control the brightness of the LEDs.
red = PWM(Pin(26), freq=1000) # red LED yellow = PWM(Pin(27), freq=1000) # yellow LED green = PWM(Pin(28), freq=1000) # green LED
Defining the Set Brightness Function
Note
Due to the fact that the pins of Raspberry Pi Pico can only output a maximum voltage of 3.3V, the green LED will appear dim.
This function sets the brightness of the LEDs. It takes two parameters: the LED and the desired brightness level (0-100%). The
duty_u16method is used to set the PWM duty cycle.def set_brightness(led, brightness): if brightness < 0 or brightness > 100: raise ValueError("Brightness should be between 0 and 100") led.duty_u16(int(brightness / 100 * 65535))
Main Loop and Traffic Light Sequence
The
while Trueloop makes the code run continuously. It controls the sequence of the traffic light: green, yellow (blinking), and red.try: while True: # Green light for 5 seconds set_brightness(green, 100) time.sleep(5) set_brightness(green, 0) ...
Handling Keyboard Interrupt
The
except KeyboardInterruptblock is used to handle a manual interruption (like Ctrl+C). It turns off all LEDs when the script is interrupted.except KeyboardInterrupt: set_brightness(red, 0) set_brightness(yellow, 0) set_brightness(green, 0)