While libraries like RPi.GPIO
and gpiozero
make Raspberry Pi hardware programming accessible and simple, certain advanced use cases—such as precise servo control, hardware PWM, or remote GPIO manipulation—require something more powerful. That’s where pigpio comes in: a versatile and high-performance library for controlling GPIO pins with greater accuracy and flexibility.
🔍 What Is pigpio?
pigpio is a C library with Python bindings that allows low-level control of GPIO pins on the Raspberry Pi. It supports:
- Accurate hardware PWM and servo control
- Precise pulse timing
- Waveform generation
- Remote GPIO control over a network
Unlike RPi.GPIO
, which runs in the foreground and may suffer from timing issues, pigpio
runs a daemon process (pigpiod
) in the background, ensuring accurate, concurrent GPIO interactions.
🛠️ Installation
To install pigpio and start the daemon:
bash
sudo apt update
sudo apt install pigpio python3-pigpio
sudo systemctl start pigpiod # Start the daemon
To use it in Python:
python
import pigpio
pi = pigpio.pi() # Connect to local pigpiod daemon
Make sure pigpiod
is running in the background; otherwise, your scripts won’t connect.
📌 Key Features of pigpio
FeatureDescriptionHardware PWM & ServoUltra-precise control of LEDs, motors, and servosWaveformsSend complex GPIO wave sequencesRemote GPIO ControlControl GPIOs over TCP/IP from another machinePulse CountingMeasure digital signals or encoder pulsesCallbacksReact to GPIO state changes with precise timingSimultaneous AccessMultiple scripts can use GPIO without conflictBit BangingEmulate I2C, SPI, or 1-Wire protocols manually
💡 Basic Example: LED Blink
python
import pigpio
import time
pi = pigpio.pi()
pi.set_mode(17, pigpio.OUTPUT)
for _ in range(10):
pi.write(17, 1) # LED on
time.sleep(1)
pi.write(17, 0) # LED off
time.sleep(1)
pi.stop()
🔁 PWM and Servo Example
python
import pigpio
import time
pi = pigpio.pi()
# Set PWM on GPIO 18
pi.set_PWM_frequency(18, 500)
pi.set_PWM_dutycycle(18, 128) # 50% duty cycle (0–255)
# Servo on GPIO 17 (pulse width 500–2500 microseconds)
pi.set_servo_pulsewidth(17, 1500)
time.sleep(1)
pi.set_servo_pulsewidth(17, 1000)
time.sleep(1)
pi.set_servo_pulsewidth(17, 0) # Turn off servo signal
pi.stop()
📶 Remote GPIO Access
You can control a Raspberry Pi’s GPIO from another computer by:
- Running
pigpiod
on the Pi. - Connecting from a remote machine with:
python
pi = pigpio.pi('raspberrypi.local') # Replace with IP or hostname
This allows network-controlled robotics, distributed sensor networks, and cloud-connected hardware projects.
⚖️ pigpio vs RPi.GPIO vs gpiozero
FeaturepigpioRPi.GPIOgpiozeroPrecisionHigh (microsecond-level)ModerateDepends on backendPWM/ServoHardware-based, accurateSoftware PWM, less preciseLimited PWM supportWaveform GenerationYesNoNoRemote GPIOYesNoYes (via pigpio backend)Ease of UseAdvancedIntermediateBeginner-friendlyPerformanceExcellentGoodModerate
🧪 Common Use Cases
- 🔁 Servo-controlled robots
- 🕹️ High-speed input devices
- 💡 PWM-controlled LEDs with dimming
- 🌐 IoT devices accessed remotely
- 🧮 Pulse or signal measurement (tachometers, rotary encoders)
- 🔀 Generating custom digital signals or protocols
🧩 Advanced Capabilities
- Wave Chains: Combine multiple pulses into a custom waveform
- Notifications via Pipe: For real-time performance in non-Python apps
- SPI/I2C Emulation: Bit-bang your own communication protocols
- Scriptable with C and other languages beyond Python
🧹 Cleanup and Safety
Always call pi.stop()
at the end of your script to release resources and avoid conflicts.
📌 Conclusion
pigpio is a powerful and precise GPIO control library for advanced Raspberry Pi projects. Whether you’re building a servo-driven robot, a remote-controlled sensor hub, or just need timing accuracy, pigpio delivers capabilities well beyond simpler libraries.
It’s not as beginner-friendly as gpiozero, but it’s a favorite among experienced developers and engineers working on complex hardware projects that require real-time accuracy and remote operation.