3

I was searching for C and Python Libraries which support Pi4.

Milliways
  • 54,718
  • 26
  • 92
  • 182

3 Answers3

3

The BCM2711 SOC in the Pi4 has a new Peripheral implementation (all previous SOC had the same Peripheral implementation) which provides additional functionality. This includes the original functionality as a subset (although there are some incompatibilities e.g. pull-up setting).

While attempting to access this additional functionality I studied existing c and Python libraries and thought others may be interested in my conclusions.


BCM2835 is probably the oldest - dating from 2012.
This is a C library which provides access to GPIO and other IO functions on the Broadcom BCM 2835 chip.

It provides functions for reading digital inputs and setting digital outputs, using SPI and I2C, and for accessing the system timers.
Works on all versions up to and including Pi4. Works with all versions of Debian up to and including Debian Buster 10.

This library consists of a single non-shared library and header file which must be installed by the user.


WiringPi is a PIN based GPIO access library written in C for the BCM2835, BCM2836 and BCM2837 SoC devices used in all Raspberry Pi. WiringPi is a shared library designed for use with C and RTB (BASIC) ON THE Raspberry Pi ONLY.

There is extensive on-line documentation for the API in the library.

It was initially released in 2013.
WiringPi was included in Raspberry Pi OS (not Bullseye), although the version (2.50) currently included does not support Pi4.
There is an update to 2.52 for the Raspberry Pi4, dated June 24 2019 which is available for download.

WiringPi was deprecated on August 6 2019 (the Blog post announcing this has been deleted) and there is no support for additional functionality on the Pi4.
WiringPi is not available in Raspberry Pi OS - Bullseye.

There is an unofficial mirror/fork of wiringPi which is a mirror of the last "official" source release, plus a fork facilitating updates to support newer hardware.


pigpio is a library for the Raspberry which allows control of the GPIO.
pigpio works on all versions of the Pi but at the moment pigpio on the Pi4B is experimental. It is included in Raspberry Pi OS.

The pigpio library is written in C and the pigpio daemon offers a socket and pipe interface to the underlying C library.

pigpio is probably the most capable library for the Raspberry Pi and offers a number of tools and enhancements.

The C library is not conventionally accessible. Your C program actually becomes the pigpio daemon when you run it, effectively blocking access to other programs.

A C library and a Python module allow control of the GPIO via the pigpio daemon and this is highly functional.

There is third party support for a number of other languages.


RPi.GPIO provides a Python class to control the GPIO on a Raspberry Pi. The current release (0.7.0 Jul 21, 2019) does not support SPI, I2C, hardware PWM or serial functionality. Software PWM is available to use on all channels.

The implementation is Python with embedded C functions which provide most of the functionality.

Despite its apparently limited scope this is probably the most popular package for controlling GPIO. It is the default backend for gpiozero which provides extensive user friendly documentation and examples.


Pi.GPIO is an enhanced version of RPi.GPIO with additions:-

read_gpio Returns the GPIO level. Returns HIGH=1=True or LOW=0=False
get_alt Return the current GPIO mode (0-7)
get_pullupdn Return the current GPIO pullup pulldown on Pi4

It also recognises CM4 & Pi400 (and should report any future models as "??")

Version 0.7.2 Adds support for PAD drive,hysteresis & slew settings
Adds hardware PWM support

This can be downloaded using
git clone https://github.com/Milliways2/Pi.GPIO.git


Missing from my list was GPIO Zero by Ben Nuttall Raspberry Pi Community Manager.

GPIO Zero started out as a friendly API on top of the RPi.GPIO library, but extended to allow other pin libraries to be used. The pigpio library is supported, and that includes the ability to remotely control GPIO pins over the network, or on a Pi Zero over USB.

GPIO Zero: a friendly Python API for physical computing
Updates to GPIO Zero
GPIO Zero v1.5 is here!


pi-gpio A dynamic C library to control Raspberry Pi GPIO channels This has equivalent functionality to Pi.GPIO (events excluded).
See Answer below for detail.


lgpio is a new C library for Linux Single Board Computers (including the Pi) which allows control of the GPIO.
rgpio allows remote control of the GPIO on systems running the rgpiod daemon.

See https://elinux.org/C for an overview

These are based on gpiochip the new GPIO user space char device interface introduced with Linux 4.8 which is intended to replace the Sysfs interface /sys/class/gpio

There are Python wrappers for the libraries.

There are limitations with the interface, which are shared by the libraries.
The interface has no knowledge of the ACTUAL function of GPIO pins.
Buster always shows default function; Bullseye is worse it doesn't even have this although both do report "user" for system programmed pins.


Command Line Tools has limited description of tools for accessing GPIO from Command Line.

Milliways
  • 54,718
  • 26
  • 92
  • 182
0

After studying the available options I decided to enhance RPi.GPIO.

If I was 20 years younger I would write a new dynamic library in C and a Python overlay, but a few simple changes to RPi.GPIO met my needs.

Version 0.7.1 is compatible with RPi.GPIO with 3 additions:-

`read_gpio` Returns the GPIO level.  Returns HIGH=1=True or LOW=0=False
`get_alt` Return the current GPIO mode (0-7)
`get_pullupdn`  Return the current GPIO pullup pulldown on Pi4

Version 0.7.2

Adds support for PAD drive,hysteresis & slew settings
Adds hardware PWM support
Fix for gcc version 10 & Python 3.9
Adds Pi400, CM4, 8GB to cpuinfo
Changed /dev/mem is used if started with root permission


This can be downloaded :-

git clone https://github.com/Milliways2/Pi.GPIO.git

or updated:- git pull

And installed with

sudo apt install python-dev python3-dev
sudo python3 setup.py install

There is a version of my gpioread program, which shows the current GPIO pullup pulldown on Pi4.

This is a refactored version of the original, which imports gpioread.py rather than duplicating code. gpioread and gpioread.py can be downloaded from https://github.com/Milliways2/gpioread

Milliways
  • 54,718
  • 26
  • 92
  • 182
0

Despite enhancing RPi.GPIO to meet my immediate needs for Python code, I still wanted a dynamic C library to control GPIO.

Some months later I have a relatively complete library which continues to be enhanced.

Installation

This can be downloaded with

git clone https://github.com/Milliways2/pi-gpio.git

And installed with

./build

There are now .deb files 32 bit - armhf and 64 bit - arm64 OS that can be downloaded and installed with sudo dpkg -i deb_file.
For those using the .deb files the examples can be downloaded.


Functionality Supported

pi-gpio A dynamic C library to control Raspberry Pi GPIO channels

Supports all production models / all SoC

This library has code to:-

Read Raspberry Pi Information

Perform basic GPIO functions
• Set gpio as input or output
• Read/Write gpio
• Set pullup/down of gpio
• Read the current gpio mode
• Read gpio pull/up down (Pi4/BCM2711 only)

Software PWM on all pins

Hardware PWM support

Read/Set PAD drive, hysteresis & slew settings

I²C using kernel drivers

SPI using kernel drivers

Extensions

GPIO access routines for MCP23017 16-Bit I/O Expander


The library also works on Ubuntu (tested on Ubuntu 22.04.1 LTS 64bit)
NOTE Users of pi-gpio programs on Raspberry Pi OS should be members of group gpio (Ubuntu group dialout). If error message "Pi Setup failure" the most likely cause is that user does not have access.

man pi-gpio will display detail of functions.

Milliways
  • 54,718
  • 26
  • 92
  • 182