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.


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


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.

Next ArticlePython libraries – Wiring Pi