0

MCP23008 - addressing more than one device - Arduino
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MCP23008 - addressing more than one device - Arduino

by TinkaTek on Thu Dec 19, 2019 9:59 pm

Hi,
I have two of MCP23008 on a board, connected by i2c to an Arduino Uno. Both of the MCP23008's have LEDs connected to their IO lines. One MCP23008 is set to address 0, the other to address 6. I can switch on/off the LEDs on the outputs, but have not been able to control all LEDs from within one sketch - i.e. I can set the address to 6, compile/upload the sketch and see the LEDs on chip two flash, but can not manage to make a sketch where I address both chips and make any LED on either chip flash.

I have used mcp.begin() or mcp.begin(0) or or mcp.begin(6) to set the addresses, but whichever command is last, is the only address which works. i.e. if in the SETUP i have mcp.begin(0) followed by mcp.begin(6), only the LEDs on chip 2/address 6 will flash.

I would like to use three of MCP23008 on one i2c line, but currently I can't even properly talk to two of MCP23008.

The code runs and the LEDs on one chip (or other) flash. The code is very simple and rather clunky, but until I can prove the basics, I can not go forward.

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include "Adafruit_MCP23008.h"
/*
Basic toggle test for i/o expansion. flips pins #0-6 of a MCP23008 i2c
pin expander up and down.

Connect pin #1 of the expander to Analog 5 (i2c clock)
Connect pin #2 of the expander to Analog 4 (i2c data)
Connect pins #3, 4 and 5 of the expander to ground (address selection) for address 0
Connect pin #6 and 18 of the expander to 5V (power and reset disable)
Connect pin #9 of the expander to ground (common ground)

Output #0 is on pin 10. LEDs connected from +v to pin, so LOW = ON
i/o 0 = red,1 = green, 2 = blue
i/o 3 = red, 4 = green, 5 = blue
2x MCP23008.
1st MCP23008 at address 0
2nd MCP23008 at address 6
*/

Adafruit_MCP23008 mcp;
 
void setup() {
  mcp.begin(6);      // Set chip address to 6, change to 0 for address 0
 
// All LEDs OFF 1st RGB LED
  mcp.digitalWrite(0, HIGH);
  mcp.digitalWrite(1, HIGH);
  mcp.digitalWrite(2, HIGH);

// All LEDs OFF 2nd RGB LED
  mcp.digitalWrite(3, HIGH);
  mcp.digitalWrite(4, HIGH);
  mcp.digitalWrite(5, HIGH);

// Set i/o pins to OUTPUT
  mcp.pinMode(0, OUTPUT);
  mcp.pinMode(1, OUTPUT); 
  mcp.pinMode(2, OUTPUT);
  mcp.pinMode(3, OUTPUT);
  mcp.pinMode(4, OUTPUT);   
  mcp.pinMode(5, OUTPUT);
}

// flip pins 0 - 5 up and down
void loop()
{
//1st RGB LED
// Red flash
  mcp.digitalWrite(0, LOW);
  delay(500);
  mcp.digitalWrite(0, HIGH);

//Green flash
  mcp.digitalWrite(1, LOW);
  delay(500);
  mcp.digitalWrite(1, HIGH);

// Blue flash
  mcp.digitalWrite(2, LOW);
  delay(500);
  mcp.digitalWrite(2, HIGH);

 
// 2nd RGB LED
// Red flash
  mcp.digitalWrite(3, LOW);
  delay(500);
  mcp.digitalWrite(3, HIGH);

// Green flash 
  mcp.digitalWrite(4, LOW);
  delay(500);
  mcp.digitalWrite(4, HIGH);

// Blue flash
  mcp.digitalWrite(5, LOW);
  delay(500);
  mcp.digitalWrite(5, HIGH);

delay(1500);
}
Help to do this would be very much appreciated.

Kind regards,
R.
Last edited by adafruit_support_mike on Fri Dec 20, 2019 1:38 am, edited 1 time in total.
Reason: added CODE tags

TinkaTek
 
Posts: 5
Joined: Thu Dec 19, 2019 6:49 pm

Re: MCP23008 - addressing more than one device - Arduino

by adafruit_support_mike on Fri Dec 20, 2019 1:42 am

You need two Adafruit_MCP23008 objects: one that talks to the chip with address 0x20, and one that talks to the chip with address 0x26.

adafruit_support_mike
 
Posts: 62768
Joined: Thu Feb 11, 2010 2:51 pm

Re: MCP23008 - addressing more than one device - Arduino

by TinkaTek on Fri Dec 20, 2019 5:02 am

Hello Mike,

My coding abilities are not sufficient to understand what you are telling me.

Please could you send me an example of how to include two (or ideally three) Adafruit_MCP23008 objects, or better still edit my code such that it can control the first 6 output lines of three of MCP23008's.

