Project 5 – Traffic Lights

Create a traffic light by using a button and 3 LEDs (red, green and amber). The traffic light should start when the user presses a button. When the red light is on a buzzer should beep constantly.

These are the required resources for this project:


7x wires

(6x Male to Female)

(1x Male to Male)

4x Resistors

(3x 330Ω)

(1x 260k Ω)

3x LEDs (Green, Red & Amber [yellow])

1x button

1x Breadboard

Raspberry Pi with Raspbian Installed



Python 3


Once we have got the required resources ready, we should setup the system for the query to work.

Ground Pin is connected with the negative side of the breadboard


Red, Green & Amber LEDs are connected to the breadboard


3 resistors are connected from the positive arms of the LEDs to the Ground Rail


GPIO14 was connected to the negative arm of the Red LED

GPIO15 was connected to the negative arm of the Amber [yellow] LED

GPIO18 was connected to the negative arm of the Green LED

The buzzer was connected to the center of the breadboard


The buzzers negative side was connected to the Ground – with a wire (negative side of the breadboard)


The buzzers positive side was connected to GPIO24


The button was connected to the center of the breadboard


A resistor was connected on the top left side of the button


A wire was connected from the buttons resistor lane to the 3.3V Pin on the Raspberry Pi


A wire was connected from the bottom right of the button to GPIO23


The final setup outcome should look like these images below:

Once we have got the setup ready, we must begin developing the code for our system to work.

Libraries are imported first, the time library is imported for a timer (time.sleep) which will be used later on.


From the Raspberry Pi library, these default codes were set.


Before implementing anything, the GPIOs were hard-coded in the program

The Red LED was setup to be GPIO14, The Amber LED was setup to be GPIO15, The Green LED was setup to be GPIO18, The buzzer was setup to be GPIO24 and the button was setup to be GPIO23 as pull-up-down which is what we need for the system to work in this criteria.


A while loop is used. In this case if the button is true which means that it was pressed, the user will be prompted with a message that a pedestrian is preparing to cross the street.


Once the button is pressed, the crucial part is shown below. When the button is pressed there will be an 8 second waiting time (makes it more reliable), by reading the comments (red text), you can clearly understand what is happening during this process.

A for loop can also be used for the buzzer sound (a way to reduce lines)

Finally a printed statement declaring that cars can now cross, which in reality the cars will be continuing their journey.


In the end, as we used a try and except statement default error codes were stated.




 The final outcome can be seen through this video below:

Try it yourself!