Don’t miss Amazon Black Friday See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Raspberry Pi GPIO C++: 10 Essential Libraries

If you’re moving beyond Python and want real-time control, low latency, or just prefer C++, this list covers the best libraries and frameworks for Raspberry Pi GPIO programming. You’ll find options for simple pin toggling, high-speed peripherals, and full kernel-level control — each with its own strengths. Whether you’re building a robot, an LED matrix, or a custom HAT, these tools will get you started.

1. pigpio

pigpio is a C library and daemon that provides the most feature-rich GPIO interface on the Pi. It supports hardware-timed PWM, servo pulses, wave forms, and precise edge-timed sampling, all without kernel modules. The daemon runs as a separate process, so your C++ program can connect via socket or pipe, which also allows remote control over the network. If you need microsecond accuracy or want to bit-bang protocols like WS2812B LEDs, pigpio is the go-to choice.

2. WiringPi (maintained fork)

WiringPi was the original de facto standard for C/C++ GPIO, but the original author deprecated it in 2019. The community-maintained fork, WiringPi (by Guzunty), keeps it alive with support for newer Pi models. Its API is simple and familiar — digitalWrite, digitalRead, pinMode — and it’s perfect for quick projects. However, it’s not as fast or precise as pigpio, and its future is somewhat uncertain, so it’s best for learning or legacy code.

3. libgpiod

libgpiod is the official Linux kernel GPIO interface, replacing the deprecated sysfs interface. It’s a C library with C++ bindings, and it’s included in the Raspberry Pi OS. It gives you clean access to GPIO lines, events, and consumer labels, and it’s the most future-proof option since it’s maintained by the kernel community. The API is more verbose than pigpio, but it’s the standard way to handle GPIO in modern Linux. For robust, production-ready code, libgpiod is a solid choice.

4. bcm2835

This library by Mike McCauley provides direct access to the Broadcom BCM2835/2836/2837/2711/2712 SoC registers, bypassing the Linux kernel entirely. It’s incredibly fast and lightweight, making it ideal for bit-banging or high-frequency toggling. However, it runs with root privileges and doesn’t handle interrupts well — you’ll have to poll. It’s a great tool for specific tasks like driving SPI displays or generating precise waveforms, but not for complex multi-threaded applications.

5. RPi.GPIO (C++ port)

If you’re coming from Python’s RPi.GPIO, there’s a C++ port that mirrors its API almost exactly. It’s not officially maintained, but it’s a quick way to port existing Python code. You get the same functions like GPIO::setup, GPIO::output, and GPIO::input, with the same limitations (no hardware PWM, no precise timing). It’s fine for simple projects, but you’ll quickly outgrow it.

6. WiringPiCPP (C++ wrapper)

This is a C++ wrapper around WiringPi that adds object-oriented abstractions like Pin, PWM, and I2C classes. It’s not a separate library — it just makes WiringPi more convenient for C++ developers. If you like the WiringPi API but want RAII and cleaner code, this is a nice middle ground. However, it inherits WiringPi’s limitations, so don’t expect high performance.

7. PicoLib (for Pico on Pi)

While not for the Pi’s GPIO pins directly, PicoLib is a C++ library for the RP2040 (the chip on the Pico) when used as a USB or serial peripheral attached to a Pi. It lets you offload real-time tasks to the Pico, which then communicates with the Pi over USB. This is a powerful pattern: the Pi handles high-level logic, and the Pico handles precise timing. If you’re building a robot or a complex sensor network, this combo is worth exploring.

8. sysfs (legacy, but still used)

You can interact with GPIO via the sysfs filesystem directly in C++ by reading and writing files like /sys/class/gpio/export. It’s not a library, but it’s a fallback that works on any Linux system. The downside is that sysfs is deprecated and slow — each operation requires a file open/close. It’s useful for quick scripts or when you can’t install a library, but for real projects, use libgpiod instead.

9. Boost.Asio with pigpio

This isn’t a library per se, but a pattern: using Boost.Asio’s asynchronous I/O to communicate with the pigpio daemon. You can run pigpio in a separate thread or process and use Asio to handle events without blocking. This is excellent for building networked or event-driven applications, like a home automation server. It’s more complex to set up, but it gives you the scalability of Asio with the raw power of pigpio.

10. C++17 with libgpiod and async

Finally, a modern approach: use libgpiod’s C++ bindings with C++17 coroutines or std::async to handle GPIO events asynchronously. This is the most ‘modern C++’ way to do GPIO — you get type safety, RAII, and concurrency without external dependencies beyond libgpiod. It’s not as fast as pigpio, but it’s clean and portable. If you’re starting a new project and want to follow best practices, this is a great foundation.

How to Choose

The first question is: do you need precise timing or high frequency? If yes, pigpio or bcm2835 are your only real options. If you’re building a simple LED blinker or reading a button, any library will do — pick the one with the simplest API (WiringPi or RPi.GPIO port). For production or long-term projects, libgpiod is the safest bet because it’s the official kernel interface and won’t break with future Pi models. Also consider whether you need interrupts: pigpio and libgpiod support them, while bcm2835 and WiringPi do not. Finally, think about your comfort level — if you’re a C++ expert, you might enjoy the low-level control of bcm2835; if you’re a beginner, start with WiringPi.

Frequently Asked Questions

Q: Can I use these libraries with C++ on a Raspberry Pi 5?
Yes, but check compatibility: pigpio and bcm2835 have been updated for Pi 5, while WiringPi may need the fork. libgpiod works out of the box.

Q: How do I compile a C++ program with pigpio?
You need to link against pigpio: g++ -o myprog myprog.cpp -lpigpio -lrt -lpthread. Also, you must start the pigpio daemon with sudo pigpiod.

Q: Which library is fastest for toggling a GPIO pin?
bcm2835 is the fastest because it writes directly to registers, but pigpio is close and offers more features. For most projects, pigpio’s speed is sufficient.

Q: Can I use these libraries in a multi-threaded C++ program?
Yes, but be careful: pigpio is thread-safe, libgpiod is not by default (you need to protect with mutexes), and bcm2835 is not thread-safe at all.

Q: Do I need root permissions to use GPIO?
With libgpiod, you may need to set permissions or use sudo. pigpio requires the daemon to run as root, but your program can run as a normal user. bcm2835 requires root.

Q: How do I handle GPIO interrupts in C++?
Use pigpio’s gpioSetAlertFunc or libgpiod’s event request with gpiod_line_request_events. Both allow you to register a callback function that runs when a pin changes state.

Final Thoughts

There’s no single ‘best’ library — it depends on your project’s needs. For most hobbyists, pigpio offers the best balance of performance, features, and ease of use. If you’re building something that will be maintained for years, invest time in learning libgpiod. And if you’re just experimenting, WiringPi’s simplicity is hard to beat. Start with one, get your LED blinking, and then explore the others as you need more power.

Raspberry Pi GPIO C++: 10 Essential Libraries
Scroll to top