Tuesday 20 November 2012

Raspberry Pi - Getting an LED to Flash

Anyway I decided to have a go and see what I could do with the Raspberry Pi's GPIO, I like the idea of controlling physical things through software, so with virtually no experience of electronics (Im a software guy) I decided to start small - could I make an LED flash!


Completing this incredibly simple task has meant I have had to learn some new things, gain a load of new skills and buy a load of stuff.

Other than a raspberry Pi, I literally had nothing, so to do this I had to get:
  • Soldering iron kit - £9.99 (reduced from £25.99 - bargain!) - Maplin.co.uk
  • Raspberry Pi GPIO Breakout Kit - £2.40 - HobbyTronics.co.uk
  • Raspberry Pi GPIO 6" cable - £2.40
  • 400 point Breadboard - £2.99
  • 30 piece Breadboard Jumper Wire Kit - £2.39 
  • 3mm Red LED / resistor combo (pack 10) - £0.95 
I had to solder the breakout board, what a pain that was, before I could build up my simple circuit.

GPIO -> Resistor -> LED -> Ground

Which looked like this.


I then wrote a small python program (using the excellent RPi.GPIO module), which set up the gpio pins and started an loop which set the value of the led to on and off, I used a try/except to capture the keyboard interrupt (ctrl c) so I could clean up before the program exited.  I notice from looking online that most people don't perform the clean up at the end, its important you do otherwise you leave the gpio in same state it was when the program finished.

nano flashled.py

import RPi.GPIO as GPIO
import time

# set gpio mode to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
# setup gpio pin 4 as output
GPIO.setup(4, GPIO.OUT)

ledOn = True
# continue until a keyboard interupt is fired
# i.e. user presses CTRL C!
try:
        while True:
                # set gpio output
                GPIO.output(4, ledOn)
                # wait
                time.sleep(1)
                # switch led
                if ledOn == True: ledOn = False
                else: ledOn = True
except KeyboardInterrupt:
        # cleanup GPIO
        GPIO.cleanup()

Run the program, which you have to do with sudo otherwise you dont have sufficient permission to  access the gpio.

sudo python flashled.py

3 comments:

  1. You could probably do something in minecraft like if the player was standing on the block the led would turn on.

    ReplyDelete

Note: only a member of this blog may post a comment.