I2Cexpander - A collection of I2C I/O devices
Another SPCoast Library
I2C IO Expander wrapper library

Introduction

This library provides a common interface to a variety of I2C expanders, ADC/DAC devices and Arduino/Photon/Wemos/ESP platform-local pins.

The design philosophy is to abstract the setup and initialization of the various chipsets into an "init" function, and then provide high level "read" and "write" calls that do the right thing. It is built on top of the base Wire infrastructure, and coexists (but does not inter-operate) with other I2C device libraries.

This version is limited to a single I2C bus; it does not know how to manage/route through I2C muxes or switch between different MCU I2C appliances.

The digitalWrite/Read functions are convenience interfaces, but not very performant - You should use the Arduino provided ones for onboard pins if you need performance.

The support for PHOTON is rudimentary - their emulation of the Arduino environment is problematic.

It is based on an array of devices that can be read and written as desired. Instead of extending the digitalRead()/digitalWrite abstraction, I chose to read and write in units of 4,6, 8 or 16 bits, depending on the device in question.

In the model railroad community, this is slightly reminiscent of the Chubb CMRI system's design

To put this in context, this is part of a code-generated control system for a model railroad layout where there are many microcontrollers in use, one for every place on the layout where there are things to control.

My default program flow is

 define each layout device (signal heads, turnout controllers, occupancy detectors,...) along with the particular bits are used to talk to it.
 loop() {
     read the layout state
     walk thru every device and ask it to update itself
     if anything changed, handle the side effects (i.e., track becomes occupied, signal needs to turn red...)
     if needed, update outputs (i.e., write new values)
 }
 

The list of supported I2C expanders is

PCA9555 // Bits 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
MCP23016 // Bits 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
PCF8574 // Bits 0 1 2 3 4 5 6 7 8
PCF8574A // Bits 0 1 2 3 4 5 6 7 8
PCF8591 // 4 8-bit A/D converters, 1 8-bit D/A
MAX731x // Bits 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
PCA9685
Pseudo-expanders - can read and write built in pins (as digital I/O) as well:
// if defined(ARDUINO_AVR_DUEMILANOVE)
// built-in Arduino ports, skipping RX/TX, Lnet RX/TX and I2C pins
ARDIO_A // Pins D2 D3 D4 D5 - low digital
ARDIO_B // D6 D9 D10 D11 - high digital
ARDIO_C // D12 D13 A0 A1 - mixed, digital and analog
ARDIO_D // A2 A3 A6 A7 - analog (A6 & A7 are input only)
// endif
// if defined(SPARK_CORE) // Built in Photon Ports
PHOTON_A // Pins D2, D3, D4, D5, -- -- -- --
PHOTON_B // D6, D7, A0, A1, -- -- -- --
PHOTON_C // A2, A3, DAC, WKP, -- -- -- --
// endif
// if defined(ARDUINO_ESP8266_WEMOS_D1MINI)
WEMOS_A // Pins D2 D3 D4 D5 GPIO 4, 0, 2, 14
WEMOS_B // D6 D7 RX TX 12, 13, 3, 1
WEMOS_C // D0 D7 RX TX 16, 13, 3, 1 // Alternate
// endif
// if defined(ARDUINO_AVR_LEONARDO)
// 0 & 1 are CMRI Serial, 2 & 3 are I2C
CPNODE_LOW,
CPNODE_HIGH,
// endif

Dependencies

Arduino wire I2C implementation

Author

Written by John Plocher

License

Released under the terms of the MIT License (MIT)