Arduino + multiple MCP23017 and i2c address

Hi to all
As English is not my Mother language and i am a self taught to programming i have issues to understand how to set up the address for multiple MCP23017 ic.

I am using the Adafruit-MCP23017-Arduino-Library for my project

The code below works fine as i used it to play with my setup (it is the default example from the library with some little changes in the pins number and how led will work)

// Controls an LED via an attached button.

// ok to include only the one needed
// both included here to make things simple for example
//#include <Adafruit_MCP23X08.h>
#include <Adafruit_MCP23X17.h>

#define LED_PIN 7     // MCP23XXX pin LED is attached to
#define BUTTON_PIN 8  // MCP23XXX pin button is attached to

// only used for SPI
//#define CS_PIN 6

// uncomment appropriate line
//Adafruit_MCP23X08 mcp;
Adafruit_MCP23X17 mcp;

void setup() {
  Serial.begin(9600);
  //while (!Serial);
  Serial.println("MCP23xxx Combo Test!");

  // uncomment appropriate mcp.begin
  if (!mcp.begin_I2C()) {
    //if (!mcp.begin_SPI(CS_PIN)) {
    Serial.println("Error.");
    while (1);
  }

  // configure LED pin for output
  mcp.pinMode(LED_PIN, OUTPUT);

  // configure button pin for input with pull up
  mcp.pinMode(BUTTON_PIN, INPUT);

  Serial.println("Looping...");
}

void loop() {
  if (mcp.digitalRead(BUTTON_PIN) == HIGH) {
    Serial.println("is HIGH");
    mcp.digitalWrite(LED_PIN, HIGH);
  }

  else if (mcp.digitalRead(BUTTON_PIN) == LOW) {
    Serial.println("is LOW");
    mcp.digitalWrite(LED_PIN, LOW);
  }
//  mcp.digitalWrite(LED_PIN, !mcp.digitalRead(BUTTON_PIN));
}

The MCP23017 to the above example has the address setup to the 000 (i2c = 0x20) based on the table below

MCP23017-addresspins1-300x203

How to add a second MCP23017 (lets say to the chip address 001 (i2c = 0x21) ??

thanks in advance

The board has address pins or jumpers for Aß to A3. Wire these pins as indicated in the table.

Hello,

You can put the address in the begin function, for example mcp2.begin_I2C( 0x21 )

Always look at the class definition to see all available functions and their parameters : Adafruit-MCP23017-Arduino-Library/Adafruit_MCP23XXX.h at master · adafruit/Adafruit-MCP23017-Arduino-Library · GitHub

1 Like

@caslor,
Combine the answers given by DrD and guix.

  • you need to tell each 23017 what it's address is, by setting jumpers on it (i.e. a hardware identity needs to be set);
  • you need to talk to the right address each time you talk to the 23017s(i.e. you need to talk to the right identity).
    Work out both of those, and you should succeed.
    C

Thank you all to find the time to answer me :slight_smile:

I knew all ready how to setup the address to the mcp23017 but thanks mention it again in case i had oversight it

the :
mcp2.begin_I2C( 0x21 )
and the :
mcp1.begin_I2C( 0x20 )

did the work i need it

thanks again all

(i edit it to correct a type in mcp1 and mcp2 addresses )

1 Like

Hi,
Did you at any stage GOOGLE;

MCP23017 using more than one arduino

Its surprising what you will find.

Glad to see you have it running.
Can I ask what your project is with all the I/O?

Tom... :smiley: :+1: :australia: :coffee:

Hi TomGeorge
Yes i Google it before but nothing was clear (or at least at the level that could understand it)

i need so many I/O as i want to try to make a some kind of a puzzle-table with lot of switches

Shouldn't you use mcp1 and mcp2 for the dfferent addresses?

yes..... typo error when i wrote it here the code (i will edit it for a future user to see it and not make a mistake)

thanks for the observation

When googling this topic you get alot of old code from when the library still used "mcp1.begin(0); mcp2.begin(1); mcp3.begin(2);" etc instead of "mcp1.begin_I2c(0x20);" and the example included on the libraries own github page doesn't even have an example showing the use of multiple chips, infact this HERE forum post is the only reference i found with the proper updated coding. Also why in the world would he google "MCP23017 using more than one arduino" when he needs to use multiple mcp23017 with a single arduino, dont put people down when you have no idea what your talking about.

I thought the same thing, but google doesn't read a sentence and make sense of it, so it was really exactly equivalent to

  arduino using more than one MCP23017

As usual, adding "arduino" to any search (anywhere!) will focus results applicable to the arduino.

a7

1 Like

Google is very robust so that's likely true. Still the first point I listed is far more relevant as to why the op couldn't just "google" and magically find the solution, I also went to the github library and requested that they add an example for using multiple mcp chips.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.