Using I2C for multiple Sparkfun I/O Expander

Hello,

For my kid’s simulator cockpit project (many buttons and many leds), I tried to use multiple Sparkfun I/O Expander. (SX1509 I/O Expander Breakout Hookup Guide - learn.sparkfun.com)

With the code below, I tried to test by using 2 I/O Expanders and many leds.
=> The I2C test message for “begin” for each I/O Exoander are correct.
So they were detected by the arduino.

=> But only one of two Expander is in function.
According to me, in the code below I didn’t affect pins to the good I2C adress and the I/O expander weren’t correctly begun.

At this point of my exploration (internet, documentations,…) I can’t figure out how to affect Pin mode to another I2C adress.
=> Is someone can help me?

Thanks by advance.

//////////////////////////////////////

#include <Wire.h> // Include the I2C library (required)
#include <SparkFunSX1509.h> // Include SX1509 library
#include <SoftwareSerial.h>

SX1509 io; // Create an SX1509 object

// Pins for the 1st I/O Expander at adress 0x3E
const int SX1509_0x3E_PIN0 = 0;
const int SX1509_0x3E_PIN1 = 1;
const int SX1509_0x3E_PIN2 = 2;
const int SX1509_0x3E_PIN3 = 3;
const int SX1509_0x3E_PIN4 = 4;
const int SX1509_0x3E_PIN5 = 5;
const int SX1509_0x3E_PIN6 = 6;
const int SX1509_0x3E_PIN7 = 7;
const int SX1509_0x3E_PIN8 = 8;
const int SX1509_0x3E_PIN9 = 9;
const int SX1509_0x3E_PIN10 = 10;
const int SX1509_0x3E_PIN11 = 11;
const int SX1509_0x3E_PIN12 = 12;
const int SX1509_0x3E_PIN13 = 13;
const int SX1509_0x3E_PIN14 = 14;
const int SX1509_0x3E_PIN15 = 15;

// Pins for the 2nd I/O Expander at adress 0x3F
const int SX1509_0x3F_PIN0 = 0;
const int SX1509_0x3F_PIN1 = 1;
const int SX1509_0x3F_PIN2 = 2;
const int SX1509_0x3F_PIN3 = 3;
const int SX1509_0x3F_PIN4 = 4;
const int SX1509_0x3F_PIN5 = 5;
const int SX1509_0x3F_PIN6 = 6;
const int SX1509_0x3F_PIN7 = 7;
const int SX1509_0x3F_PIN8 = 8;
const int SX1509_0x3F_PIN9 = 9;
const int SX1509_0x3F_PIN10 = 10;
const int SX1509_0x3F_PIN11 = 11;
const int SX1509_0x3F_PIN12 = 12;
const int SX1509_0x3F_PIN13 = 13;
const int SX1509_0x3F_PIN14 = 14;
const int SX1509_0x3F_PIN15 = 15;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  pinMode(13, OUTPUT); // Use pin 13 LED as debug output
  digitalWrite(13, LOW); // Start it as low

  // Call io.begin(<I2C address>) to initialize the I/O expander.
  if (!io.begin(0x3E))  {
    Serial.println("0x3E undetected");
  }
  if (io.begin (0x3E)) {
    Serial.println("0x3E OK");
  }
  /////////////////////////
  if (!io.begin(0x3F))  {
    Serial.println("0x3F undetected");
  }
  if (io.begin (0x3F)) {
    Serial.println("0x3F OK");
  }

  // Pinmode for the 1st I/O Expander at adress 0x3E
  io.pinMode(SX1509_0x3E_PIN0, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN1, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN2, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN3, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN4, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN5, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN6, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN7, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN8, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN9, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN10, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN11, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN12, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN13, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN14, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN15, OUTPUT);

  // pinmode for the 2nd I/O Expander at adress 0x3F
  io.pinMode(SX1509_0x3F_PIN0, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN1, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN2, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN3, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN4, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN5, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN6, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN7, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN8, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN9, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN10, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN11, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN12, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN13, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN14, OUTPUT);
  io.pinMode(SX1509_0x3F_PIN15, OUTPUT);

}

