Remote Control Power Sockets: Part 7: Setting Up a Raspberry Pi

Updated 2026: This part now uses libgpiod instead of the old GPIO library that only worked on Broadcom chips, cross-compilers from the distribution and Raspberry Pi OS Lite. The BeagleBone Black follows in Part 8 and librsswitch in Part 9.

Anyone who has read the first post of this series already knows what we want: to replace the remote control of these devices with a cheap 433 MHz transmitter and a small Linux computer. In this part we set up the first of the two target systems: a Raspberry Pi with Raspberry Pi OS Lite, up to a first program that toggles a GPIO pin. In Part 8 we do the same for the BeagleBone Black, and in Part 9 we send real codewords with librsswitch.

Host system (x86_64)

We cross-compile on a Debian 13 (Trixie) host, where Debian installs the ARM packages next to the native ones via multiarch. Alternatively, we build natively on the Pi (see "Building and running" below), which works from any host.

GPIO library (libgpiod)

The Linux kernel exposes every GPIO controller as a character device (/dev/gpiochip0, /dev/gpiochip1, ...). From userspace, a program can open the device and request individual lines with ioctl(). For this the kernel returns a separate file descriptor, through which the program sets and reads the lines. When the program closes the descriptor, explicitly or simply because it exits, the kernel releases the lines again. libgpiod wraps this ioctl interface in a C API and comes with the command line tools gpiodetect, gpioinfo and gpioset. The libgpiod API works the same on every board and replaces two older approaches: a library that mapped the GPIO registers of a single Broadcom chip directly into the program, and the sysfs interface under /sys/class/gpio, which the kernel has marked as deprecated.

The API of libgpiod 2.x is not compatible with version 1.x. Raspberry Pi OS Trixie (Debian 13) ships libgpiod 2.2.1, Debian 12 (Bookworm) still ships 1.6.3. The program toggle.c below is written for libgpiod 2.x.

Debian 13: multiarch

Cross toolchain

Cross-compiling means building the program on the fast host instead of on the Pi. Debian ships the compilers as packages: gcc-arm-linux-gnueabihf builds for a 32-bit Raspberry Pi OS and gcc-aarch64-linux-gnu for a 64-bit system.

sudo apt install make gcc-arm-linux-gnueabihf gcc-aarch64-linux-gnu

The armhf compiler of the distribution targets ARMv7. Programs built with it do not run on the ARMv6 boards Raspberry Pi 1, Zero and Zero W. For these boards we build natively on the Pi.

libgpiod for the target

To cross-compile, the libgpiod headers and libraries for the target architecture must be present on the host. Debian solves this with multiarch, which installs packages of a foreign architecture next to the native ones:

sudo dpkg --add-architecture armhf
sudo dpkg --add-architecture arm64
sudo apt update
sudo apt install libgpiod-dev:armhf libgpiod-dev:arm64

The following command shows which version a host would install:

apt-cache policy libgpiod-dev:armhf

The candidate version must start with 2, as on Debian 13. A host that only offers 1.x, for example Debian 12, cannot cross-compile toggle.c this way. In that case we build natively on the Pi.

Hello world program

The "hello world" of GPIO programming switches a pin on and off. The program toggle configures GPIO17 (pin 11 on the header) as an output and alternately drives it high and low for 1 ms each. On the oscilloscope we expect a square wave with a period of about 2 ms. GPIO17 is also the line that librsswitch uses by default for the transmitter in Part 9. Until then we leave the transmitter disconnected: with toggle running, it would send a meaningless square wave on 433 MHz into the neighbourhood. The GPIO outputs of the Raspberry Pi use 3.3 V logic levels.

toggle is written against the libgpiod 2.x API; librsswitch supports libgpiod 1.x and 2.x.

Makefile

The Makefile has one native target and two cross targets: make builds directly on the Pi, make clean cross produces a 32-bit ARM binary (arm-linux-gnueabihf-) and make clean cross64 a 64-bit binary (aarch64-linux-gnu-). The clean before a cross target is important, because make does not notice that a leftover toggle belongs to a different architecture. The recipe for toggle comes from make's built-in rule that turns toggle.c into toggle. That is why this Makefile gets by without a single line indented with a tab, and we can copy it from the page unchanged. Anyone who has ever copied a Makefile from a browser will appreciate that.

