In this tutorial, we’ll compare every popular Raspberry Pi GPIO library: gpiozero, RPi.GPIO, lgpio, libgpiod, pigpio, and WiringPi. You’ll learn which ones work on the Raspberry Pi 5, how to install each one, and how to blink an LED, read an input, and catch an edge with each of them in Python and C/C++, with the same circuit and the same task for every library.
We’ll answer the question: which Raspberry Pi 5 GPIO library should you use, and what changed compared to the Raspberry Pi 4 and older boards. You’ll see the real error messages of the libraries that fail on the Pi 5, and my oscilloscope measurements of each library’s GPIO toggle speed, CPU use, memory, and start-up time. Without further ado, let’s get right into it!
Note: All examples in this tutorial are tested with: Raspberry Pi 5 (8GB) with Raspberry Pi OS (64-bit) Trixie.
This tutorial assumes your Raspberry Pi is set up with Raspberry Pi OS and reachable over SSH. If it isn’t, follow the Raspberry Pi Setup Guide first.
Raspberry Pi GPIO Library Comparison Table
On the Raspberry Pi 5, use gpiozero for Python and libgpiod for C and C++. Both are part of Raspberry Pi OS, use the kernel’s GPIO interface, and need no sudo. This table sums up all six libraries as they ran on my Raspberry Pi 5:
| Library | Works on Pi 5? | Languages | Pre-installed? | Toggle Speed (Measured) | Best For |
|---|---|---|---|---|---|
| gpiozero | Yes | Python | Yes | 131kHz | New Python projects |
| RPi.GPIO | Yes, through rpi-lgpio | Python | Yes (rpi-lgpio) | 129kHz | Running existing RPi.GPIO code |
| lgpio | Yes | Python, C | Yes (C header: liblgpio-dev) | 458kHz (Python), 781kHz (C) | Pin-level control, I2C/SPI helpers |
| libgpiod v2 | Yes | C, C++, Python | Yes (headers: libgpiod-dev) | 870kHz (C), 792kHz (C++), 298kHz (Python) | New C and C++ projects |
| pigpio | No | Python, C | No (no daemon package) | – | Raspberry Pi 4 and older only |
| WiringPi 3 | Yes (since v3.0) | C | No (.deb from GitHub) | 20.0MHz | The fastest bit-banging in C |
Here is the same small job in the two recommended libraries: blink the LED on GPIO17 (pin 11) three times.
Python Code example (gpiozero)
|
1 2 3 4 5 6 7 |
from gpiozero import LED from time import sleep led = LED(17) for _ in range(3): led.on(); sleep(0.2) led.off(); sleep(0.2) |
C++ Code example (libgpiod v2)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <gpiod.hpp> #include <chrono> #include <thread> int main() { auto req = gpiod::chip("/dev/gpiochip0").prepare_request() .add_line_settings(17, gpiod::line_settings().set_direction(gpiod::line::direction::OUTPUT)) .do_request(); for (int i = 0; i < 3; i++) { req.set_value(17, gpiod::line::value::ACTIVE); std::this_thread::sleep_for(std::chrono::milliseconds(200)); req.set_value(17, gpiod::line::value::INACTIVE); std::this_thread::sleep_for(std::chrono::milliseconds(200)); } } |
The rest of this tutorial explains why some libraries fail on the Raspberry Pi 5, shows the complete test for each library with its real output, and compares their measured speed, CPU use, and memory.
Which Raspberry Pi GPIO Library Should You Use?
The right Raspberry Pi GPIO library depends on your language and on the code you already have. This chart sums up my recommendation for the Raspberry Pi 5:

- New Python project: gpiozero. Its LED, Button, and PWMLED classes need the least code, and it’s pre-installed.
- Existing RPi.GPIO code: keep import RPi.GPIO. Raspberry Pi OS runs it on rpi-lgpio, so most scripts work unchanged. Move to gpiozero when you rewrite them.
- C or C++ program: libgpiod v2. It’s the official Linux GPIO interface and works on every Raspberry Pi model.
- Maximum speed in C: WiringPi 3. It writes the RP1 registers directly and toggled a pin at 20.0MHz, 23 times faster than libgpiod.
- Old pigpio code: port it. pigpio doesn’t run on the Raspberry Pi 5; lgpio, from the same author, has the closest API.
Raspberry Pi 5 GPIO Software Stack Explained
A Raspberry Pi GPIO library reaches the pins in one of two ways: through the Linux kernel’s GPIO character device ( /dev/gpiochip0), or by writing the GPIO registers directly through a memory map ( /dev/gpiomem). On the Raspberry Pi 5 this choice decides whether a library works at all.

