Project

rgpio

0.0
The project is in a healthy, maintained state
Control a Raspberry Pi's hardware from Ruby. GPIO goes through libgpiod v2 — the Linux GPIO character device (uAPI v2) — rather than the deprecated sysfs interface. PWM is either the hardware peripheral through sysfs or timed in Ruby on any line; I2C and SPI are ioctl calls on /dev/i2c-N and /dev/spidev, needing no libgpiod at all. On top sits a gpiozero-style device layer: LED, Button, Motor, PWMLED, RGBLED, Servo, and drivers for the ADT7410 temperature sensor, ST7032 character LCD and MCP3208 ADC. Bound through the stdlib `fiddle`, which is built with the interpreter and so matches any Pi's architecture, unlike the precompiled `ffi` gem.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
~> 1.90

Runtime

>= 1.0
 Project Readme

rgpio

Ruby bindings for libgpiod v2 — the modern Linux GPIO character device API.

Provides GPIO input/output and jitter-free hardware PWM control on Raspberry Pi, targeting the uAPI v2 ioctl interface instead of the deprecated sysfs GPIO interface. No C extension — calls libgpiod.so directly through the stdlib fiddle, which (unlike the precompiled ffi gem) is built with the interpreter and works on every Pi, including ARMv6 boards (Pi Zero / Pi 1).

Status: GPIO + hardware PWM verified on Raspberry Pi 5 and Raspberry Pi 4 (Trixie, libgpiod 2.x). The device API (LED / Button / Motor) is verified on Pi 5. Hardware PWM (sysfs) also works on a Bookworm Pi 4, where the libgpiod GPIO path is unavailable (v1). Multi-board support, the roadmap, and planned APIs are tracked in PLAN.md; released changes are recorded in CHANGELOG.md.

require "rgpio"

led = Rgpio::LED.new(4)              # GPIO4 = physical pin 7
button = Rgpio::Button.new(17)       # switch between GPIO17 and 3.3 V

button.when_pressed  { led.on }
button.when_released { led.off }

Rgpio.pause                          # run the callbacks until Ctrl-C

Requirements

  • OS: Debian Trixie (13) or later — verified target. Bookworm ships libgpiod 1.x, which is not supported.
  • Hardware: Raspberry Pi 5 and Pi 4 (verified). Other Pi models: see PLAN.md.
  • Library: libgpiod2 (>= 2.1)
  • Ruby: >= 3.3 (CRuby) — matches Trixie's default ruby

Install the runtime library on the Pi:

sudo apt update
sudo apt install libgpiod3

To verify libgpiod is available and working:

gpiodetect          # lists GPIO chips
gpioinfo --chip gpiochip0  # lists lines on chip 0

Installation

Add to your Gemfile:

gem "rgpio"

Or install directly:

gem install rgpio

Hello, world

Wire an LED with a 330 Ω resistor between GPIO4 (pin 7) and GND (pin 6), save this as blink.rb and run ruby blink.rb:

require "rgpio"

led = Rgpio::LED.new(4)

5.times do
  led.on
  sleep 0.5
  led.off
  sleep 0.5
end

led.close

If it fails with Errno::EACCES, add yourself to the gpio group (sudo usermod -aG gpio $USER, then log in again).

What's in the box

Area Classes Guide
Digital devices (gpiozero-style) LED, Button, RotaryEncoder, Motor Device API
PWM devices PWMLED, RGBLED, Servo Device API, Software PWM
Raw GPIO Chip, LineRequest GPIO Usage
Hardware PWM HardwarePWM Hardware PWM Usage
I2C I2C, ADT7410, ST7032 I2C Usage
SPI SPI, MCP3208, ILI9341 (colour TFT), XPT2046 (touch) SPI Usage

Every class is documented in full in docs/guide.md, with wiring notes, the API reference and how to run the examples in examples/. Devices written but still waiting on hardware verification (such as MotionSensor) are listed in PLAN.md.

Project status

0.1.0 is the first release. It is 0.x in earnest: everything documented has been exercised on real hardware, but the API may still change before 1.0.

  • Released changes: CHANGELOG.md
  • Roadmap, planned APIs, and multi-board validation status: PLAN.md

License

MIT