I just got two of these 16 relay boards which use the MCP23016 controller and noticed that nobody had anything that really worked so today I wrote two simple programs that really work. This is a 12V board. There is no place to connect +5V from the I2C connector, so don't do it, you only need GND, SDA and SCL.
/******************************************************************************
Test Program for the 12CZXRELAY16 Board from inexglobal.com
16 Relay Scanner Night Rider Style (for the fun of it)
made by EdArmstrong@testelectronics.com
December 27th 2010
*
******************************************************************************/
#include <Wire.h>
//#define MCP23016_I2C_WRITE 0x40 //do not use, the chip is 7 bit so the library adds a LSB bit so 0x40 becomes 0x20
//#define MCP23016_I2C_READ 0x41 //do not use
#define MCP23016_I2C 0x20 //the library adds last bit so it makes it a 0x40
// COMMAND BYTE TO REGISTER RELATIONSHIP
#define GP0 0x00 // PORT0 first 8 relays
#define GP1 0x01 // PORT1 second 8 relays
#define OLAT0 0x02 // LATCH0 command to write on latch 0 first 8 relays
#define OLAT1 0x03 // LATCH1 command to write on latch 1 second 8 relays
#define IPOL0 0x04 // INPUT POLARITY PORT REGISTER 0
#define IPOL1 0x05 // INPUT POLARITY PORT REGISTER 1
#define IODIR0 0x06 // I/O DIRECTION REGISTER 0
#define IODIR1 0x07 // I/O DIRECTION REGISTER 1
#define INTCAP0 0x08 // INTERRUPT CAPTURE REGISTER 0
#define INTCAP1 0x09 // INTERRUPT CAPTURE REGISTER 1
#define IOCON0 0x0A // I/O EXPANDER CONTROL REGISTER 0
#define IOCON1 0x0B // I/O EXPANDER CONTROL REGISTER 1
#define PAUSE 100
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
write_io (IODIR0, 0x00); //sets direction to output
write_io (IODIR1, 0x00);
write_io (GP0, B00000000); //clears all relays
write_io (GP1, B00000000);
delay (PAUSE);
}
void loop()
{
write_io (GP0, B00000001);
delay (PAUSE);
write_io (GP0, B00000010);
delay (PAUSE);
write_io (GP0, B00000100);
delay (PAUSE);
write_io (GP0, B00001000);
delay (PAUSE);
write_io (GP0, B00010000);
delay (PAUSE);
write_io (GP0, B00100000);
delay (PAUSE);
write_io (GP0, B01000000);
delay (PAUSE);
write_io (GP0, B10000000);
delay (PAUSE);
write_io (GP0, B00000000);
write_io (GP1, B00000001);
delay (PAUSE);
write_io (GP1, B00000010);
delay (PAUSE);
write_io (GP1, B00000100);
delay (PAUSE);
write_io (GP1, B00001000);
delay (PAUSE);
write_io (GP1, B00010000);
delay (PAUSE);
write_io (GP1, B00100000);
delay (PAUSE);
write_io (GP1, B01000000);
delay (PAUSE);
write_io (GP1, B10000000);
delay (PAUSE);
write_io (GP1, B01000000);
delay (PAUSE);
write_io (GP1, B00100000);
delay (PAUSE);
write_io (GP1, B00010000);
delay (PAUSE);
write_io (GP1, B00001000);
delay (PAUSE);
write_io (GP1, B00000100);
delay (PAUSE);
write_io (GP1, B00000010);
delay (PAUSE);
write_io (GP1, B00000001);
delay (PAUSE);
write_io (GP1, B00000000);
write_io (GP0, B10000000);
delay (PAUSE);
write_io (GP0, B01000000);
delay (PAUSE);
write_io (GP0, B00100000);
delay (PAUSE);
write_io (GP0, B00010000);
delay (PAUSE);
write_io (GP0, B00001000);
delay (PAUSE);
write_io (GP0, B00000100);
delay (PAUSE);
write_io (GP0, B00000010);
delay (PAUSE);
write_io (GP0, B00000001);
}
void write_io(int command, int value)
{
Wire.beginTransmission(MCP23016_I2C);
Wire.send(command),Wire.send(value); // reset register pointer then set GP0 to push_this // use (0,??)
Wire.endTransmission();
}