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.
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.
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.
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()

cant get the code to run. this is supposed to be a .py?
ReplyDeletei am running the 512 version. does that matter?
yes the code need to be saved as filename.py
Deleteversion of pi doesn't matter.
This comment has been removed by the author.
ReplyDeleteTested again and the code seems to work fine on both of my RPi. Use the view plain option and then copy the code.
DeleteI 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?
ReplyDeletetry running the script as sudo python led_blink.py
DeleteRemember you must be root to write to the gpio bus. (I believe)
DeleteI 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.
DeleteIs there visual GUI for this?
ReplyDeleteHi,
ReplyDeleteI'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.
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 .
Deletehow would you run this program in the backround after pi boots....would u add somthing like python flashingled.pi to startup script?
ReplyDeletetrying to get a sequence of flashing lights to run in backround so raspberry pi looks like Orac : )
I have done this and it works, but the light is very dim, what can i do to make it more brighter?
ReplyDeleteUse a smaller resistor :) The higher the resistance the dimmer the light, But dont just not use one as it can blow the LED
Deletetoo much tanks sir,
ReplyDeleteonly your kod works... now we are working... 2 days we couldnt do but now work :)
LED is just flashing instead of blinking. It seems that GPIO.output(pin,GPIO.HIGH) does not Keep the pin on HIGH.
ReplyDeleteWhat am I missing?
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/
ReplyDeleteYour 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!
ReplyDeleteCool tutorial for a beginner (y)
ReplyDeleteThank You Rahul Kar
Worked for me. THanks.
ReplyDeleteWorked for me. Thanks.
ReplyDeleteWorked perfectly first time - thanks for this, it was a great test script for my first adventure with the Pi and LEDs!
ReplyDeleteSmall 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:
ReplyDeletesudo apt-get install python2.7-dev
from there the rest was smooth sailing for me. Thanks for the tutorial!
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.
ReplyDeleteExecute 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!