Many thanks and kindest regards,
R.

TinkaTek
 
Posts: 5
Joined: Thu Dec 19, 2019 6:49 pm

Re: MCP23008 - addressing more than one device - Arduino

by adafruit_support_mike on Fri Dec 27, 2019 12:47 am

We don't have any examples for that, and we aren't an on-demand coding service.

The best thing you can do is work through the existing examples one line at a time and figure out what each one does. Get comfortable with what the code is doing, then start writing your own versions with small changes and see if they do what you expect. Along the way, you'll acquire the understanding you need to do what you want.

adafruit_support_mike
 
Posts: 62768
Joined: Thu Feb 11, 2010 2:51 pm

Re: MCP23008 - addressing more than one device - Arduino

by TinkaTek on Fri Jan 03, 2020 6:42 pm

I have been trying to do the right thing from the beginning.
I have bought genuine Adafruit boards from a re-seller in New Zealand, so am not expecting help with equipment sourced from cheap third-party suppliers.
I spent a considerable time attempting to control the MCP23008 devices with separate addresses, using the approach that you suggested - i.e. line-by-line editing of the code examples, but was only ever able to control one device. Being able to switch on/off an I/O line on a MCP23008 device does not need a lot of code, so the examples did not give me a lot to go on and ultimately I was not successful. In spite of spending considerable time searching the Internet, I could not find any other command or the modifier to allow the existing command to do it. Perhaps I could have "bit-bashed" it by using the deeper data in the device data sheet - but I was concerned that I would be introducing more unfamiliar variables into my new learning experience, I was sure that the Adafruit library would allow for the control of more than one device, so I finally did what perhaps I should have done at the very beginning and made direct contact through your forum.

Yes - I do not expect you to write my code for me, but I think it likely that the magic command to selectively command the several MCP23008 devices is a matter of understanding how to use the given commands - but I have not been able to find any practical examples or information regarding the necessary semantics.

Please help me out with that golden nugget that will help me realise my dream-project.
R.

TinkaTek
 
Posts: 5
Joined: Thu Dec 19, 2019 6:49 pm

Re: MCP23008 - addressing more than one device - Arduino

by kcl1s on Fri Jan 03, 2020 9:06 pm

Here is the example code with 2 mpc23008 chips. This should give the syntax you need to get you going. Just like regular variables each object in C++ needs a unique name.

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include "Adafruit_MCP23017.h"

// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!

// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)

// Output #0 is on pin 21 so connect an LED or whatever from that to ground

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

  mcp.pinMode(0, OUTPUT);
  mcp2.pinMode(0, OUTPUT);
 
}


// flip the pin #0 up and down

void loop() {
  delay(100);

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

  delay(100);

  mcp.digitalWrite(0, LOW);
  mcp2.digitalWrite(0, LOW);
}

Fellow hobbyist
Keith

kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: MCP23008 - addressing more than one device - Arduino

by TinkaTek on Wed May 05, 2021 5:32 pm

Hi Keith,

MANY THANKS for your all-important tip - which with a bit of minor tweaking to suit the MCP2003 worked perfectly!

My sincere apologies for delaying my thanks for over a year, but a few things happened in the interim, but now life is great - particularly here in New Zealand.

Thanks again for the tiny, vital nugget of information that I so desperately needed - but was so strangely NOT forthcoming from the very place that ostensibly exists for just that purpose.

I hope that all is good with you, and thank you for taking pity on me, and for helping me in my hour of need.

Kindest regards,
Ross.

TinkaTek
 
Posts: 5
Joined: Thu Dec 19, 2019 6:49 pm

Re: MCP23008 - addressing more than one device - Arduino

by TinkaTek on Wed May 05, 2021 6:03 pm

Here is my final code, which could easily be modified to run up to eight MCP23008 - the maximum possible with the 3 address lines on the MCP23008.

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include "Adafruit_MCP23008.h"
/*
  Sketch name i2c_MCP23008x2_RGB_LEDsx4.ino
  2x MCP23008, 8 I/O lines each, only 6 used,connected to 2x RGB LEDs.
  All I/O lines set to O/P
  Basic toggle test for i/o expansion chips. flips pins #0-5 of 2x MCP23008 i2c
  pin expander up and down.

  Connect pin #1 of the MCP23008 to Uno Analog 5 (i2c clock)
  Connect pin #2 of the MCP23008 to Uno Analog 4 (i2c data)
  Connect pins #3, 4 and 5 of the first MCP23008 to Uno ground (address selection) for address 0
  Connect pin #5 of the second MCP23008 to Uno ground (address selection) for address 6 (pins 3 and 4 pulled high)
  Connect pin #6 and 18 of the MCP23008 to Uno 5V (power and reset disable)
  Connect pin #9 of the MCP23008 to Uno ground (common ground)

  MCP23008 output #0 is on pin 10. LEDs connected from +v to o/p pin, so LOW = ON
  MCP23008 i/o 0 = red, 1 = green, 2 = blue  RGB LED 1
  MCP23008 i/o 3 = red, 4 = green, 5 = blue  RGB LED 2

  2x MCP23008.
  1st MCP23008 at i2c address 0
  2nd MCP23008 at i2c address 6
*/

