Access the Raspberry Pi's GPIO
Pig Pen
The wiringPi library, which many Raspberry Pi fans have grown attached to over the years, is no longer under maintenance by its developer. An alternative, in the form of Pigpio, has arrived just in time.
The Two Sides of Open Source
The open source community makes many software projects available to the general public. Open source thrives on enthusiasts who develop projects in their spare time. All too often, however, it happens that these developers do not get any recognition for their work. On the contrary, many people expect developers to provide the kind of professional support you would want from a corporate vendor, and people are even insulted if bugs are not fixed immediately. As a result, projects repeatedly disappear because developers no longer want to work under these conditions.
Another phenomenon relates to developers who upload a library or tool to GitHub and simply leave it orphaned after some time. Usually, it takes some time to notice that a particular library no longer works with the current version of the underlying programming language. I have often stumbled across Python libraries that developers never ported to Python 3. Oddly enough, the people responsible often don't remove their outdated projects. To make matters worse, you also find a plethora of documentation that no longer works. Especially with beginners, for whom the Raspberry Pi is the first step into the world of programming, this outdated detritus often causes people to drop the small-board computer (SBC) in frustration.
For as long as the Raspberry Pi has been around, wiringPi has served as a library for accessing the GPIO. With the related gpio
tool, programmers could quickly manage the GPIO at the command line. Many Raspberry Pi projects build on this library.
Not least because of frustration about what were in part rude email communications from some users, developer Gordon Henderson decided to discontinue his one-man wiringPi project in August 2019 after releasing his last version. He explained in great detail on his website why he had stopped developing the library. The post, which has since been deleted, is still available on the Wayback Machine internet archive [1]. For more information, see the box "The Two Sides of Open Source."
If you want to continue using the Raspberry Pi's GPIO, you need to look for an alternative to wiringPi. The successor is Pigpio, which also reads the Raspberry Pi's GPIO but uses a daemon to do so. At first glance, the Pigpio library seems to offer everything you might need for your projects. In this article, I take a closer look at the library to see if this first impression is correct.
Pigpio
Much like wiringPi, Pigpio lets you access the GPIO of the Raspberry Pi. However, you need to be aware of some differences. The first thing to notice about the pigpio library is that it requires a running daemon to work. On the one hand, the daemon continuously consumes a bit of the Raspberry Pi's CPU time, which is not a problem for most projects. On the other hand, a daemon has some advantages – more on that later.
Both wiringPi and Pigpio are based on C code. Similar to wiringPi, Pigpio has a tool to access the GPIO ports at the command line. Unlike wiringPi, though, it uses the BCM pin designations (Figure 1), which although not a genuine problem, does require some getting used to.
Wrappers for Java, Node.js, Ruby, and Perl, among others, help integrate the library into different programming languages. The Python module is part of the library itself. The complete documentation of the library with many examples can be found on the project website [2].
Installation
Pigpio installs to Raspberry Pi OS with the commands:
$ sudo apt update $ sudo apt upgrade $ sudo apt install pigpio
The commands you need to start, stop, and enable daemon autostart on boot are:
$ sudo systemctl start pigpiod $ sudo systemctl stop pigpiod $ sudo systemctl enable pigpiod
The daemon expects a number of parameters at startup. An overview, including a description, can be found on the Pigpio homepage [3]. To change the parameters of the daemon permanently, make the appropriate changes in the systemd service unit (/lib/systemd/system/pigpiod.service
). The most common parameter change needed is in the line that disables the remote socket interface by default:
ExecStart=/usr/bin/pigpiod -l
Just remove the -l
switch to get autostart to work.
Pigs
The pigs
command-line utility is a simple tool to access the GPIO through the daemon. Simple functions like digital in and out can be accessed, but you can also access I2C, SPI, UART, and the complete range of PWM functions. Myriad parameters let it address all the functions of each GPIO port. Listing 1 shows some sample commands that illustrate how pigs
works in principle. An overview with all the parameters can be found on the project website [4].
Listing 1
Pigs Functions
### Set GPIO10 to 1 $ pigs w 10 1 ### Set GPIO10 to 0 $ pigs w 10 0 ### PWM base frequency fr ### Set GPIO4 to 8 kHz $ pigs pfs 4 8000 ### Set GPIO4 PWM to 0% $ pigs p 4 0 $ pigs p 4 0 ### Set GPIO4 PWM to 50% $ pigs p 4 $ pigs p 4 127 ### Set GPIO4 PWM to 100% $ pigs p 4 $ pigs p 4 255@KE:
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
First Release Candidate for Linux Kernel 6.14 Now Available
Linus Torvalds has officially released the first release candidate for kernel 6.14 and it includes over 500,000 lines of modified code, making for a small release.
-
System76 Refreshes Meerkat Mini PC
If you're looking for a small form factor PC powered by Linux, System76 has exactly what you need in the Meerkat mini PC.
-
Gnome 48 Alpha Ready for Testing
The latest Gnome desktop alpha is now available with plenty of new features and improvements.
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.