Second Pi GPIO project - traffic lights

Philip’s second GPIO project using the Raspberry Pi

After the success of our first project we wanted to try building the traffic lights detailed on the OCR resource sheet
here

After helping Philip solder the components onto a prototype circuit board we connected it up to the GPIO breakout board.

I showed Philip how to write the code to make one of the lights flash on and off. He was then challenged to find a way of making the three LEDs show the correct traffic light sequence.




GPIO-project21

GPIO-project22

GPIO-project23

GPIO-project24

In a similar way to the first project I was really pleased with the determination Philip showed in writing this simple piece of code.

import time
import RPi.GPIO as GPIO

GPIO.cleanup()

GPIO.setmode(GPIO.BOARD)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)
GPIO.setup(7,GPIO.OUT)

GPIO.setwarnings(False)

GPIO.output(7,GPIO.HIGH)
time.sleep(5)
GPIO.output(5,GPIO.HIGH)
time.sleep(2)
GPIO.output(7,GPIO.LOW)
GPIO.output(5,GPIO.LOW)
GPIO.output(3,GPIO.HIGH)
time.sleep(10)
GPIO.output(3,GPIO.LOW)
GPIO.output(5,GPIO.HIGH)
time.sleep(2)
GPIO.output(5,GPIO.LOW)
GPIO.output(7,GPIO.HIGH)
time.sleep(5)

GPIO.output(3,GPIO.LOW)
GPIO.output(5,GPIO.LOW)
GPIO.output(7,GPIO.LOW)






Comments

First Pi GPIO project

Philip’s first project using the Raspberry Pi and the GPIO to control a LED.

This project is based on a recipe card available
here.

After buying our second Pi last week we were keen to start a project with components connected to the GPIO.


flashing LED 1

The code is relatively simple and the circuit basically involves connected a LED with resistor to the Pi GPIO breakout board (available here)


Once we had the basic code written and circuit created I asked Philip if he could adapt the code to:

- select how many flashes the LED will do
- the ‘on’ duration
- the ‘off’ duration

With the final code written Philip was very keen to show us code and flashing LED.

In comparison to the world of GTA5 a flashing LED seems very dull - but this little exercise of building and coding was very well received!

The final code is included below:

import time
import RPi.GPIO as GPIO
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)


print 'Welcome to the flashing LED program by Spencer and Philip'

print ' '
print ' '

print 'We first need to decide how many flashes we need'

flash = input('How many flashes would you like: ')

on = input ("how fast do you want the flashes to atay on in seconds ")

off = input ("how long do you want it to stay off for? ")

number = 0


for number in range (0, flash):

GPIO.output(7,GPIO.HIGH)
time.sleep(on)

GPIO.output(7,GPIO.LOW)
time.sleep(off)



We have ordered a few more LEDs from CPC and are hoping to build traffic lights next weekend!





Comments