Blink an LED using GPIO port on Raspberry Pi

One┬áthing that separates the Pi from other the rest of Single Board Computer is its ability to use the GPIO (General Purpose Input/Output) pins which can be switched as HIGH or LOW to control any external devices. All you will need is a GPIO breakout board and a few jumper cable to begin with. Here I’ve used a female-to-male connector to complete the job.

GPIO of raspberry piIn this tutorial I have used pin 9 for GND and pin 11 for GPIO17. The light emitting diode was linked using a 470 ohm register in series with pin 9 and 11 to restrict the current.

Software Implementation

The quickest way to get started is to make use of python which comes pre-installed in all the OS images. Obtain the RPi.GPIO library and copy the gz tar ball to your SD card. Open the terminal and browse to the extracted directory containing the RPi.GPIO library.

Then type: $ sudo python setup.py install to install the library.

Note: As the OS is multitasking and not Real-time unlike Arduino there may possibly be jitters depending on CPU load.

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

 

 

You can use nano text editor to write the code on the console or copy paste the code and save it as a blink.py file and upload the file to your raspberry pi

 

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

 

One you have the file saved just run the python code as follows

sudo python blink.py

 
blinking LED on raspberry pi

Leave a Reply