Software I2C library (master mode only)

The library can be downloaded from Github: https://github.com/felias-fogg/SoftI2CMaster.

Why another I2C library?

The standard I2C library for the Arduino is the Wire library. While this library is sufficient most of the time, there are situations when it cannot be used:

  • the I2C pins A4/A5 (or SDA/SCL) are in use already for other purposes,
  • the code shall run on both an ATmega processor with 16 MHz and an ATtiny processor with 1 MHz,
  • you are short on memory (flash and RAM).

I adapted Peter Fleury's I2C software library that is written in AVR assembler, extremely light weight and very fast. Even on an ATtiny running with 1MHz, the bus operates with 33 kHz, which implies that you can drive slave devices that use the SMBus protocol (which timeout if the the bus frequency is below 10 kHz). If you want a solution running on an ARM MCU, use the pure C++ library SlowSoftI2CMaster.

Features

  • compatible with all 8-bit AVR MCUs
  • can make use of almost any pin
  • clock stretching (by slaves) supported
  • timeout on clock stretching
  • timeout on ACK polling for busy devices (new!)
  • internal MCU pullup resistors can be used (new!)
  • very lightweight (roughly 250 bytes of flash and 0 byte of RAM, except for call stack)
  • very fast (standard and fast mode on UNO, 33 kHz with 1 MHz CPU clock)
  • Optional Wire library compatible interface
  • no bus arbitration (i.e., only one master allowed on bus)
  • supports only master mode
  • GPL license

Using the library

Just download the Zip-File from github, uncompress, rename the directory to SoftI2CMaster and move it into the libraries folder. In case you have not installed a library before, consult the the respective help page. The documentation can be found on Github as well.

Alternative Interface

Meanwhile, I have written a wrapper around SoftI2CMaster that emulates the Wire library (master mode only). It is another C++-header file called SoftWire.h which you need to include instead of SoftI2CMaster.h. Directly after this include statement you need to create a Wire instance.

This interface sacrifices some of the advantages of the original library, in particular its small footprint, but comes handy if you need a replacement of the original Wire library. The following table lists the memory requirements.

 WireSoftI2CMasterSoftWire
Flash memory1956252712
RAM208064