Sunday, December 22, 2013

Raspberry pi LED Blinker

Ever since I came to Malaysia my inventive side is becoming more and more sharper. Mainly because of the Electronic components available easily in Malaysia for affordable prices. I bought a myself a Raspberry pi Model B with a Transparent casing + 8 Gb SSD Card with Preloaded Noobs for just MYR 199 (USD 60.53).

Furthermore I went to an area called Jalan Pasar in Pudu where there are lots and lots of Electronic shops which sells all sorts of components. I am waiting to do more hacks and hobby projects in the future but to start off I did a small LED Blinker project using Raspberry pi.

The following steps needs to be carried out to Run the LED Blinker in Raspberry pi.
  • Login to Raspberry pi
  • Open a LXTerminal window
  • Download the python GPIO library
wget http://raspberry-gpio-python.googlecode.com/files/RPi.GPIO-0.4.1a.tar.gz



  • Untar the Tar file

  • tar zxvf RPi.GPIO-0.4.1a.tar.gz



  • Go into untared directory

  • cd RPi.GPIO-0.4.1a



  • Install GPIO for python

  • sudo python setup.py install

    If you get errors during the installation process then you must first do the following


    source/py_gpio.c:23:20: fatal error: Python.h: No such file or directory
    Compilation terminated.
    error: command 'gcc' failed with exit status 1
    
    • Run apt-get update
    sudo apt-get update
    
    • The install python dev (Debian)
    sudo apt-get install python-dev
    

    • Pidora 

    sudo yum -y install python-devel

    After that do the following wiring where an LED ground is connected using a 1K Resistor to the Raspberry pi GND pin 6. And pin 7 GPIO 4 is connected to the LED Positive.


    Then copy the following code to a text editor and save it as blink_led.py

    import RPi.GPIO as GPIO 
    import time 
    import threading
    
    GPIO.setmode(GPIO.BOARD) 
    GPIO.setup(7, GPIO.OUT) 
    
    class Blinker(threading.Thread):
        def __init__(self, speed, times):    
            threading.Thread.__init__(self);
            self.speed = speed;    
            self.times = times;
    
        def run(self):
            for i in range(0,self.times):
                print "Time : %d" % ((i+1))
                GPIO.output(7,True)
                time.sleep(self.speed)
                GPIO.output(7,False)
                time.sleep(self.speed)
            print "Blinking Complete!" 
            GPIO.cleanup()    
    
    blinker = Blinker(1, 10)
    blinker.start()
    
    • Finally close the LXTerminal and open a new one and execute the following
    sudo python blink_led.py
    

    Everything working together


    References
    • http://www.thirdeyevis.com/pi-page-1.php#gpio-setup
    • http://www.thirdeyevis.com/pi-page-2.php
    • http://www.raspberrypi.org/phpBB3/viewtopic.php?f=31&t=13003