0% found this document useful (0 votes)
8 views2 pages

Traffic Signal

The Python code uses a Raspberry Pi to control GPIO pins connected to LEDs representing traffic lights, turning the lights on and off in sequence with time delays to simulate a basic traffic signal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Traffic Signal

The Python code uses a Raspberry Pi to control GPIO pins connected to LEDs representing traffic lights, turning the lights on and off in sequence with time delays to simulate a basic traffic signal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a python code using raspberry pi to display traffic signals.

import RPi.GPIO as GPIO


import time
# Define GPIO pin numbers
RED_PIN = 17
YELLOW_PIN = 27
GREEN_PIN = 22
# Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(RED_PIN, GPIO.OUT)
GPIO.setup(YELLOW_PIN, GPIO.OUT)
GPIO.setup(GREEN_PIN, GPIO.OUT)
def traffic_lights():
try:
while True:
# Turn on Red light
GPIO.output(RED_PIN, GPIO.HIGH)
GPIO.output(YELLOW_PIN, GPIO.LOW)
GPIO.output(GREEN_PIN, GPIO.LOW)
time.sleep(5) # Red light duration
# Turn on Green light
GPIO.output(RED_PIN, GPIO.LOW)
GPIO.output(YELLOW_PIN, GPIO.LOW)
GPIO.output(GREEN_PIN, GPIO.HIGH)
time.sleep(5) # Green light duration
# Turn on Yellow light
GPIO.output(RED_PIN, GPIO.LOW)
GPIO.output(YELLOW_PIN, GPIO.HIGH)
GPIO.output(GREEN_PIN, GPIO.LOW)
time.sleep(2) # Yellow light duration
except KeyboardInterrupt:
pass
finally:
# Clean up GPIO
GPIO.cleanup()
if __name__ == '__main__':
traffic_lights()

You might also like