On the Raspberry Pi 4 and older boards, the 40-pin header is wired to the GPIO block inside the Broadcom SoC. The original RPi.GPIO and pigpio map that block into memory and write its registers, which is fast and simple. On the Raspberry Pi 5, the header pins belong to the RP1 I/O controller, a separate chip connected to the BCM2712 SoC over PCIe. A library that writes the old Broadcom registers changes nothing on the header, so the original RPi.GPIO stops with an error and pigpio was never ported, per the Raspberry Pi white paper RP-006553-WP and pigpio issue #589.
The libraries that go through the kernel don’t care which chip holds the pins. gpiozero (with its lgpio pin factory), rpi-lgpio, lgpio, and libgpiod all open /dev/gpiochip0, which on the Pi 5 is the pinctrl-rp1 driver, and the kernel writes the RP1 registers for them. WiringPi 3 is the exception on the direct path: since version 3.0 (2024) it knows the RP1 register map and writes it through /dev/gpiomem0. For the pin numbers and alternate functions of the header itself, see the Raspberry Pi 5 Pinout guide.
Raspberry Pi GPIO Library Test Setup
Every library in this tutorial runs the same task on the same circuit, so the results compare directly:
- Blink the LED on GPIO17 three times.
- Read GPIO24 while GPIO23 drives it HIGH and LOW through a jumper wire.
- Catch edges: send 5 pulses on GPIO23 and count the rising edges on GPIO24 with the library’s callback or event API.
These are the parts for the test circuit:
| QTY. | Component Name | Amazon.com | AliExpress |
|---|---|---|---|
| 1 | Raspberry Pi 5 (4GB or 8GB) | Amazon | AliExpress |
| 1 | Raspberry Pi 27W USB-C Power Supply | Amazon | AliExpress |
| 1 | 5mm LED (Yellow) | Amazon | AliExpress |
| 1 | 330Ω Resistor | Amazon | AliExpress |
| 1 | Breadboard | Amazon | AliExpress |
| 1 | Jumper Wires (Female-Male and Female-Female) | Amazon | AliExpress |
I used the same breadboard as in my Raspberry Pi GPIO tutorial. Its push button on GPIO27 isn’t used in this comparison.

