0% found this document useful (0 votes)
118 views4 pages

Traffic Lights Python

This document describes how to create an interactive traffic light simulation using Python and Raspberry Pi GPIO pins. It includes instructions on connecting LEDs and buttons to pins, importing the GPIOZero library, and programming the lights to respond to button presses by turning on and off individual lights or using the TrafficLights class. Later sections explain how to make the lights sequence through different states automatically or for a pedestrian crossing, and add a buzzer to signal when it is safe to cross. The goal is to create a complete interactive traffic light simulation with automated cycles and on-demand crossing signals.

Uploaded by

Debraj Maiti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views4 pages

Traffic Lights Python

This document describes how to create an interactive traffic light simulation using Python and Raspberry Pi GPIO pins. It includes instructions on connecting LEDs and buttons to pins, importing the GPIOZero library, and programming the lights to respond to button presses by turning on and off individual lights or using the TrafficLights class. Later sections explain how to make the lights sequence through different states automatically or for a pedestrian crossing, and add a buzzer to signal when it is safe to cross. The goal is to create a complete interactive traffic light simulation with automated cycles and on-demand crossing signals.

Uploaded by

Debraj Maiti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Interactive traffic

lights with Python

Introduction
Connect the LEDs

1 Connect your LEDs to the following pins:

LED GPIO
Button 21
Red LED 25
Amber LED 28
Green LED 27
Buzzer 15

Control the LEDs


Introduction

and button

1 Open Mu from the main menu.

1
2 Enter the following code:

from gpiozero import LED, Button

red = LED(25)
button = Button(21)

while True:
if button.is_pressed:
red.on()
else:
red.off()

1
3 Save your code and run it with F5

This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
1
4 Remove the while loop and add two more LEDs

from gpiozero import LED, Button

red = LED(25)
amber = LED(28)
green = LED(27)

button = Button(21)

1
5 Get them to come on when the
button is pressed: while True:
if button.is_pressed:
green.on()
6 Run the code and press the amber.on()
button. red.on()
else:
green.off()
amber.off()
red.off()
Traffic lights
Introduction

You can use the built-in TrafficLights class instead of three individual LEDs.

1 Amend the from gpiozero import... line to replace LED with TrafficLights:

from gpiozero import TrafficLights, Button


from time import sleep

button = Button(21)
lights = TrafficLights(25, 28, 27)

while True:
button.wait_for_press()
lights.on()
button.wait_for_release()
lights.off()

This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
21 Try changing the lights to blink:

while True:
lights.blink()
button.wait_for_press()
lights.off()
button.wait_for_release()

Traffic lights sequence

As well as controlling the whole set of lights together, you can also control each LED individually.
With traffic light LEDs, a button, and a buzzer, you can create your own traffic lights sequence,
complete with pedestrian crossing!

1 Modify your loop to perform an 1


2 Add a wait_for_press() so that pressing
automated sequence of LEDs being lit: the button initiates the sequence:

while True: while True:


lights.green.on() button.wait_for_press()
sleep(1) lights.green.on()
lights.amber.on() sleep(1)
sleep(1) lights.amber.on()
lights.red.on() sleep(1)
sleep(1) lights.red.on()
lights.off() sleep(1)
lights.off()

Try some more sequences of your own.

3 Now try creating the full traffic lights sequence:

Green on

Amber on

Red on

Red and amber on

Green on

Be sure to turn the correct lights on and off at the right time, and make sure you use
sleep to time the sequence perfectly.

This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning
14 Try adding the button for a pedestrian crossing. The button should move the lights to red
(not immediately), and give the pedestrians time to cross before moving the lights back
to green until the button is pressed again.

1
5 Now try adding a buzzer to beep quickly to indicate
that it is safe to cross, for the benefit of visually buzzer = Buzzer(15)
impaired pedestrians:
buzzer.on()
buzzer.off()
buzzer.beep(0.1, 0.1)

16 Your final interactive traffic lights code should start on a green light and then:

Wait for the button to be pressed

When pressed, change to red/amber, then green

Beep for a while to say it’s time to cross

Go to amber and then green

Repeat

RASPBERRY

This project is provided free by the Raspberry Pi Foundation under a Creative Commons licence.
See more at projects.raspberrypi.org and github.com/raspberrypilearning

You might also like