Access the Raspberry Pi's GPIO

Pig Pen

© Lead Image © evgeny turaev, 123RF.com

© Lead Image © evgeny turaev, 123RF.com

Article from Issue 263/2022
Author(s):

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.

Figure 1: Pigpio uses the BCM designations of the GPIO pins instead of the old wiringPi numbering system.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Julia on the Pi

    Create GUIs and a web app that connects to sensors.

  • RFID over SPI

    Inexpensive components for the SPI interface let you upgrade a Raspberry Pi 4 to a display system for zero-contact RFID-based data acquisition.

  • Whiptail

    Whiptail interfaces add menus and information pages to your Raspberry Pi projects.

  • Kitchen Timer

    A simple kitchen helper with two timers assists budding chefs in coping with dishes that are unlikely to be ready at the same time.

  • ARM64 Assembly and GPIO

    Reading, writing, and arithmetic with the Raspberry Pi in ARM64 assembly language.

comments powered by Disqus
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.

Learn More

News