Working with GPIOs on a Raspberry Pi in 2025
When working with GPIOs on Raspberry Pis, you usually
- find a library like
gpiozero, - pick a backend/middleware (
RPI,pigpio,sysfs, etc) - go on your merry way.
pigpio does everything I'd want, even remotely.
I would start a pigpio addon for godot and add support to its socket API for me needs. Things is,
it's so low level that it fiddles with memory that doesn't belong to it and hardware changes
made in Raspberri 5 make it incompatible unless a large rewrite is done, which doesn't seem like
it will ever happen. I'm using a Raspberri zero though, so for me it's fine just using pigpio.
But writing a godot wrapper seems kinda pointless since it won't support newer devices,
while integrating with recent linux kernel APIs would be future-proof, provided they
reached good old feature parity with obsoleted drivers & frameworks.
A few points:
- the legacy sysfs driver has been deprecated/obsoleted for a while. Sergio Prado [explains it well]
(https://sergioprado.blog/new-linux-kernel-gpio-user-space-interface/).
- libgpiod is the new way. Their rationale
section is an interesting summary:
The new character device interface guarantees all allocated resources are freed after closing the device file descriptor and adds several new features that are not present in the obsolete sysfs interface (like event polling, setting/reading multiple values at once or open-source and open-drain GPIOs).
Unfortunately interacting with the linux device file can no longer be done using only standard command-line tools. This is the reason for creating a library encapsulating the cumbersome, ioctl-based kernel-userspace interaction in a set of convenient functions and opaque data structures.
Libgpiod doesn't support PWM. This has been implemented in its own interface. Here's a digestible introduction.
Ideal Way Forward [1]
[1] if I had all the time in the world.
Develop an addon that:
1. integrates a libgpiod wrapper. Good contenders are
Dimitry Ishenko's gpio or wiringPi)
2. integrates a pwm API lib. WiringPi only does software PWM
and seems like it's been deprecated for a while too. lgpio seems promising:
By the same author as
pigpio,lgpiois a more general-purpose library than 'pigpio', and will run on other SBCs and Linux devices. It uses the /dev/gpiochip device kernel interface rather than accessing registers directly. The library is written in C, but there is also a Python library and a version that provides remote control of GPIOs using therpgioddaemon in both C and Python. (PDF source)
At the end of the day, lpgio seems like a future-proof path forward:
flowchart LR
subgraph Pi
subgraph rgpiod
lgpio["lgpio/libgpio"]
end
subgraph kernel
chip["gpio iface"]
end
end
lgpio --> chip
subgraph remote[Remote Host]
subgraph rgs
rgpio
end
end
rgpio --> rgpiod
Ideally I'd write a c++ Godot extension to make a rpgio wrapper addon, and use
it either in the server or even the client (I don't think latency for a direct
ethernet connection will be an issue).
Concrete Way Forward
To integrate lgpio as fast as possible, I'll rather wrap rgs and run shell commands,
which is not efficient (and a little ugly) but will let me iterate
to a first working prototype in no time.