Traffic Light System Using Raspberry Pi 3 (Python)


This article demonstrates how to make a smart traffic system using Raspberry Pi and Python.
Learn how to use the GPIO pins on your Raspberry Pi to interface with electronic components, such as LEDs, Buzzer and buttons.
As well as a Raspberry Pi with an SD card and the usual peripherals, you'll also  need:
  • 1x Solderless breadboard
  • All king of jumper leads 
  • 1x Tactile button
  • 3x LEDs (Red, Green, and Yellow)
  • 3x 330 ohm Register
  • Buzzer
  • Button
Raspberry Pi needs NOOBS OS. NOOBS is a way of making setting up a Raspberry Pi for the first time much, much easier. You won't need network access, and you won't need to download imaging software. Just head to the download page, grab a copy of the NOOBS zip file and unpack it onto a freshly formatted 4GB (or larger) SD card. Once you have installed an operating system, your Pi will boot as normal.
GPIO pins 
One powerful feature of the Raspberry Pi is the row of GPIO pins along the top edge of the board. GPIO stands for General-Purpose Input/Output. These pins are a physical interface between the Raspberry Pi and the outside world.
If you don't have a pin label, then this can help you to identify the pin numbers
3.3 volts           
Anything connected to these pins will always get 3.3 v of power.
5 volts 
Anything connected to these pins will always get 5 v of power.
Zero volts, used to complete a circuit
These pins are for general-purpose use and can be configures as input or output pins
Special purpose pins. 
Lighting an LED 
LEDs are delicate little things. if you put too much current through them they will pop. To limit the current going through the LED, you should always use a register in series with it.
Long leg an LED to the Pi 3.3v and the short leg to a GND pin. Automatically LED will turn ON
If you connect special purpose(GPIO) pin are connect to a long leg, make sure to write the import code.
Switching an LED on and off
GPIO Zero is a new Python library which provides a simple interface to everyday GPIO component. It comes installed by default in Rasbian.
Open IDLE(Integrated Development Environment), which you can use to write and run code.
Raspbian Menu Icon >> Programming >> Python 3 (IDLE). 
  • To create a new file in IDLE, You can click on File and then New File in IDLE's menu bar. 
  • Create a new file by clicking File >> New File
  • Save the new file by clicking File >> Save. Save the file as
  • You'll need the LED Class, and to tell it that the LED is on pin 17. Write the following code in your new file.
  1. from gpiozero import LED    
  2. led = LED(17)  
  • To make the LED switch on, type the following and press Enter
  1. led.on()  
  • To make it switch off you can type 
  Your LED should switch on and then off again. But that's not all you can do. Similarly checks the Buzzer and Button. Just import a Buzzer and Button for the header file.
Making Traffic Light
We need a breadboard, three LEDs, a button, a buzzer, and the necessary jumper cables and registers.
First, you need to understand how each component is connected.
  • A push-button requires 1 ground pin and 1 GPIO pin
  • An LED requires 1 ground pin and 1 GPIO pin, with a current limiting register
  • A buzzer requires 1 ground pin and 1 GPIO pin
Place the components on the breadboard and connect them to the Raspberry Pi GPIO pins, according to the following diagram.
Component GPIO pin 
   Button                           21
   Red LED                       25
   Yellow LED                    8
   Green LED                    7
   Buzzer                           15
This is the same as the Switching and LED on and off step
  • Open Python 3 from the main menu
  • Create a new file just save with the project
Add TrafficLight, Button and Buzzer Code
  1. from gpiozero import Button, TrafficLights, Buzzer    
  2. from time import sleep    
  4. buzzer = Buzzer(15)    
  5. button = Button(21)    
  6. lights = TrafficLights(2587)    
  8. while True:    
  9.            button.wait_for_press()   
  10.            buzzer.on()   
  12.            sleep(1)    
  13.            lights.amber.on()    
  14.            sleep(1)    
  16.            sleep(1)    
Run your Powershell coding (F5)
Finally, we have successfully created a smart traffic system using a Raspberry Pi.