Raspberry Pi

How to Install and Setup RPi.GPIO Library on Raspberry Pi

The Raspberry Pi GPIO is something that makes the machine special and if you want to directly talk with the circuit, you certainly need GPIO pins for that case. Handling the GPIO Pins in the Raspberry Pi is a hard nut to crack. However, thankfully, you won’t find any difficulty in handling these pins now because of the presence of Python libraries which are available now to make your complex task easy.

In this article, you will be guided on how you can use these pins by importing the libraries of GPIO on your Python code.

RPI GPIO library

Here, you will get to know how to import RPI GPIO library if you want to use your GPIO pins to take input and output from your Raspberry Pi device. In order to access the GPIO pins, you will be required to do the below steps correctly otherwise it will become difficult for you to access the pins.

Installing RPi.GPIO Library on Raspberry Pi from Repository

In order to install RPI GPIO library on Raspberry Pi, you will be required to first update the packages on your Raspberry Pi. For that, enter the below given command line in the terminal.

$ sudo apt-get update


After performing the packages update, you will now be ready to install the RPI GPIO library on your Raspberry Pi through the below given command line.

$ sudo apt-get -y install python3-rpi-gpio

After a few seconds, you will get confirmation that the RPI GPIO library is already installed or if not it will successfully install on your Raspberry Pi device.

Manual Installation of RPI.GPIO Library

If the above method is not working on your Raspberry Pi, then you will also install the RPI GPIO library through the Python library and below are the steps which are required to perform manual installation of the library.

Step 1: First, you will be required to download the library by entering the below given command into the terminal of your Raspberry Pi device.

$ wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz



Step 2: In the next step, you will then need to extract the RPI GPIO archive to a new folder by adding the below command in the terminal.

$ tar -xvf RPi.GPIO-0.5.11.tar.gz


Step 3: Next, you will have to browse to the created new directory though the below given command.

$ cd RPi.GPIO-0.5.11


Step 4: After successfully performing the above steps, you are now ready to install the library using the below given command.

$ sudo python setup.py install


Step 5: After the successful installation of the library, you can then remove the created directory and the archive file to free up your SD card space. The below mentioned command will do it for you.

$ cd ~

$ sudo rm -rf RPi.GPIO-0.*


After the above steps, you are ready to start importing the RPI.GPIO library and when you are writing a python program on your Raspberry Pi device, you will need to import RPI. The below mentioned Python code is an example of use of GPIO library.

import RPi.GPIO as GPIOfrom time import sleep

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

GPIO.setup(18,GPIO.OUT)

while True:

    GPIO.output(18,GPIO.HIGH)

    print ("LED ON")

    sleep(1)

    GPIO.output(18,GPIO.LOW)

    print ("LED OFF")

    sleep(1)


The above code is used to blink the LED with the delay of 1 second using GPIO library. Save the above program as “myfile.py” and to run the above code use the command:

$ python myfile.py

After running the code in the terminal you will be able to see a blinking LED. Below are the image and the videos of LED blinking.

Conclusion

You can build your projects with the help of Raspberry Pi GPIO pins and in order to enable these pins you are required to install the RPI GPIO library and the above steps will help you to use the GPIO pins. It is entirely up to you whether to go with the repository method or to follow the manual installation method, the only matter is the successful installation of the GPIO library so that you may be able to import in your Python code and can configure them later on for your projects.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.