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

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!

Leave a Reply

Your email address will not be published. Required fields are marked *