The Raspberry Pi is single board computer with now four revisions and a minimalistic zero variant. It is a popular choice for different projects because of its small size, efficient power consumption, processing speed and by being a full Linux based computer.
When you want to connect several microcontrollers with each other, several protocols exist: I2C, SIP, and UART. In my last two articles, I explained these protocols and showed examples for C++ libraries with Arduino and with the Raspberry Pi.
This article now presents general GPIO libraries for working with the Raspberry Pi. The libraries are Wiring Pi, Pigpio, Gpiozero, Rpi.GPIO. Each library is explained with a description, its main features, a code example on Python, and a code example in C if supported by the library. I assume you have a general understanding of GPIO programming, and will therefore not explicitly detail each source code line in the examples.
This article originally appeared at my blog admantium.com.
Wiring Pi
The WiringPi library is an often used, functional rich library that supports I2C and SPI connections. It is written in C and provides Python bindings. The library is unfortunately deprecated as the original author stated. Decide yourself if you want to use the library: On one hand, you find several examples on the internet, on the other hand, members from the Raspberry Pi community start to move to new libraries.
The libraries latest version can be accessed on Github: See the WiringPi C and WiringPi Python.
C
#include <stdlib.h>
#include <stdbool.h>
#include <wiringPi.h>
#define LED_PIN 6
int main()
{
pinMode(LED_PIN, OUTPUT);
while (true)
{
delay(1000);
digitalWrite(LED_PIN, 0);
delay(1050);
digitalWrite(LED_PIN, 1);
}
}
Python
import wiringpi
import time
ledPin = 6
wiringpi.wiringPiSetup()
wiringpi.pinMode(ledPin,1)
while(True):
wiringpi.digitalWrite(ledPin, 1)
time.sleep(1)
wiringpi.digitalWrite(ledPin, 0)
time.sleep(1)
Pigpio
The pigpio is an actively developed library with an impressive set of features: All GPIO pins of the Raspberry Pi can be read, written to, attached to interrupt handlers, and output PWM signals at the same time. Also, UART, I2C and SPI protocols are implemented. It is written in C and also provides Python bindings. Go to the official Github repository and build the newest version on your Raspberry Pi, or install it with apt install pigpio python-pigpio python3-pigpio
.
C
#include <stdlib.h>
#include <stdbool.h>
#include <pigpio.h>
#define LED_PIN 6
int main()
{
gpioInitialise()
gpioSetMode(LED_PIN, PI_OUTPUT);
while (true)
{
gpioDelay(100000);
gpioWrite(LED_PIN, 1);
gpioDelay(100000);
gpioWrite(LED_PIN, 0);
}
}
Python
import pigpio
import time
ledPin = 6
pi = pigpio.pi()
pi.set_mode(ledPin, pigpio.OUTPUT)
while (True):
pi.write(ledPin, True)
time.sleep(1)
pi.write(ledPin, False)
time.sleep(1)
Gpiozero
Officially endorsed by the Raspberry Foundation, gpiozero is a Python-only library that not only accesses the GPIO pins, but also provides direct hardware support for working with actuators and sensors. For connecting other devices, only SPI is supported - I2C was announced, but is not included yet.
Gpiozero is already installed in the current Raspberry Pi OS - if not, just run python3 -m pip gpiozero
. The source code files can be accessed on Github. The official documentation on readthedocs.io provides rich information and plenty example code.
Python
from gpiozero import LED
from time import sleep
led = LED("6")
while True:
led.on()
sleep(1)
led.off()
sleep(1)
Rpi.GPIO
The RPI.GPIO library is another Python-only library. It provide basic interactions with the GPIO pins, but no implementation of any connection protocol yet. The projects python files can be downloaded from Pypi.org, the projects home page is hosted on Scourceforge.
Python
import RPi.GPIO as GPIO
import time
ledPin= 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(ledPin , GPIO.OUT)
while True:
GPIO.output(ledPin , GPIO.HIGH)
time.sleep(1)
GPIO.output(ledPin , GPIO.LOW)
time.sleep(1)
Conclusion
This article presented four Raspberry Pi GPIO programming libraries: WiringPI, Pigpio, Gpiozero, RPI.GPIO. For each library, I explained the main feature, linked to their homepages and/or source code repositories, and gave a short example source code example in C and/or Python.
Latest comments (0)