void loop()
{
  // Light main test led
  digitalWrite(13, HIGH);
  
  // Light all Led on 1st I/O Expander at adress 0x3E
  io.digitalWrite(SX1509_0x3E_PIN1, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN2, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN3, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN4, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN5, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN6, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN7, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN8, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN9, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN10, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN11, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN12, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN13, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN14, HIGH);
  io.digitalWrite(SX1509_0x3E_PIN15, HIGH);

  // Light all Led on 2nd I/O Expander at adress 0x3F
  io.digitalWrite(SX1509_0x3F_PIN0, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN1, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN2, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN3, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN4, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN5, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN6, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN7, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN8, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN9, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN10, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN11, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN12, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN13, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN14, HIGH);
  io.digitalWrite(SX1509_0x3F_PIN15, HIGH);

  delay(500);

  // Unlight main test led
  digitalWrite(13, LOW);
  
  // Unlight all Led on 1st I/O Expander at adress 0x3E
  io.digitalWrite(SX1509_0x3E_PIN0, LOW);
  io.digitalWrite(SX1509_0x3E_PIN1, LOW);
  io.digitalWrite(SX1509_0x3E_PIN2, LOW);
  io.digitalWrite(SX1509_0x3E_PIN3, LOW);
  io.digitalWrite(SX1509_0x3E_PIN4, LOW);
  io.digitalWrite(SX1509_0x3E_PIN5, LOW);
  io.digitalWrite(SX1509_0x3E_PIN6, LOW);
  io.digitalWrite(SX1509_0x3E_PIN7, LOW);
  io.digitalWrite(SX1509_0x3E_PIN8, LOW);
  io.digitalWrite(SX1509_0x3E_PIN9, LOW);
  io.digitalWrite(SX1509_0x3E_PIN10, LOW);
  io.digitalWrite(SX1509_0x3E_PIN11, LOW);
  io.digitalWrite(SX1509_0x3E_PIN12, LOW);
  io.digitalWrite(SX1509_0x3E_PIN13, LOW);
  io.digitalWrite(SX1509_0x3E_PIN14, LOW);
  io.digitalWrite(SX1509_0x3E_PIN15, LOW);

  // Unlight all Led on 2nd I/O Expander at adress 0x3F
  io.digitalWrite(SX1509_0x3F_PIN1, LOW);
  io.digitalWrite(SX1509_0x3F_PIN2, LOW);
  io.digitalWrite(SX1509_0x3F_PIN3, LOW);
  io.digitalWrite(SX1509_0x3F_PIN4, LOW);
  io.digitalWrite(SX1509_0x3F_PIN5, LOW);
  io.digitalWrite(SX1509_0x3F_PIN6, LOW);
  io.digitalWrite(SX1509_0x3F_PIN7, LOW);
  io.digitalWrite(SX1509_0x3F_PIN8, LOW);
  io.digitalWrite(SX1509_0x3F_PIN9, LOW);
  io.digitalWrite(SX1509_0x3F_PIN10, LOW);
  io.digitalWrite(SX1509_0x3F_PIN11, LOW);
  io.digitalWrite(SX1509_0x3F_PIN12, LOW);
  io.digitalWrite(SX1509_0x3F_PIN13, LOW);
  io.digitalWrite(SX1509_0x3F_PIN14, LOW);
  io.digitalWrite(SX1509_0x3F_PIN15, LOW);

  delay(500);
}

Here is the Aarduino library used for this code :

https://github.com/sparkfun/SparkFun_SX1509_Arduino_Library

I haven’t used one of these things, but I rather suspect that you need two of these:

SX1509 io; // Create an SX1509 object

One for each address. Different names of course.

Thanks for your answer.

I've tried to create multiple "io" (io1 & io2)

//////// SX1509 io1; // Create an SX1509 object SX1509 io2; // Create an SX1509 object

//////////

But The compilation error message was

"Many libraries found for "SparkFunSX1509.h"

So i deduct that one library could be used once and for only one object? Perhaps i did wrong?

One library. Multiple objects instances can be created with that one library, so long as each object has a unique name.

Post the code that gave you that error.

Thanks a lot it works like it does.

Here the code (if another people have the same situation)

#include <Wire.h> // Include the I2C library
#include <SparkFunSX1509.h> // Include SX1509 library
#include <SoftwareSerial.h>

SX1509 io; // Create an SX1509 object for the 1st I/O Expander at adress 0x3E
SX1509 io2; // Create an SX1509 object for the 2nd I/O Expander at adress 0x3F
//... ect if yoy have more than 2 I/O Expenders


// Pins for the 1st I/O Expander at adress 0x3E
const int SX1509_0x3E_PIN0 = 0;
const int SX1509_0x3E_PIN1 = 1;
...
const int SX1509_0x3E_PIN15 = 15;

// Pins for the 2nd I/O Expander at adress 0x3F
const int SX1509_0x3F_PIN0 = 0;
const int SX1509_0x3F_PIN1 = 1;
...
const int SX1509_0x3F_PIN15 = 15;



void setup()
{
  Serial.begin(9600);
  Wire.begin();
  pinMode(13, OUTPUT); // Use pin 13 LED as debug output
  digitalWrite(13, LOW); // Start it as low

  // Call io.begin(<I2C address>) to initialize the I/O expander.
  if (!io.begin(0x3E))  {
    Serial.println("0x3E undetected");
  }
  if (io.begin (0x3E)) {
    Serial.println("0x3E OK");
  }
  ///////////////////////// start with second object and second adress
  if (!io2.begin(0x3F))  {
    Serial.println("0x3F undetected");
  }
  if (io2.begin (0x3F)) {
    Serial.println("0x3F OK");
  }

  // Pinmode for the 1st I/O Expander at adress 0x3E
  io.pinMode(SX1509_0x3E_PIN0, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN1, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN2, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN3, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN4, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN5, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN6, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN7, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN8, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN9, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN10, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN11, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN12, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN13, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN14, OUTPUT);
  io.pinMode(SX1509_0x3E_PIN15, OUTPUT);

  // pinmode for the 2nd I/O Expander at adress 0x3F
  io2.pinMode(SX1509_0x3F_PIN0, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN1, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN2, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN3, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN4, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN5, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN6, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN7, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN8, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN9, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN10, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN11, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN12, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN13, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN14, OUTPUT);
  io2.pinMode(SX1509_0x3F_PIN15, OUTPUT);

}

loop

{
...
}