The LED’s long leg goes to the 330Ω resistor on GPIO17 and its short leg to GND. One jumper wire connects GPIO23 to GPIO24, so a program can drive one pin and read the other back. The oscilloscope’s channel 1 probe sits on GPIO17 for the speed test.
GND (pin 9) goes to the breadboard’s ground rail, which connects the LED’s cathode and the push button.
GPIO17 (pin 11) drives the LED through the 330Ω resistor, and is the pin measured on the oscilloscope.
GPIO27 (pin 13) goes to the push button from the GPIO tutorial. It stays wired but isn’t used in this comparison.
GPIO23 (pin 16) connects to GPIO24 (pin 18) with one jumper: GPIO23 is the output, GPIO24 the input.
gpiozero on the Raspberry Pi 5 (Python)
gpiozero works on the Raspberry Pi 5 out of the box: it’s pre-installed on Raspberry Pi OS (version 2.0.1) and uses the lgpio pin factory on the Pi 5. Instead of pin calls, you create objects for the parts you wire up ( LED, Button, PWMLED, DigitalInputDevice). The gpiozero documentation lists all of them. On an image without it, install it with sudo apt install python3-gpiozero.
Here is The Test Code For gpiozero (gpiozero_demo.py)
Python Code example (gpiozero)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# LAB Name: Raspberry Pi 5 GPIO Library Test (gpiozero): Blink, Read, Edge Callback # Author: Khaled Magdy # For More Info Visit: www.DeepBlueMbedded.com from gpiozero import LED, DigitalOutputDevice, DigitalInputDevice, Device from time import sleep led = LED(17) # GPIO17 (pin 11): LED + 330R drive = DigitalOutputDevice(23) # GPIO23 (pin 16): drives the jumper sense = DigitalInputDevice(24, pull_up=False) # GPIO24 (pin 18): reads the jumper print("Pin factory:", type(Device.pin_factory).__name__) # 1. Blink the LED 3 times for _ in range(3): led.on(); sleep(0.2) led.off(); sleep(0.2) print("Blink: done (3 times)") # 2. Read GPIO24 through the loopback jumper for level in (1, 0): drive.value = level sleep(0.01) print(f"Read: GPIO23={level} -> GPIO24={sense.value}") # 3. Edge callback: count rising edges on GPIO24 edges = 0 def on_rising(): global edges edges += 1 sense.when_activated = on_rising for _ in range(5): drive.on(); sleep(0.05) drive.off(); sleep(0.05) print(f"Callback: {edges} rising edges seen (5 sent)") |
This is the real output on my Raspberry Pi 5. The pin factory is LGPIOFactory, the jumper reads back correctly, and the when_activated callback saw all 5 rising edges:
gpiozero Pin Factories on the Raspberry Pi 5
A gpiozero pin factory is the low-level library gpiozero uses underneath. On the Raspberry Pi 5, two of the four factories work. I created LED(17) with each factory in turn ( factory_test.py in the download):
| Pin Factory | Library Underneath | Raspberry Pi 5 Result |
|---|---|---|
LGPIOFactory | lgpio | Works (the default) |
RPiGPIOFactory | RPi.GPIO (rpi-lgpio on Raspberry Pi OS) | Works |
PiGPIOFactory | pigpio daemon | OSError: failed to connect to localhost:8888 |
NativeFactory | direct Broadcom registers | OSError: unable to open /dev/gpiomem or /dev/mem; upgrade your kernel or run as root |
Older tutorials set GPIOZERO_PIN_FACTORY=pigpio for remote GPIO or better PWM timing. On the Raspberry Pi 5 that line breaks the script, so remove it and let gpiozero pick lgpio. Without the python3-pigpio package, the same setting fails earlier with ModuleNotFoundError: No module named 'pigpio'.
RPi.GPIO on the Raspberry Pi 5 (rpi-lgpio)
RPi.GPIO code runs on the Raspberry Pi 5, but not with the original library. Raspberry Pi OS ships rpi-lgpio ( python3-rpi-lgpio), a drop-in replacement with the same API built on lgpio. import RPi.GPIO loads it, and GPIO.VERSION reports 0.7.2. The original RPi.GPIO writes the Broadcom registers, so it doesn’t support the Pi 5.
Here is The Test Code For RPi.GPIO (rpigpio_demo.py)
Python Code example (RPi.GPIO API)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# LAB Name: Raspberry Pi 5 GPIO Library Test (RPi.GPIO API via rpi-lgpio): Blink, Read, Edge Callback # Author: Khaled Magdy # For More Info Visit: www.DeepBlueMbedded.com import RPi.GPIO as GPIO from time import sleep LED, DRIVE, SENSE = 17, 23, 24 # BCM numbers: pins 11, 16, 18 GPIO.setmode(GPIO.BCM) GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(DRIVE, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(SENSE, GPIO.IN, pull_up_down=GPIO.PUD_OFF) print("RPi.GPIO version:", GPIO.VERSION) try: # 1. Blink the LED 3 times for _ in range(3): GPIO.output(LED, GPIO.HIGH); sleep(0.2) GPIO.output(LED, GPIO.LOW); sleep(0.2) print("Blink: done (3 times)") # 2. Read GPIO24 through the loopback jumper for level in (1, 0): GPIO.output(DRIVE, level) sleep(0.01) print(f"Read: GPIO23={level} -> GPIO24={GPIO.input(SENSE)}") # 3. Edge callback: count rising edges on GPIO24 edges = 0 def on_rising(channel): global edges edges += 1 GPIO.add_event_detect(SENSE, GPIO.RISING, callback=on_rising) for _ in range(5): GPIO.output(DRIVE, 1); sleep(0.05) GPIO.output(DRIVE, 0); sleep(0.05) print(f"Callback: {edges} rising edges seen (5 sent)") finally: GPIO.cleanup() |
I also tested the rest of the RPi.GPIO API on rpi-lgpio in the Raspberry Pi GPIO tutorial: BCM and BOARD numbering, pull-ups, wait_for_edge, bouncetime, and software PWM all passed. The rpi-lgpio documentation lists the small differences.
The Original RPi.GPIO Error on the Raspberry Pi 5
To see what the original library does, I installed RPi.GPIO 0.7.1 from PyPI in a separate virtual environment (not in the system Python). It imports fine, then fails at the first GPIO.setup():
If you see RuntimeError: Cannot determine SOC peripheral base address, your script runs the original RPi.GPIO, usually from a pip install RPi.GPIO in a virtual environment or from the Debian package python3-rpi.gpio. Remove it and use rpi-lgpio ( sudo apt install python3-rpi-lgpio); a virtual environment created with --system-site-packages sees the system’s rpi-lgpio. This command tells you which one you have: python3 -c "import RPi.GPIO, sys; print('rpi-lgpio' if 'lgpio' in sys.modules else 'original')".
RPi.GPIO to gpiozero: The Four Common Patterns
For new code, gpiozero replaces the RPi.GPIO boilerplate with one object per part. The full mapping is in the gpiozero migration guide; these are the four patterns most scripts use:
| RPi.GPIO | gpiozero |
|---|---|
GPIO.setup(17, GPIO.OUT), GPIO.output(17, 1) | led = LED(17), led.on() |
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP), GPIO.input(27) | btn = Button(27), btn.is_pressed |
GPIO.add_event_detect(27, GPIO.FALLING, callback=f) | btn.when_pressed = f |
p = GPIO.PWM(17, 100), p.start(50) | led = PWMLED(17), led.value = 0.5 |
lgpio on the Raspberry Pi 5 (Python and C)
lgpio is the library under gpiozero and rpi-lgpio on the Raspberry Pi 5, written by joan2937, the author of pigpio. It talks to /dev/gpiochip0 and gives you each pin directly: you open the chip, claim a pin as an output, input, or alert, and read or write it. It also has I2C, SPI, and serial helpers (lgpio documentation). The Python module ( python3-lgpio, 0.2.2) is pre-installed; for C, install the header with sudo apt install liblgpio-dev.
Here is The Test Code For lgpio in Python (lgpio_demo.py)
Python Code example (lgpio)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# LAB Name: Raspberry Pi 5 GPIO Library Test (lgpio): Blink, Read, Edge Callback # Author: Khaled Magdy # For More Info Visit: www.DeepBlueMbedded.com import lgpio from time import sleep LED, DRIVE, SENSE = 17, 23, 24 h = lgpio.gpiochip_open(0) # gpiochip0 = 40-pin header (RP1) print("lgpio version:", lgpio.get_module_version()) try: lgpio.gpio_claim_output(h, LED, 0) lgpio.gpio_claim_output(h, DRIVE, 0) # 1. Blink the LED 3 times for _ in range(3): lgpio.gpio_write(h, LED, 1); sleep(0.2) lgpio.gpio_write(h, LED, 0); sleep(0.2) print("Blink: done (3 times)") # 2. Read GPIO24 through the loopback jumper lgpio.gpio_claim_input(h, SENSE) for level in (1, 0): lgpio.gpio_write(h, DRIVE, level) sleep(0.01) print(f"Read: GPIO23={level} -> GPIO24={lgpio.gpio_read(h, SENSE)}") # 3. Edge callback: the pin must be claimed as an alert to get edges lgpio.gpio_free(h, SENSE) lgpio.gpio_claim_alert(h, SENSE, lgpio.RISING_EDGE) edges = 0 def on_rising(chip, gpio, level, tick): global edges edges += 1 cb = lgpio.callback(h, SENSE, lgpio.RISING_EDGE, on_rising) for _ in range(5): lgpio.gpio_write(h, DRIVE, 1); sleep(0.05) lgpio.gpio_write(h, DRIVE, 0); sleep(0.05) print(f"Callback: {edges} rising edges seen (5 sent)") cb.cancel() finally: lgpio.gpiochip_close(h) # frees every claimed line |
A pin must be claimed with gpio_claim_alert() to report edges. With gpio_claim_input() you can read it, but the callback never fires and no error tells you why.
Here is The Test Code For lgpio in C (lgpio_demo.c)
C Code example (lgpio)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/* * LAB Name: Raspberry Pi 5 GPIO Library Test (lgpio, C): Blink, Read, Edge Callback * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com * Build: gcc lgpio_demo.c -o lgpio_demo -llgpio */ #include <lgpio.h> #include <stdio.h> #define LED 17 /* pin 11 */ #define DRIVE 23 /* pin 16 */ #define SENSE 24 /* pin 18 */ static volatile int edges = 0; static void on_alert(int n, lgGpioAlert_p evt, void *user) { for (int i = 0; i < n; i++) if (evt[i].report.level == 1) edges++; /* rising edge */ } int main(void) { int h = lgGpiochipOpen(0); /* gpiochip0 = 40-pin header (RP1) */ if (h < 0) { printf("lgGpiochipOpen: %s\n", lguErrorText(h)); return 1; } lgGpioClaimOutput(h, 0, LED, 0); lgGpioClaimOutput(h, 0, DRIVE, 0); /* 1. Blink the LED 3 times */ for (int i = 0; i < 3; i++) { lgGpioWrite(h, LED, 1); lguSleep(0.2); lgGpioWrite(h, LED, 0); lguSleep(0.2); } printf("Blink: done (3 times)\n"); /* 2. Read GPIO24 through the loopback jumper */ lgGpioClaimInput(h, 0, SENSE); for (int level = 1; level >= 0; level--) { lgGpioWrite(h, DRIVE, level); lguSleep(0.01); printf("Read: GPIO23=%d -> GPIO24=%d\n", level, lgGpioRead(h, SENSE)); } /* 3. Edge callback: claim GPIO24 as an alert for rising edges */ lgGpioFree(h, SENSE); lgGpioClaimAlert(h, 0, LG_RISING_EDGE, SENSE, -1); lgGpioSetAlertsFunc(h, SENSE, on_alert, NULL); for (int i = 0; i < 5; i++) { lgGpioWrite(h, DRIVE, 1); lguSleep(0.05); lgGpioWrite(h, DRIVE, 0); lguSleep(0.05); } lguSleep(0.05); printf("Callback: %d rising edges seen (5 sent)\n", edges); lgGpiochipClose(h); return 0; } |
Note: Closing the chip takes time in lgpio: lgGpiochipClose() ( gpiochip_close() in Python) blocked for 100ms on my Raspberry Pi 5, in both C and Python. A C program that opens, sets, and closes GPIO17 ran in 101ms, and in 1ms without the close call. It only matters for short programs started many times, such as a script called from cron.
libgpiod v2 on the Raspberry Pi 5 (Python, C++ and C)
libgpiod is the official library for the Linux GPIO character device, maintained with the kernel. Raspberry Pi OS Trixie ships version 2.2.1 with the command-line tools ( gpioset, gpioget, gpioinfo) and the Python bindings ( python3-libgpiod, import gpiod). For C and C++, install the headers with sudo apt install libgpiod-dev. The C++ examples and the full C++ walkthrough are in my Raspberry Pi GPIO tutorial.
Note: Raspberry Pi OS has libgpiod v2, and v1 code doesn’t run on it. Many Raspberry Pi 5 tutorials from 2023 use the v1 API ( chip.get_line(), LINE_REQ_DIR_OUT, gpiod_chip_open_by_name()). Use the v2 request API shown below.
Here is The Test Code For libgpiod in Python (gpiod_demo.py)
Python Code example (libgpiod v2 Python bindings)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# LAB Name: Raspberry Pi 5 GPIO Library Test (libgpiod v2 Python bindings): Blink, Read, Edge Events # Author: Khaled Magdy # For More Info Visit: www.DeepBlueMbedded.com import gpiod from datetime import timedelta from time import sleep from gpiod.line import Direction, Value, Edge LED, DRIVE, SENSE = 17, 23, 24 CHIP = "/dev/gpiochip0" # 40-pin header (RP1) print("gpiod version:", gpiod.__version__) out = gpiod.request_lines(CHIP, consumer="g2-out", config={ (LED, DRIVE): gpiod.LineSettings(direction=Direction.OUTPUT, output_value=Value.INACTIVE)}) inp = gpiod.request_lines(CHIP, consumer="g2-in", config={ SENSE: gpiod.LineSettings(direction=Direction.INPUT, edge_detection=Edge.RISING)}) # 1. Blink the LED 3 times for _ in range(3): out.set_value(LED, Value.ACTIVE); sleep(0.2) out.set_value(LED, Value.INACTIVE); sleep(0.2) print("Blink: done (3 times)") # 2. Read GPIO24 through the loopback jumper for level in (Value.ACTIVE, Value.INACTIVE): out.set_value(DRIVE, level) sleep(0.01) print(f"Read: GPIO23={level.value} -> GPIO24={inp.get_value(SENSE).value}") # 3. Edge events: libgpiod queues rising edges in the kernel, we read them back while inp.wait_edge_events(timedelta(0)): # drop the edge from step 2 inp.read_edge_events() for _ in range(5): out.set_value(DRIVE, Value.ACTIVE); sleep(0.05) out.set_value(DRIVE, Value.INACTIVE); sleep(0.05) edges = 0 while inp.wait_edge_events(timedelta(milliseconds=100)): edges += len(inp.read_edge_events()) print(f"Edge events: {edges} rising edges seen (5 sent)") out.release(); inp.release() |
libgpiod has no callback thread: the kernel queues each edge with a timestamp, and your program reads them with wait_edge_events() and read_edge_events(). That’s why the code drops the edge from step 2 before counting.
Here is The Test Code For libgpiod in C++ (libgpiod_demo.cpp)
C++ Code example (libgpiod v2)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* * LAB Name: Raspberry Pi 5 GPIO Library Test (libgpiod v2, C++): Blink, Read, Edge Events * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com * Build: g++ -std=c++17 libgpiod_demo.cpp -o libgpiod_demo $(pkg-config --cflags --libs libgpiodcxx) */ #include <gpiod.hpp> #include <chrono> #include <iostream> #include <thread> using namespace std::chrono_literals; using gpiod::line::value; int main() { const gpiod::line::offset LED = 17, DRIVE = 23, SENSE = 24; gpiod::chip chip("/dev/gpiochip0"); // 40-pin header (RP1) auto out = chip.prepare_request().set_consumer("g2-out") .add_line_settings({LED, DRIVE}, gpiod::line_settings() .set_direction(gpiod::line::direction::OUTPUT) .set_output_value(value::INACTIVE)) .do_request(); auto in = chip.prepare_request().set_consumer("g2-in") .add_line_settings(SENSE, gpiod::line_settings() .set_direction(gpiod::line::direction::INPUT) .set_edge_detection(gpiod::line::edge::RISING)) .do_request(); // 1. Blink the LED 3 times for (int i = 0; i < 3; i++) { out.set_value(LED, value::ACTIVE); std::this_thread::sleep_for(200ms); out.set_value(LED, value::INACTIVE); std::this_thread::sleep_for(200ms); } std::cout << "Blink: done (3 times)\n"; // 2. Read GPIO24 through the loopback jumper for (value level : {value::ACTIVE, value::INACTIVE}) { out.set_value(DRIVE, level); std::this_thread::sleep_for(10ms); std::cout << "Read: GPIO23=" << (level == value::ACTIVE) << " -> GPIO24=" << (in.get_value(SENSE) == value::ACTIVE) << "\n"; } // 3. Edge events: the kernel queues rising edges, we read them back gpiod::edge_event_buffer buffer; while (in.wait_edge_events(0ms)) // drop the edge from step 2 in.read_edge_events(buffer); for (int i = 0; i < 5; i++) { out.set_value(DRIVE, value::ACTIVE); std::this_thread::sleep_for(50ms); out.set_value(DRIVE, value::INACTIVE); std::this_thread::sleep_for(50ms); } size_t edges = 0; while (in.wait_edge_events(100ms)) edges += in.read_edge_events(buffer); std::cout << "Edge events: " << edges << " rising edges seen (5 sent)\n"; return 0; } |
The C API needs a few more objects (line settings, line config, request config), so here is the blink part only:
C Code example (libgpiod v2)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* * LAB Name: Raspberry Pi 5 GPIO Library Test (libgpiod v2, C): Blink * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com * Build: gcc libgpiod_demo.c -o libgpiod_demo $(pkg-config --cflags --libs libgpiod) */ #include <gpiod.h> #include <stdio.h> #include <unistd.h> int main(void) { unsigned int led = 17; /* GPIO17 = pin 11 */ struct gpiod_chip *chip = gpiod_chip_open("/dev/gpiochip0"); if (!chip) { perror("gpiod_chip_open"); return 1; } /* v2 API: line settings -> line config -> request */ struct gpiod_line_settings *settings = gpiod_line_settings_new(); gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_OUTPUT); struct gpiod_line_config *cfg = gpiod_line_config_new(); gpiod_line_config_add_line_settings(cfg, &led, 1, settings); struct gpiod_request_config *rcfg = gpiod_request_config_new(); gpiod_request_config_set_consumer(rcfg, "g2-c"); struct gpiod_line_request *req = gpiod_chip_request_lines(chip, rcfg, cfg); if (!req) { perror("gpiod_chip_request_lines"); return 1; } for (int i = 0; i < 3; i++) { gpiod_line_request_set_value(req, led, GPIOD_LINE_VALUE_ACTIVE); usleep(200000); gpiod_line_request_set_value(req, led, GPIOD_LINE_VALUE_INACTIVE); usleep(200000); } printf("Blink: done (3 times)\n"); gpiod_line_request_release(req); gpiod_request_config_free(rcfg); gpiod_line_config_free(cfg); gpiod_line_settings_free(settings); gpiod_chip_close(chip); return 0; } |
libgpiod v1 Python Code Error on the Raspberry Pi 5
This is the v1 Python code still found in popular 2023 Raspberry Pi 5 guides, run on Raspberry Pi OS Trixie. gpiod.Chip('gpiochip4') fails first because v2 expects a path; with /dev/gpiochip0 it fails at get_line:
The C and C++ compile errors of v1 code are in the Raspberry Pi GPIO tutorial.
pigpio on the Raspberry Pi 5
pigpio doesn’t work on the Raspberry Pi 5. Its daemon, pigpiod, drives the Broadcom GPIO, PWM, and DMA blocks directly, and the Pi 5’s header is on RP1. The pigpio issue #589 (“pigpio will not run on a Pi 5”) has been open since November 2023, and the last pigpio release, v79, is from March 2021. On a Raspberry Pi 4 and older, pigpio still works, per its documentation.
Raspberry Pi OS Trixie doesn’t even package the daemon: sudo apt install pigpio finds nothing. Only the client side exists ( python3-pigpio and pigpio-tools), so an old pigpio script installs without complaint and then fails like this:
What to use instead of pigpio on the Raspberry Pi 5:
- lgpio for pigpio-style Python or C code: same author, similar calls ( gpio_write, callback, tx_pwm).
- gpiozero with its default lgpio pin factory, instead of PiGPIOFactory.
- The kernel PWM driver for jitter-free hardware PWM (servos), through the pwm or pwm-2chan overlay.
WiringPi on the Raspberry Pi 5
WiringPi works on the Raspberry Pi 5. The original author stopped it in 2019, but the project continues on GitHub: version 3.0 added the Raspberry Pi 5 in February 2024, and the latest release is 3.20 (September 2026). Per its README, only the GPIO clock (GCLK) function isn’t supported on the Pi 5. It isn’t in the Raspberry Pi OS repositories, so install the .deb from the releases page (96KB):
|
1 2 3 |
wget https://github.com/WiringPi/WiringPi/releases/download/v3.20/wiringpi_3.20_arm64.deb sudo apt install ./wiringpi_3.20_arm64.deb gpio -v |
The gpio readall command prints the header with BCM, WiringPi, and physical numbers, and each pin’s mode and level. Here GPIO17 and GPIO23 show OUT and GPIO24 shows IN from the earlier tests:
Here is The Test Code For WiringPi (wiringpi_demo.c)
C Code example (WiringPi 3)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* * LAB Name: Raspberry Pi 5 GPIO Library Test (WiringPi 3, C): Blink, Read, Edge Callback (ISR) * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com * Build: gcc wiringpi_demo.c -o wiringpi_demo -lwiringPi */ #include <wiringPi.h> #include <stdio.h> #define LED 17 /* BCM numbers (wiringPiSetupGpio): pin 11 */ #define DRIVE 23 /* pin 16 */ #define SENSE 24 /* pin 18 */ static volatile int edges = 0; static void on_rising(void) { edges++; } int main(void) { wiringPiSetupGpio(); /* use BCM GPIO numbers */ pinMode(LED, OUTPUT); pinMode(DRIVE, OUTPUT); pinMode(SENSE, INPUT); pullUpDnControl(SENSE, PUD_OFF); /* 1. Blink the LED 3 times */ for (int i = 0; i < 3; i++) { digitalWrite(LED, HIGH); delay(200); digitalWrite(LED, LOW); delay(200); } printf("Blink: done (3 times)\n"); /* 2. Read GPIO24 through the loopback jumper */ for (int level = 1; level >= 0; level--) { digitalWrite(DRIVE, level); delay(10); printf("Read: GPIO23=%d -> GPIO24=%d\n", level, digitalRead(SENSE)); } /* 3. Edge callback: WiringPi runs the ISR function in its own thread */ wiringPiISR(SENSE, INT_EDGE_RISING, on_rising); for (int i = 0; i < 5; i++) { digitalWrite(DRIVE, HIGH); delay(50); digitalWrite(DRIVE, LOW); delay(50); } delay(50); printf("Callback: %d rising edges seen (5 sent)\n", edges); wiringPiISRStop(SENSE); return 0; } |
wiringPiSetupGpio() selects BCM numbers, so the code uses the same GPIO17, GPIO23, and GPIO24 as the other libraries. It ran as my normal user, with no sudo. WiringPi is C only today; the WiringPi-Python wrapper on PyPI (2.60.1, 2021) predates the Pi 5 support.
Raspberry Pi GPIO Library Speed Test
WiringPi is the fastest Raspberry Pi 5 GPIO library by far: it toggled GPIO17 at 20.0MHz, against 870kHz for libgpiod in C and 131kHz for gpiozero. Each library ran a tight loop that sets GPIO17 HIGH and LOW for 45 seconds, and I measured the frequency on my oscilloscope and with the program’s own counter (they agree within 1%).
| Library | Language | Toggle Speed | Period |
|---|---|---|---|
| RPi.GPIO (rpi-lgpio) | Python | 129kHz | 7.8µs |
| gpiozero | Python | 131kHz | 7.6µs |
| libgpiod v2 bindings | Python | 298kHz | 3.35µs |
| lgpio | Python | 458kHz | 2.18µs |
| lgpio | C | 781kHz | 1.28µs |
| libgpiod v2 | C++ | 792kHz | 1.26µs |
| libgpiod v2 | C | 870kHz | 1.15µs |
| WiringPi 3.20 | C | 20.0MHz | 50ns |
The gpiozero, RPi.GPIO, lgpio Python, and libgpiod C++ numbers are the same runs as in my Raspberry Pi GPIO tutorial; I repeated them for this comparison and three of the four landed within 1.3% of those values. These are my oscilloscope captures of the four other libraries. Channel 1 (yellow) is GPIO17 (pin 11), and the orange arrow marks one period with its measured value:
At 20.0MHz, the WiringPi signal looks like a rounded wave between 0.56v and 2.52v. My probe is set to 1X, which limits its bandwidth to a few MHz; the frequency reading (20.00MHz, 50ns period) is exact and matches the program’s count of 19,995kHz:
Why WiringPi Is 23 Times Faster
The CPU time split shows where each loop spends its time. A kernel-path library makes one ioctl() system call per pin change; WiringPi writes the RP1 register from user space with no system call at all. I measured the user and kernel CPU seconds of each 5-second busy loop:
| Library | User CPU (s) | Kernel CPU (s) | Time in Kernel |
|---|---|---|---|
| gpiozero (Python) | 3.78 | 1.34 | 26% |
| RPi.GPIO (Python) | 3.79 | 1.26 | 25% |
| gpiod (Python) | 3.56 | 1.50 | 30% |
| lgpio (Python) | 2.46 | 2.58 | 51% |
| lgpio (C) | 0.98 | 4.02 | 80% |
| libgpiod (C++) | 0.97 | 4.03 | 81% |
| libgpiod (C) | 0.61 | 4.39 | 88% |
| WiringPi (C) | 5.00 | 0.00 | 0% |
Every busy loop uses one full CPU core, whatever the library. The C programs on the kernel path spend 80% to 88% of that core inside the kernel, while WiringPi spends 0%. The price of WiringPi’s speed is that the kernel no longer arbitrates the pin: another program, or a driver that owns the pin, can be overwritten without any error. For most projects, 870kHz is far more than needed. Fast, exact timing belongs to the hardware peripherals (PWM, SPI) anyway, because Linux can pause any loop at any moment. When a project needs cycle-exact GPIO timing, a microcontroller such as the Raspberry Pi Pico does it better (see the Raspberry Pi Pico digital input and output tutorial).
Memory and Start-Up Time of Each Library
I also measured each library’s peak memory (resident set size, while toggling) and the time from launching the program until it set GPIO17 HIGH and exited:
| Library | Peak Memory | Start-Up Time |
|---|---|---|
| gpiozero (Python) | 19.9MB | 208ms |
| RPi.GPIO (Python) | 12.7MB | 38ms |
| gpiod (Python) | 13.1MB | 50ms |
| lgpio (Python) | 11.5MB | 29ms |
| lgpio (C) | 1.7MB | 102ms (100ms of it in the close call) |
| libgpiod (C++) | 3.4MB | 2.6ms |
| libgpiod (C) | 1.5MB | 1.6ms |
| WiringPi (C) | 1.8MB | 0.5ms |
Python itself starts in about 15ms on the Pi 5, so most of gpiozero’s 208ms is its import time. On an 8GB Raspberry Pi 5, 20MB is not a problem; the start-up time only matters for scripts that are launched again and again.
Raspberry Pi GPIO Libraries: Versions, Licenses and Features
This table lists each library’s latest release, license, and features, checked on the projects’ official pages on November 28, 2026. The Raspberry Pi 5 column comes from my bench; the hardware PWM and I2C/SPI columns come from each project’s documentation.
| Library | Latest Release | License | Needs Root or Daemon | Hardware PWM | I2C / SPI |
|---|---|---|---|---|---|
| gpiozero | 2.0.1 (Feb 2024) | BSD-3-Clause | No | No (software PWM) | SPI devices only |
| rpi-lgpio | 0.6 (May 2024) | BSD-3-Clause | No | No (software PWM) | No |
| RPi.GPIO (original) | 0.7.1 (Feb 2022) | MIT | No (fails on Pi 5) | No (software PWM) | No |
| lgpio | 0.2.2 (2023) | Unlicense | No | No (software-timed) | Yes |
| libgpiod | 2.3.2 (Nov 2026; Trixie ships 2.2.1) | LGPL-2.1+ | No | No (GPIO only) | No (GPIO only) |
| pigpio | v79 (Mar 2021) | Unlicense | Root daemon | Yes (Pi 4 and older) | Yes (Pi 4 and older) |
| WiringPi | 3.20 (Sep 2026) | LGPL-3.0 | No on the Pi 5 | Yes (per README) | Yes |
Raspberry Pi GPIO Library Errors and Fixes
RuntimeError: Cannot determine SOC peripheral base address
You’re running the original RPi.GPIO on a Raspberry Pi 5. Remove it ( pip uninstall RPi.GPIO in your virtual environment, or sudo apt install python3-rpi-lgpio to replace python3-rpi.gpio) and use rpi-lgpio, which keeps the same API.
Can’t connect to pigpio at localhost(8888)
pigpio’s daemon isn’t running, and on the Raspberry Pi 5 it can’t run: there is no pigpiod package in Raspberry Pi OS Trixie and no Pi 5 support upstream. Port the code to lgpio or gpiozero. In gpiozero, remove GPIOZERO_PIN_FACTORY=pigpio.
sudo: pigpiod: command not found
Same cause: Trixie only packages the pigpio client ( python3-pigpio, pigpio-tools), not the daemon.
OSError: unable to open /dev/gpiomem or /dev/mem
gpiozero’s NativeFactory was selected. It writes the Broadcom registers, which don’t drive the Pi 5 header. Use the default lgpio factory. Running it with sudo doesn’t fix it.
AttributeError: ‘Chip’ object has no attribute ‘get_line’
Your Python code uses the libgpiod v1 API. Rewrite it with gpiod.request_lines() as in the libgpiod section above.
Do I need sudo for any of these libraries?
No. gpiozero, rpi-lgpio, lgpio, libgpiod, and WiringPi 3 all ran as my normal user, which is in the gpio group. Only pigpio’s daemon needed root, and it doesn’t run on the Pi 5.
Raspberry Pi GPIO Library FAQ
Is RPi.GPIO deprecated?
The original RPi.GPIO hasn’t had a release since version 0.7.1 in February 2022 and doesn’t support the Raspberry Pi 5. Its API lives on through rpi-lgpio, which Raspberry Pi OS installs by default. New code should use gpiozero.
Is WiringPi deprecated?
No. The original author ended it in 2019, but the community project on GitHub released version 3.0 with Raspberry Pi 5 support in 2024 and version 3.20 in September 2026.
Does gpiozero support hardware PWM on the Raspberry Pi 5?
No. On the Pi 5, gpiozero’s PWMLED and PWMOutputDevice use lgpio’s software PWM. For hardware PWM, use the kernel PWM driver through the pwm overlay.
Can two libraries use the same GPIO pin?
Not with the kernel-path libraries: the kernel gives a line to one program at a time, and the second request fails with “Device or resource busy” or “GPIO busy”. WiringPi’s direct writes bypass that check, so avoid mixing it with other libraries on the same pins.
Download Raspberry Pi GPIO Library Examples
The download contains every test program from this tutorial (gpiozero, RPi.GPIO, lgpio, libgpiod, WiringPi, and pigpio), the speed-test loops in Python, C, and C++, the gpiozero pin factory test, and the CPU, memory, and start-up measurement script, with a README that lists each build command.
Download Raspberry Pi GPIO Library Examples (Python, C, C++)
Download all attachment files for this Article/Tutorial (project files, schematics, code, etc.) using the link below. Everything is tested and verified to work on real hardware, as you’ve seen in the demo videos & screenshots. Please consider supporting my work if it helped you.
Wrap Up
To conclude this tutorial, we’ve tested six Raspberry Pi GPIO libraries on the Raspberry Pi 5 with the same circuit and the same task. gpiozero, RPi.GPIO through rpi-lgpio, lgpio, libgpiod v2, and WiringPi 3 all work; the original RPi.GPIO and pigpio don’t, because they expect the Broadcom GPIO block that no longer drives the header. For new projects, I recommend gpiozero in Python and libgpiod in C and C++, and WiringPi when you need the last bit of speed in C.
To get started with the circuit and the basics of GPIO output and input in Python and C++, follow my Raspberry Pi GPIO tutorial. If your Raspberry Pi isn’t set up yet, start with the Raspberry Pi Setup Guide. The next tutorials in this Raspberry Pi series cover LED projects, push buttons with debouncing and interrupts, and PWM.
![Raspberry Pi 5 Pinout, Datasheet, Schematic [Ultimate Guide] Raspberry Pi 5 Pinout, Datasheet, Schematic [Ultimate Guide]](https://deepbluembedded.com/wp-content/uploads/2026/11/raspberry-pi-5-pinout-datasheet-schematic-getting-started-guide.webp)
