F9HBe


Using GPIO on Raspberry Pi to blink an LED
Posted by Rahul Kar × 1:00 PM

  http://www.rpiblog.com/2012/09/using-gpio-of-raspberry-pi-to-blink-led.html

One of the few things that separates the Pi from other SBC (Single Board Computer)  is the ability to use the GPIO (General Purpose Input/Output) pins which can be set as HIGH or LOW to control any external devices. All you need is a female to male jumper wire to get started. Here I have used a HDD IDE connector to get the job done.

In this post pin 9 is used for GND and pin 11 for GPIO17. The LED was connected using a 470 ohm register in series with pin 9 and 11 to limit the current.

  3.3V       1 |  2  5V
  12C0 SDA   3 |  4  DNC
  12C0 SCL   5 |  6  GROUND
  GPIO4      7 |  8  UART TXD
  DNC        9 | 10  UART RXD
  GPIO17    11 | 12  GPIO18
  GPIO21    13 | 14  DNC
  GPIO22    15 | 16  GPIO23
  DNC       17 | 18  GPIO24
  SPIO MOSI 19 | 20  DNC
  SPIO MISO 21 | 22  GPIO25
  SPIO SCLK 23 | 24  SPIO CE0 N
  DNC       25 | 26  SPIO CE1 N

Software Implementation:

The fastest way to get started is to use python which comes pre-installed with all images. Download the RPi.GPIO library and copy the gz tar ball to the RPi wheezy raspbian. Open the terminal and navigate to the extracted folder containing the RPi.GPIO library. Then type:

  $ sudo python setup.py install

to install the module. Imp: As the OS is multitasking  and not Real-time unlike Arduino there may be jitters depending on CPU priority.

Based on the library I have written a simple code to turn ON and turn OFF the LED after a delay of 1 sec (1000ms) each. The LED blinks 50 times.

    import RPi.GPIO as GPIO
    import time

    # blinking function
    def blink(pin):
        GPIO.output( pin, GPIO.HIGH)
        time.sleep( 1)
        GPIO.output( pin, GPIO.LOW)
        time.sleep( 1)
        return

    # to use Raspberry Pi board pin numbers
    GPIO.setmode( GPIO.BOARD)

    # set up GPIO output channel
    GPIO.setup( 11, GPIO.OUT)

    # blink GPIO17 (on pin 11) 50 times
    for i in range( 0, 50):
        blink( 11)

    GPIO.cleanup()
