9

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

1
  • Thanks for sharing this, Milliways. Reviewers, please check the answers before you vote to close. Commented Oct 14, 2020 at 12:56

3 Answers 3

13

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.

NOTE This was posted in 2020 and is still applicable to Pi4 & earlier although some of the comments on gpiochip are out of date as the kernel drivers have been updated.

The Pi5 uses an external chip for GPIO which appears to break most of the existing GPIO libraries.


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.
Claims to work on all versions up to and including Pi4 and all versions of Debian up to and including Debian Buster 10 (but the web page refers to 26 pin IDE plug(sic) which hasn't been used since 2014).

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 was available for download but has now been removed (as has all the documentation).

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. As of 31 Oct 2023 this has been archived and is not being maintained.


pigpio is a library for the Raspberry which allows control of the GPIO.
pigpio works on all versions of the Pi available in September 2023. 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.

Bookworm includes 0.7.1~a4-1+b4.
The former Bullseye release was (0.7.0 Jul 21, 2019) and neither 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 was the default backend for gpiozero which provides extensive user friendly documentation and examples.
The Bookworm OS includes gpiozero 2.0 which replaced this by lgpio.


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 gpiozero GPIO Zero 2.0.1 is installed in Raspberry Pi OS Bookworm which among other changes replaced RPi.GPIO by lgpio.

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.

2

The pi-gpio dynamic C library to control GPIO is now at Release 1.4

Includes a socket interface to the pi-gpio dynamic C library to control Raspberry Pi GPIO.
This allows access to all functions without sudo and allows remote access.

Overlays

Includes python overlays with access to most functions
This is available with include pi_gpio or include kpwm for kernel PWM.

Socket access include pi_sock

Utilities

gpioStatus Display status of all GPIO
pi-gpiod A socket interface to pi_gpio

Installation

This can be downloaded with

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

It is recommended to install from a .deb file.
There are now 32 bit - armhf & 64 bit - arm64 .deb files which can be downloaded from https://github.com/Milliways2/pi-gpio/releases/latest
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

A fast dynamic C library to control Raspberry Pi GPIO channels
Supports all production models available in September 2023 / all SOC
NOTE does not run on Pi5/BCM2712

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

kernel PWM

I²C using kernel drivers

SPI using kernel drivers

Overlays

Includes python overlays with access to most functions
This is available with include pi_gpio or include kpwm for kernel PWM Socket access include pi_sock

Utilities

gpioStatus Display status of all GPIO
pi-gpiod A socket interface to pi_gpio

Extensions

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

Documentation

man pi-gpio will display detail of functions.

The python overlay with access to most functions is available with include pi_gpio.
The kernel PWM is in a separate moduleinclude pi_gpio.

To read the Python documentation run
python3 -m pydoc pi_gpio
python3 -m pydoc kpwm


The library also works on Ubuntu (tested on Ubuntu 22.04.1 LTS 64bit)

NOTE Users of pi-gpio or 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.

1

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

I have since written a new dynamic library in C and a Python overlay pi-gpio & pi_gpio which makes this obsolete.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.