Raspberry PI GPIO¶
- RPi docs: GPIO and the 40-pin header
- RPi A+/B+, Pi ⅔/4 pin numbers and GPIO numbers
Libraries¶
Legacy:
- WiringPi
- "Due to the underlying hardware changes on Raspberry Pi 5, Wiring Pi does not yet work on this device. For this reason, it is not yet recommended for new projects. Wiring Pi is actively developed, so more recent versions may work on Raspberry Pi 5."
- pigpio
- Last commit 2024
- pigpiod
- A daemon/resident version of pigpio which accepts commands from pipe and socket interfaces
- lgpio
- Last release 2024-03, no gti repo found
- Same author as
pigpioandpigpiod
- RPi.GPIO
- Last release 2022
- Mock.GPIO Mock Library for RPI.GPIO python Library
Current libraries:
- libgpiod: see below
- gpiozero
- GitHub
- Last tag 2023-02
pinctrlcommand- Display all the GPIO chips on a Raspberry Pi 5:
pinctrl get 26-42
- Display all the GPIO chips on a Raspberry Pi 5:
libgpiod¶
There are two versions of libgpiod, version 1 and version 2. At present, Raspberry Pi OS includes the version 1 library, so you should take that into account when using the various tools as their behaviour does change between versions.
Setup¶
By default, libgpiod follows the Linux ethos of taking control of a peripheral, allowing the peripheral to be used, and, when the process ends, returning the peripheral to its previous state. This means that there is no guarantee that when a program to set a GPIO to a particular value exits, the value will remain as set. It may or may not be returned to the state from before the set program was run. In order to make the move from the persistent operations of the previous sysfs to libgpiod easier, the Raspberry Pi kernel modifies this behaviour so that the state of a GPIO IS retained when a program that has changed the value (e.g. gpioset, see below) exits.
This means, without the following modification a LED turned on with i.e.
gpioset --mode time --sec 3 gpiochip0 17=1 will not reliably turn off
after the command exits.
Enable the automatic reversion behaviour of libgpiod by adding the following to the config.txt file:
CLI usage¶
General info:
Find GPIO:
Get state of pin 21:
Monitor state of pin:
Set GPIO3 high for three seconds, then return to default:
Python bindings¶
- gpiod
- Pypi: gpiod
- Debian package: gpiod
- Pre-installed on Raspberry Pi OS
Tutorials: