Python libraries – RPi.GPIO

I’m starting new series today. I will show, describe and compare GPIO Python libraries. I’m starting with the most popular: RPi.GPIO

RPi.GPIO summary

  • Author: Ben Croston
  • License: Mit
  • PWM type: Software
  • Available on PyPi: Yes
  • Python 2 support: Yes
  • Python 3 support: Yes
  • Documentation: Good
  • Source hosting: Sourceforge

How to compare GPIO libraries? How to compare any libraries? The answer is easy, by comparing the code. Some other things are also important. On the left you can see some basic information about library:

Author. It’s good to know who wrote the code.

License says what can you do with your code, where can you use it. In python world most popular licences are from Apache, MIT and BSD family.

PWM type may or may not be important for you. If you want to drive a Servo or DC motor it’s very important.

Availability on PyPi is important when you need never version, not available in you distribution and for some reason you can not compile it by yourself. It’s also easier to use it with virtualenv.

Python 2 and Python 3 support. Yes, we have two not compatible versions of python so it’s nice when you can use GPIO library with both of them.

Documentation, especially examples are very important for beginners, documentation of API is very useful for more advanced users.

At the end there is a source hosting. In python world Github and Bitbucket are the most popular so people know how to use this sites.

To compare the libraries I will show the code for following mini projects:

  • Enabling/disabling LED in a loop
  • Reading button state
  • Rotating servo motor
  • Changing LED brightness
  • Changing DC motor speed.

RPi.GPIO

Ok, let’s start with the first GPIO library.

According to PyPi ranking, RPi.GPIO is the most popular library on the PyPi. What’s funny is that the library from the second place is a dead project… Let’s see the code.

Enabling/disabling LED

Reading button state

Rotating servo motor

LED brightness and DC motor speed

To control LED brightness or motor speed you can use the same code

Summary

RPi.GPIO is more than enough for any beginner. It’s very easy to use. You just configure the pin and use it. I didn’t show it here but it’s very easy to work with inputs. You can only react to failing edge or to rising. You can create callbacks which will be called when input changes and you can set default value using pull up and pull down resistors. PWM is also very easy to use, unfortunately the signal may be a little shaky. At the end of the series I will show this on video where driving servo motor is not that smooth. The problem is lack of support for UART, SPI and I2C. Someone may say that it’s not required but when you are done with basic GPIO operations you need to start looking for another library which you can use.

That is all for today. On next Tuesday I will show Wiring Pi library.

  • Pingback: Python libraries – Wiring Pi |()

  • Pingback: Python libraries – pigpio |()

  • Rajat Kukar

    Hi I have copied exactly ur code for Enabling/disabling LED. but LED is not blinking. Circuit is good because when I connect it to Multi meter it lights up. I am beginner. Can you help with debugging what the issue could be with my setup.

    I am using GPIO.setmode(GPIO.BOARD) and connecting + with Pin 7 and – with Pin 6.

    • Hi Rajat,
      pin numbers are different when you are using GPIO.BOARD and different when you are using GPIO.BCM. Please make sure that you are using correct pin numbers.

      Can you describe how your circuit is built?

      • Rajat Kukar

        When I use the GPIO.BOARD I use the board pin # and when I try with BCM I put + in 12 and refer to as Pin 18 in the code. circuit is simple. 220 ohms resister and LED in series. Pin 12 connecting to + of the LED side .

        • Rajat Kukar

          I even tried keeping the LED on all the time. it does not work. Is there is way to debug / get status of pin or to see which Pin is High or low?

        • How are you connecting it? You need connect it to one GPIO pin and GND. It should look like this:
          pin -> resistor -> LED -> GND. Also please remember that LED has to be correctly connected. Shorter leg should be connected to GND

Next ArticlePython libraries – Wiring Pi