chrissbarr / PCAL9535A-Arduino-Library

An Arduino library for the PCAL9535A GPIO Expander IC
GNU General Public License v3.0
2 stars 0 forks source link

Multiple I2C interfaces #15

Open mateusz-kusmierz opened 2 years ago

mateusz-kusmierz commented 2 years ago

Hello. How do I use this library on other than default I2C interface? I'm using teensy 4.1 board that has multiple I2C interfaces, but I don't know how to initialize this library on other I2C interfaces.

chrissbarr commented 2 years ago

Hi @mateusz-kusmierz ,

I don't have a Teensy 4.1 on hand, so can't test this properly. I've put together an example I believe should work (#16), which is as below:

#include <Arduino.h>
#include "i2c_driver_wire.h"
#include "PCAL9535A.h"

/**
 * This example uses the multiple I2C interfaces on the Teensy 4.1 board.
 * Uses the teensy4_i2c library here:
 * https://github.com/Richard-Gemmell/teensy4_i2c 
 * 
 * (I2C peripherals are exposed as Wire, Wire1, and Wire2)
 */

PCAL9535A::PCAL9535A<TwoWire> gpio1(Wire);
PCAL9535A::PCAL9535A<TwoWire> gpio2(Wire1);
PCAL9535A::PCAL9535A<TwoWire> gpio3(Wire2);

void printAllGPIO(PCAL9535A::PCAL9535A<TwoWire>& gpio) {
  int state = gpio.readGPIO16();  
  for (int i = 0; i < 16; i++) {
    bool b = state & (1 << 15);
    Serial.print(b);
    state = state << 1;
  }
  Serial.println();
}

void setup() {  
  gpio1.begin(PCAL9535A::HardwareAddress::A000);  // 0x20 - Pins = 000
  gpio2.begin(PCAL9535A::HardwareAddress::A000);
  gpio3.begin(PCAL9535A::HardwareAddress::A000);
  Serial.begin(9600);
}

void loop() {
  Serial.println("");
  Serial.print("GPIO1: "); printAllGPIO(gpio1);
  Serial.print("GPIO2: "); printAllGPIO(gpio2);
  Serial.print("GPIO3: "); printAllGPIO(gpio3);
  delay(100);
}

This is using the Teensy 4.1 I2C Library here: https://github.com/Richard-Gemmell/teensy4_i2c.git

I am unsure if you are using that library or if there are alternatives. Let me know if there's something else you're using and I can try that too.

The above example builds for me and seems like it should work. If you do give it a go, can you let me know if it works for you?