Using GPIO on Raspberry Pi to blink an LED

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. To get started with hardware equivalent of "Hello world", all you need is a female to male jumper wire along with an LED and a resistor. Make sure to check out the cool Starter Kit put together by CanaKit. It includes everything needed to get started using the GPIO port of the Raspberry Pi.

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.

GPIO of raspberry pi
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 50 times
for i in range(0,50):
        blink(11)
GPIO.cleanup() 

led blinking with raspberry pi           

24 comments

  1. cant get the code to run. this is supposed to be a .py?
    i am running the 512 version. does that matter?

    ReplyDelete
    Replies
    1. yes the code need to be saved as filename.py
      version of pi doesn't matter.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Tested again and the code seems to work fine on both of my RPi. Use the view plain option and then copy the code.

      Delete
  3. I copied out it exactly as you wrote and when I try to run, it gives "invalid syntax" and highlights "time" on line 06. What am I doing wrong?

    ReplyDelete
    Replies
    1. try running the script as sudo python led_blink.py

      Delete
    2. Remember you must be root to write to the gpio bus. (I believe)

      Delete
    3. I ran into the same problem, and it comes from trying to copy and paste the script from the internet. The spacing characters don't copy over to well, so just type it out yourself. Thats how I got mine to work. Also, when running it as an executable, be sure to title the script as #!/usr/bin/env python so whatever you use to run it knows where the interpreter is located.

      Delete
  4. Hi,

    I'm a little bit of a beginner on the RPi, but I have some experience with the Arduino. I was wondering if like the Arduino,I could use the Python program on my windows computer to port hardware commands to my RPi, or do I have to download a linux system? If I do have to download linux, how do I port the commands through there?

    Thanks.

    ReplyDelete
    Replies
    1. You have to download a Linux system (Noobs) after that you can connect to from your laptop to raspberry board e.g. using SSH and from there you can directly program it .

      Delete
  5. how would you run this program in the backround after pi boots....would u add somthing like python flashingled.pi to startup script?

    trying to get a sequence of flashing lights to run in backround so raspberry pi looks like Orac : )

    ReplyDelete
  6. I have done this and it works, but the light is very dim, what can i do to make it more brighter?

    ReplyDelete
    Replies
    1. Use a smaller resistor :) The higher the resistance the dimmer the light, But dont just not use one as it can blow the LED

      Delete
  7. too much tanks sir,

    only your kod works... now we are working... 2 days we couldnt do but now work :)

    ReplyDelete
  8. LED is just flashing instead of blinking. It seems that GPIO.output(pin,GPIO.HIGH) does not Keep the pin on HIGH.
    What am I missing?

    ReplyDelete
  9. Very good tutorial for beginners that uses Raspberry Pi minicomputer. I add this tutorial in a series of tutorials from where the users can learn how to start working with this device. The article can be found here: http://www.intorobotics.com/18-excellent-tutorials-compilation-start-working-raspberry-pi/

    ReplyDelete
  10. Your tutorial about GPIO and Pi help me and others to get started with Pi. And because I want to help many more hobbyists to start building robots with Pi, I share this tutorial on my post http://www.intorobotics.com/18-excellent-tutorials-compilation-start-working-raspberry-pi/. Thank you!

    ReplyDelete
  11. Cool tutorial for a beginner (y)
    Thank You Rahul Kar

    ReplyDelete
  12. Worked perfectly first time - thanks for this, it was a great test script for my first adventure with the Pi and LEDs!

    ReplyDelete
  13. Small problem with python installing the RPI.GPIO, if you get an error when trying to install that includes fatal error: Python.h: No such file or directory compilation terminated do the following:

    sudo apt-get install python2.7-dev

    from there the rest was smooth sailing for me. Thanks for the tutorial!

    ReplyDelete
  14. This also was my first goal for the Pi. Thanks author for the well written script. Mine also error out on first attempt. If after launching Python shell, choose File New, then paste the text.
    Execute the script from the command shell in the directory the file was saved in- with the command: xxxx:¬$ python LEDblink.py, or whatever you gave your file name. Great starter project!

    ReplyDelete