MCP23017 Library Port

You’ll need to instantiate an instance of the class for each MCP23017 and use the address you assigned them in the mcp.begin(addr) routine. You may not have noticed but it’s overloaded to also take a parameter, the address.

Here’s the link to the source:
https://github.com/pkourany/Adafruit_MCP23017_IDE/blob/master/firmware/Adafruit_MCP23017.cpp#L110

If you’re using the online code editor, there’s a little github icon that shows next to Library name once its selected. This takes you right to the firmware source for the library.

1 Like

Thanks for your reply @jbeacon !
I suppose that’s also what @ScruffR explained in the new topic I started (LINK)

I will do my best, but I’m not sure if I can figure it out how exactly to do this.
Actually, I was hoping for an example… :wink:

PS: I’m only a hardware engineer who has programming in “wiring” as a hobby…

Great @jbeacon and @ScruffR !
I scrolled through the library: it’s a masterpiece!
But it goes far beyond my capabilities to interpret how to use all possibilities without an example…

If I take one of the existing examples “toggle.ino” (LINK), I would like to try it out with a second MCP23017 chip.

So, the first thing I do is set an address with the three pins:

Pin 17 Pin 16 Pin 15 HEX Address
0 0 0 0x20
0 0 1 0x21
0 1 0 0x22
0 1 1 0x23
1 0 0 0x24
1 0 1 0x25
1 1 0 0x26
1 1 1 0x27

Below is part of the “toggle.ino” sketch.
It says: “// use default address 0”

I guess the default address “0” mentioned in the example is actually HEX address “0x20”, right?
Suppose I select for the second chip address “0x21”, that must then be address “1” to use in my sketch. OK?

The questions for me are:

  1. How do I indicate both address 1 and 2 for my two chips
  2. How do I repeat the same commands for the corresponding pins on the two chips?

Adafruit_MCP23017 mcp;

void setup()
{
mcp.begin(); // use default address 0

mcp.pinMode(0, OUTPUT);
}

// flip the pin #0 up and down

void loop()
{
delay(100);

mcp.digitalWrite(0, HIGH);

delay(100);

mcp.digitalWrite(0, LOW);
}


Adafruit_MCP23017 mcp;
Adafruit_MCP23017 secondmcp;
mcp.begin(); //implicit (0)
secondmcp.begin(1);

2 Likes

Thanks @MORA and @peter_a ! (LINK)

So, to summarize how I understand it:
For the above “toggle.ino” sketch I will change as shown in BOLD:


Adafruit_MCP23017 mcp;
Adafruit_MCP23017 secondmcp;

void setup()
{

mcp.begin(); //implicit (0)
secondmcp.begin(1);

mcp.pinMode(0, OUTPUT);
mcp.pinMode(16, OUTPUT);
// I assume chip 1 has pins 0 - 15 and chip 2 has pins 16 - 31

}

// flip the pin #0 and #16 up and down

void loop()
{
delay(100);

mcp.digitalWrite(0, HIGH);
mcp.digitalWrite(16, HIGH);

delay(100);

mcp.digitalWrite(0, LOW);
mcp.digitalWrite(16, LOW);

}


Do you think this is correct?

@FiDel, the MCP23017 are not “aware” of each other so each of their pins are 0-15.

2 Likes

As @peekay123 notes, each mcp unit has 0-15, if you want to use higher pin numbers, you need to write that abstraction yourself.

Adafruit_MCP23017 mcp;
Adafruit_MCP23017 secondmcp;

void setup()
{

mcp.begin(); //implicit (0)
secondmcp.begin(1);

mcp.pinMode(0, OUTPUT);
secondmcp.pinMode(0, OUTPUT);
// I assume chip 1 has pins  0 - 15 and chip 2 has pins 16 - 31

}

// flip the pin #0 and #16 up and down

void loop()
{
delay(100);

mcp.digitalWrite(0, HIGH);
secondmcp.digitalWrite(0, HIGH);

delay(100);

mcp.digitalWrite(0, LOW);
secondmcp.digitalWrite(0, LOW);

}
1 Like

OK! It works!

Now that is indeed logical, but I have not enough background (yet) to figure that out!
This is a great community!

Sorry for creating this new post @ScruffR , but I believe it can help others to find easier how to do this…

I wish I could invite all who helped to a good Belgian beer tonight!
I’ll drink one to your health :wink:

Thanks @peekay123 @MORA @peter_a @jbeacon @ScruffR !

4 Likes

Sorry to bump this thread but is there any way to change the wirespeed for these calls (i need it to be as fast as possible) and perhaps do digitalWriteFast against the chip?

I want to use one of these to control 16 triac devices for ac dimming so increasing the speed of read and writes would be great. The triac relies on millisecond timing so i need to speed my calls up as much as possible

Jamie

@bigjme, the fastest wire speed supported by the Photon is 400KHz.

All digital writes via the MCP23017 are done via a set of I2C commands so there is no "digitalWriteFast" that can be done.

There is, however, an SPI version of the chip (MCP23S17) that can handle 10MHz SPI. I believe I have seen an SPI library for this version of the chip. At that speed, you could control all 16 channels within the target timeframe.

@peekay123, thanks for your reply

I will have to have a look for the SPI code, for now is the wirespeed still set using the following

Wire.setSpeed(CLOCK_SPEED_400KHZ);

I got a little confused as you instigate the begin using an instance of the class

Jamie

@bigjme, yes, that is the way to change the speed. The docs about setClockSpeed() say:

Sets the I2C clock speed. This is an optional call (not from the original Arduino specs.) and must be called once before calling begin(). The default I2C clock speed is 100KHz and the maximum clock speed is 400KHz.

@peekay123, many thanks :slight_smile:

1 Like