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
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.