Raspberry Pi - Traffic Light
In this tutorial, we are going to learn how to use Raspberry Pi control the traffic light module. In detail, we will learn:
How to connect the traffic light module to Raspberry Pi
How to program Raspberry Pi to control RGB traffic light module
How to program Raspberry Pi to control RGB traffic light module without using delay() function
Or you can buy the following sensor kits:
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.
A traffic light module includes 4 pins:
GND pin: The ground pin, connect this pin to GND of Raspberry Pi.
R pin: The pin to control the red light, connect this pin to a digital output of Raspberry Pi.
Y pin: The pin to control the yellow light, connect this pin to a digital output of Raspberry Pi.
G pin: The pin to control the green light, connect this pin to a digital output of Raspberry Pi.
This image is created using Fritzing. Click to enlarge image
import RPi.GPIO as GPIO
import time
PIN_RED = 7
PIN_YELLOW = 8
PIN_GREEN = 25
RED_TIME = 4
YELLOW_TIME = 4
GREEN_TIME = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_RED, GPIO.OUT)
GPIO.setup(PIN_YELLOW, GPIO.OUT)
GPIO.setup(PIN_GREEN, GPIO.OUT)
try:
while True:
GPIO.output(PIN_RED, GPIO.HIGH)
GPIO.output(PIN_YELLOW, GPIO.LOW)
GPIO.output(PIN_GREEN, GPIO.LOW)
time.sleep(RED_TIME)
GPIO.output(PIN_RED, GPIO.LOW)
GPIO.output(PIN_YELLOW, GPIO.HIGH)
GPIO.output(PIN_GREEN, GPIO.LOW)
time.sleep(YELLOW_TIME)
GPIO.output(PIN_RED, GPIO.LOW)
GPIO.output(PIN_YELLOW, GPIO.LOW)
GPIO.output(PIN_GREEN, GPIO.HIGH)
time.sleep(GREEN_TIME)
except KeyboardInterrupt:
GPIO.cleanup()
Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
Make sure your Raspberry Pi is connected to the same local network as your PC.
Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update
sudo apt-get install python3-rpi.gpio
import RPi.GPIO as GPIO
import time
PIN_RED = 7
PIN_YELLOW = 8
PIN_GREEN = 25
RED_TIME = 4
YELLOW_TIME = 4
GREEN_TIME = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_RED, GPIO.OUT)
GPIO.setup(PIN_YELLOW, GPIO.OUT)
GPIO.setup(PIN_GREEN, GPIO.OUT)
try:
while True:
GPIO.output(PIN_RED, GPIO.HIGH)
GPIO.output(PIN_YELLOW, GPIO.LOW)
GPIO.output(PIN_GREEN, GPIO.LOW)
time.sleep(RED_TIME)
GPIO.output(PIN_RED, GPIO.LOW)
GPIO.output(PIN_YELLOW, GPIO.HIGH)
GPIO.output(PIN_GREEN, GPIO.LOW)
time.sleep(YELLOW_TIME)
GPIO.output(PIN_RED, GPIO.LOW)
GPIO.output(PIN_YELLOW, GPIO.LOW)
GPIO.output(PIN_GREEN, GPIO.HIGH)
time.sleep(GREEN_TIME)
except KeyboardInterrupt:
GPIO.cleanup()
python3 traffic_light.py
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
It's important to note that the exact workings of a traffic light can vary depending on the specific design and technology used in different regions and intersections. The principles described above provide a general understanding of how traffic lights operate to manage traffic and enhance safety on the roads.
The code above demonstrates individual light control. Now, let's enhance the code for better optimization.
import RPi.GPIO as GPIO
import time
PIN_RED = 7
PIN_YELLOW = 8
PIN_GREEN = 25
RED_TIME = 2
YELLOW_TIME = 1
GREEN_TIME = 2
RED = 0
YELLOW = 1
GREEN = 2
pins = [PIN_RED, PIN_YELLOW, PIN_GREEN]
times = [RED_TIME, YELLOW_TIME, GREEN_TIME]
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_RED, GPIO.OUT)
GPIO.setup(PIN_YELLOW, GPIO.OUT)
GPIO.setup(PIN_GREEN, GPIO.OUT)
def traffic_light_on(light):
for i in range(len(pins)):
if i == light:
GPIO.output(pins[i], GPIO.HIGH)
else:
GPIO.output(pins[i], GPIO.LOW)
try:
while True:
traffic_light_on(RED)
time.sleep(times[RED])
traffic_light_on(YELLOW)
time.sleep(times[YELLOW])
traffic_light_on(GREEN)
time.sleep(times[GREEN])
except KeyboardInterrupt:
GPIO.cleanup()
import RPi.GPIO as GPIO
import time
PIN_RED = 7
PIN_YELLOW = 8
PIN_GREEN = 25
RED_TIME = 2
YELLOW_TIME = 1
GREEN_TIME = 2
RED = 0
YELLOW = 1
GREEN = 2
pins = [PIN_RED, PIN_YELLOW, PIN_GREEN]
times = [RED_TIME, YELLOW_TIME, GREEN_TIME]
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_RED, GPIO.OUT)
GPIO.setup(PIN_YELLOW, GPIO.OUT)
GPIO.setup(PIN_GREEN, GPIO.OUT)
def traffic_light_on(light):
for i in range(len(pins)):
if i == light:
GPIO.output(pins[i], GPIO.HIGH)
else:
GPIO.output(pins[i], GPIO.LOW)
try:
while True:
for light in range(RED, GREEN + 1):
traffic_light_on(light)
time.sleep(times[light])
except KeyboardInterrupt:
GPIO.cleanup()
import RPi.GPIO as GPIO
import time
PIN_RED = 7
PIN_YELLOW = 8
PIN_GREEN = 25
RED_TIME = 2
YELLOW_TIME = 1
GREEN_TIME = 2
RED = 0
YELLOW = 1
GREEN = 2
pins = [PIN_RED, PIN_YELLOW, PIN_GREEN]
times = [RED_TIME, YELLOW_TIME, GREEN_TIME]
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_RED, GPIO.OUT)
GPIO.setup(PIN_YELLOW, GPIO.OUT)
GPIO.setup(PIN_GREEN, GPIO.OUT)
light = RED
last_time = time.time()
def traffic_light_on(light):
for i in range(len(pins)):
if i == light:
GPIO.output(pins[i], GPIO.HIGH)
else:
GPIO.output(pins[i], GPIO.LOW)
try:
while True:
if (time.time() - last_time) > times[light]:
light += 1
if light >= 3:
light = RED
traffic_light_on(light)
last_time = time.time()
except KeyboardInterrupt:
GPIO.cleanup()