# Native build: make
# Cross build, 32-bit OS: make clean cross
# Cross build, 64-bit OS: make clean cross64
CROSS_COMPILE ?=
CC = $(CROSS_COMPILE)gcc
CFLAGS ?= -O2 -Wall -Wextra
LDLIBS = -lgpiod

# make's built-in rule turns toggle.c into toggle (CC, CFLAGS, LDLIBS)
toggle: toggle.c

# Target-specific values also apply to the prerequisite toggle
cross: CROSS_COMPILE = arm-linux-gnueabihf-
cross: toggle

cross64: CROSS_COMPILE = aarch64-linux-gnu-
cross64: toggle

clean: ; rm -f toggle

.PHONY: cross cross64 clean

toggle.c

As soon as we connect the transmitter to this pin in Part 9, the line must not stay high: a 433 MHz transmitter whose data pin stays high permanently sends an unbroken carrier and blocks the band. That is why toggle does not simply "die" on Ctrl-C: SIGINT and SIGTERM only set a flag, the loop ends, the program drives the line low, releases it first and then frees the configuration objects.

#include <errno.h>
#include <gpiod.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define CHIP "/dev/gpiochip0"
#define LINE 17 /* GPIO17, pin 11 on the header */

static volatile sig_atomic_t stop;

static void on_signal(int sig)
{
        (void)sig;
        stop = 1;
}

int main(void)
{
        struct gpiod_chip *chip;
        struct gpiod_line_settings *settings;
        struct gpiod_line_config *line_cfg;
        struct gpiod_request_config *req_cfg;
        struct gpiod_line_request *request = NULL;
        struct sigaction sa;
        unsigned int offset = LINE;
        int ret = EXIT_FAILURE;

        /* Ctrl-C and kill end the loop instead of killing the program */
        memset(&sa, 0, sizeof(sa));
        sa.sa_handler = on_signal;
        sigemptyset(&sa.sa_mask);
        sigaction(SIGINT, &sa, NULL);
        sigaction(SIGTERM, &sa, NULL);

        chip = gpiod_chip_open(CHIP);
        if (!chip) {
                fprintf(stderr, "cannot open %s: %s\n", CHIP, strerror(errno));
                return EXIT_FAILURE;
        }

        settings = gpiod_line_settings_new();
        line_cfg = gpiod_line_config_new();
        req_cfg = gpiod_request_config_new();
        if (!settings || !line_cfg || !req_cfg) {
                fprintf(stderr, "out of memory\n");
                goto out;
        }

        /* One output line, low until the loop starts */
        if (gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_OUTPUT) ||
            gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_INACTIVE) ||
            gpiod_line_config_add_line_settings(line_cfg, &offset, 1, settings)) {
                fprintf(stderr, "cannot configure line %u: %s\n", offset, strerror(errno));
                goto out;
        }
        gpiod_request_config_set_consumer(req_cfg, "toggle");

        request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
        if (!request) {
                fprintf(stderr, "cannot request line %u: %s\n", offset, strerror(errno));
                goto out;
        }

        while (!stop) {
                if (gpiod_line_request_set_value(request, offset, GPIOD_LINE_VALUE_ACTIVE))
                        break;
                usleep(1000);
                if (gpiod_line_request_set_value(request, offset, GPIOD_LINE_VALUE_INACTIVE))
                        break;
                usleep(1000);
        }
        if (stop)
                ret = EXIT_SUCCESS;
        else
                fprintf(stderr, "cannot set line %u: %s\n", offset, strerror(errno));

        /* Leave the line low, so a transmitter on it does not stay keyed */
        gpiod_line_request_set_value(request, offset, GPIOD_LINE_VALUE_INACTIVE);

out:
        gpiod_line_request_release(request);
        gpiod_request_config_free(req_cfg);
        gpiod_line_config_free(line_cfg);
        gpiod_line_settings_free(settings);
        gpiod_chip_close(chip);
        return ret;
}

All three targets need the libgpiod headers and library for the architecture they build for: on the host from the multiarch packages above, on the Pi from libgpiod-dev (see the target system further below). file shows whether the build worked:

file toggle

After make clean cross, the output starts with ELF 32-bit LSB and ARM, EABI5; after make clean cross64, with ELF 64-bit LSB and ARM aarch64.

Target system (Raspberry Pi OS Lite)

Installing the operating system