Adafruit_MCP23008 mcp1;  // The missing nugget - 1st half
Adafruit_MCP23008 mcp2;  // The missing nugget - 2nd half  - willfully and spitefully withheld

void setup()
{
  Serial.begin(9600);               // Set Baud rate to send messages to the PC serial monitor
  mcp1.begin(0);      // Set first MCP23008 chip address to address 0
  mcp2.begin(6);      // Set second MCP23008 chip address to 6

  // All LEDs OFF 1st RGB LED
  mcp1.digitalWrite(0, HIGH);
  mcp1.digitalWrite(1, HIGH);
  mcp1.digitalWrite(2, HIGH);

  // All LEDs OFF 2nd RGB LED
  mcp1.digitalWrite(3, HIGH);
  mcp1.digitalWrite(4, HIGH);
  mcp1.digitalWrite(5, HIGH);

  // Set i/o pins of 1st MCP23008 to OUTPUT
  mcp1.pinMode(0, OUTPUT);
  mcp1.pinMode(1, OUTPUT);
  mcp1.pinMode(2, OUTPUT);
  mcp1.pinMode(3, OUTPUT);
  mcp1.pinMode(4, OUTPUT);
  mcp1.pinMode(5, OUTPUT);


  // All LEDs OFF 3rd RGB LED
  mcp2.digitalWrite(0, HIGH);
  mcp2.digitalWrite(1, HIGH);
  mcp2.digitalWrite(2, HIGH);

  // All LEDs OFF 4th RGB LED
  mcp2.digitalWrite(3, HIGH);
  mcp2.digitalWrite(4, HIGH);
  mcp2.digitalWrite(5, HIGH);

  // Set i/o pins of 2nd MCP23008 to OUTPUT
  mcp2.pinMode(0, OUTPUT);
  mcp2.pinMode(1, OUTPUT);
  mcp2.pinMode(2, OUTPUT);
  mcp2.pinMode(3, OUTPUT);
  mcp2.pinMode(4, OUTPUT);
  mcp2.pinMode(5, OUTPUT);
}


void loop()
{
  // Sequentially flashes R, G, B, of 4x RGB LEDs connected to 2x MCP23008
  Serial.println("i2c_MCP23008x2_RGB_LEDsx4.INO"); // Print the firmware version to the console
  //  - so you always know the name of the sketch that you're dealing with!

  // 2nd RGB LED  Air
  // Red flash
  mcp1.digitalWrite(3, LOW);
  delay(500);
  mcp1.digitalWrite(3, HIGH);

  // Green flash
  mcp1.digitalWrite(4, LOW);
  delay(500);
  mcp1.digitalWrite(4, HIGH);

  // Blue flash
  mcp1.digitalWrite(5, LOW);
  delay(500);
  mcp1.digitalWrite(5, HIGH);

  //1st RGB LED   PUMP
  // Red flash
  mcp1.digitalWrite(0, LOW);
  delay(500);
  mcp1.digitalWrite(0, HIGH);

  //Green flash
  mcp1.digitalWrite(1, LOW);
  delay(500);
  mcp1.digitalWrite(1, HIGH);

  // Blue flash
  mcp1.digitalWrite(2, LOW);
  delay(500);
  mcp1.digitalWrite(2, HIGH);


  // 4th RGB LED   FLOAT
  // Red flash
  mcp2.digitalWrite(3, LOW);
  delay(500);
  mcp2.digitalWrite(3, HIGH);

  // Green flash
  mcp2.digitalWrite(4, LOW);
  delay(500);
  mcp2.digitalWrite(4, HIGH);

  // Blue flash
  mcp2.digitalWrite(5, LOW);
  delay(500);
  mcp2.digitalWrite(5, HIGH);


  // Second MCP23008
  //3rd RGB LED         HEARTBEAT
  // Red flash
  mcp2.digitalWrite(0, LOW);
  delay(500);
  mcp2.digitalWrite(0, HIGH);

  //Green flash
  mcp2.digitalWrite(1, LOW);
  delay(500);
  mcp2.digitalWrite(1, HIGH);

  // Blue flash
  mcp2.digitalWrite(2, LOW);
  delay(500);
  mcp2.digitalWrite(2, HIGH);

  delay(100);
}

TinkaTek
 
Posts: 5
Joined: Thu Dec 19, 2019 6:49 pm

Please be positive and constructive with your questions and comments.