IC2 and MCP23017

If you are asking a question, please follow this template:

  1. My goal is: Be able to write to 16 GPIO, and read from another 16
  2. My actions are: No actions so far away, just need to know if I2C library would work with the MCP23017, and how many MCP23017 can I attach (in different address) through the I2C interface
  3. The result I see is:
  4. My expectation & question is: Is this possible?

Sure. I’ve used MCP23017 in a previous project.

Include the i2c library in mos.yml and you just need to initialize it with mongoose I2C functions, here’s my example:

Declare:

#include “mgos_i2c.h”

#define MCP23017 0x20

static void iot_mcp23017_init(){
struct mgos_i2c *i2c = mgos_i2c_get_global();
mgos_i2c_write_reg_b(i2c, MCP23017, 0x00, 0x00); //portA pins like outputs
mgos_i2c_write_reg_b(i2c, MCP23017, 0x01, 0xFF); //portB pins like inputs
mgos_i2c_write_reg_b(i2c, MCP23017, 0x0C, 0xF0); //no pull-ups
mgos_i2c_write_reg_b(i2c, MCP23017, 0x0D, 0x00); //no pull-ups
}

Example of a function to reset my 3G modem attached to MCP23017:

void reset_3g_modem(void){
struct mgos_i2c *i2c = mgos_i2c_get_global();
mgos_i2c_write_reg_b(i2c, MCP23017, 0x12, 0) //sets output to 1;
mgos_msleep(100);
mgos_i2c_write_reg_b(i2c, MCP23017, 0x12, 1 //sets back reset pin to 0);
}

You could also look for a MCP23017 library, it eases things up a bit.
For better compreehension, read the datasheet describing register functions.
Hope it helps.

Edit: You can alter inicialization in different initializing functions, setting as input or output.
There is a limitation for a single i2c address. You could try plugging several MCP23017 in the same address, maybe they’ll just mirror one another. I myself wouldn’t recommend, you can choose between ADD 0x20 and 0x21 for all I know

1 Like

it suports (jumpers), from 0x20 to 0x28 according to the data sheet (A0/1/2)