5

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:


Hardware

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

20170312_102251

Hardware

Python 3

python


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

20170313_100536

Red, Green & Amber LEDs are connected to the breadboard

20170313_100640

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

20170313_100753

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

20170313_101058

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

20170313_101137

The buzzers positive side was connected to GPIO24

20170313_101308

The button was connected to the center of the breadboard

20170313_101343

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

20170313_101516

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

20170313_101555

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

20170313_101646

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.

Screenshot_4

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

Screenshot_1

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.

Screenshot_2

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.

Screenshot_3

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.

Screenshot_5

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

Screenshot_6

Screenshot_2

Screenshot_3

 The final outcome can be seen through this video below:

Try it yourself!

Advertisements