I2C - Ruby I2C library


Interface to Linux I2C (a.k.a. TWI) implementations. Right now targeted at the Raspberry Pi, but should work with any linux i2c-dev I2C-hardware.


The library is split into two parts:

  • A backend for accessing the I2C bus (right now only through the i2c-dev

in Linux, other impementations are possible).

  • Drivers for I2C enabled ICs.


To use the i2c-dev backend it is necessary to load the “i2c-dev” linux kernel module. This is not done automatically even if the module for the underlying I2C-hardware is. To automatically load the i2c-dev driver on startup add it to /etc/modules. Also the device file (usually /dev/i2c-0) must be user accessible. A working (at least on a RaspberryPi running Raspbian) udev rule file is available (rules/88-i2c.rules) and can be installed to /etc/udev/rules.d/.


The backends are instantiated through the #I2C::create method. depending on the format of the passed bus descriptor the correct backend is invoked.

Right now there is only a i2c-dev backend available

i2c-dev Backend

Backend for the Linux I2C implementation. Accepts device file names as bus descriptors. E.g.

I2C.create(“/dev/i2c-0”) returns an instance of I2C::Dev attached to the first I2c bus on the system.



16 bit IO-Expander MCP23017 from Microchip. Provides a wiringpi-ruby compatible API and may therefore be used as a drop-in replacement for IO tasks on a Raspberry Pi. Datasheet: ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf


8 bit IO-Expander MCP23008 from Microchip. Basically a small version of the MCP23017. Datasheet: ww1.microchip.com/downloads/en/DeviceDoc/21919e.pdf


The low-level IO (mainly in i2c-dev.rb) was extracted from Ruby-I2C (rubyforge.org/projects/i2c/) by Jonas Bähr <[email protected]>



  • Pierre Paques

Copyright / Licence

This code may be used under the terms of the GNU General Public Licence, Version 2.

Copyright © 2018 David Bailey <[email protected]> Copyright © 2012 Christoph Anderegg <[email protected]> Copyright © 2008 Jonas Bähr <[email protected]>