Raspberry Pi OS Lite is a Debian without a desktop, made specifically for the Raspberry Pi. With the Raspberry Pi Imager we write the image (Debian Trixie) to an SD card, 32-bit or 64-bit, depending on the board. In the Imager's customisation dialog we set the host name and a user, enter a Wi-Fi network if needed and enable SSH.

The first version of this post used a lean distribution, because the standard Linux kernel has no real-time capability. Raspberry Pi OS does not change that: Linux can interrupt the program at any time, and the oscilloscope shows these interruptions as jitter (see below). Part 9 shows how much this matters for the sockets.

After the installation we put the SD card into the Raspberry Pi, connect it to the Ethernet and power it on. If the host name chosen in the Imager does not resolve, we ask the router for the IP address or find it with nmap. Then we log in via SSH:

ssh user@raspberrypi.local

Tools and libgpiod

On the Pi we install the compiler, libgpiod including the header files and the libgpiod command line tools, and check which libgpiod version apt installed:

sudo apt update
sudo apt install build-essential libgpiod-dev gpiod
apt-cache policy libgpiod-dev

The installed version must start with 2 (2.2.1 on Trixie). If the release still ships libgpiod 1.x, the compiler aborts on toggle.c with implicit declaration of function 'gpiod_line_settings_new'.

The tools show which GPIOs the kernel offers: gpiodetect lists the GPIO chips, gpioinfo -c gpiochip0 all lines of the first chip, and gpioinfo GPIO17 looks up the line that toggle uses by its name.

gpiodetect
gpioinfo -c gpiochip0
gpioinfo GPIO17

On the Raspberry Pi 3 used here, the output looks like this (shortened):

$ gpiodetect
gpiochip0 [pinctrl-bcm2835] (54 lines)
gpiochip1 [brcmvirt-gpio] (2 lines)
gpiochip2 [raspberrypi-exp-gpio] (8 lines)
$ gpioinfo -c gpiochip0
gpiochip0 - 54 lines:
        line   0:      "ID_SDA"        input
        line   1:      "ID_SCL"        input
        ...
        line  17:      "GPIO17"        input
        ...
$ gpioinfo GPIO17
gpiochip0 17    "GPIO17"            input

Line 17 of gpiochip0 is called GPIO17; it is an input, and no program uses it yet. This is exactly the chip and line that toggle.c enters at CHIP and LINE.

Building and running

Natively on the Pi: we copy Makefile and toggle.c from the host to the Pi and build the program there.

ssh user@raspberrypi.local mkdir -p toggle
scp Makefile toggle.c user@raspberrypi.local:toggle/
ssh user@raspberrypi.local
cd toggle
make
./toggle

Cross-compiling on the host: we build for the architecture of the installed Raspberry Pi OS (make clean cross for 32-bit, make clean cross64 for 64-bit), copy the binary to the Pi and run it there.

make clean cross64
scp toggle user@raspberrypi.local:
ssh user@raspberrypi.local ./toggle

We stop the program with Ctrl-C. Whether a normal user may open /dev/gpiochip0 depends on the group that owns the device. ls -l /dev/gpiochip0 shows the group gpio, and groups shows that the user created in the Imager is already a member. That is why toggle runs without sudo. For other users, we run sudo usermod -aG gpio $USER and log in again. We could of course also start the program as root, but for security reasons we do not.

Oscilloscope

We shut down the Raspberry Pi and disconnect it from the power. Then we connect the probe tip to pin 11 (GPIO17) and the ground clip to a ground pin of the header, for example pin 9. We power the Raspberry Pi on again, log in and start toggle:

Oscilloscope: GPIO17 switches between high and low with a period of 2.18 ms

The line toggles between high and low. With 1 ms each for high and low, we expect a period of about 2 ms. Because usleep is not exact and Linux is not a real-time system, the edges wander from period to period: that is the jitter mentioned above. On the Raspberry Pi 3 used here, the oscilloscope measures about 2.18 ms instead of 2 ms: every usleep sleeps a little longer than requested, and the calls into libgpiod and the kernel add to that.

We tested everything with a Raspberry Pi 3 Model B Rev 1.2 and the 64-bit Raspberry Pi OS (Trixie), built natively and with make clean cross64. Other models and a 32-bit system with make clean cross should work the same way, but we have not tried them. cat /proc/device-tree/model shows the model.