Multiple PCA9635

Has anyone had any luck with running multiple PCA9635's? I've got one running just fine using RAMBO's code here: GitHub - rambo/pca9635: Arduino library for controlling PCA9635 I2C LED drivers

The issue is that I can't find a way to address a second unit. I mean I CAN but if I define a second unit right below the initial definition as shown below (driver.begin(97, false):wink: the code for 96 stops working. I assume that has to do with the standard format of the Wire library being to:

Wire.beginTransmission(44); // transmit to device #44
  Wire.write(byte(0x00)); // sends instruction byte
  Wire.write(val);        // sends potentiometer value byte
  Wire.endTransmission(); // stop transmitting
// Get this from https://github.com/rambo/I2C
#include <I2C.h> // For some weird reason including this in the relevant .h file does not work
#include <i2c_device.h> // For some weird reason including this in the relevant .h file does not work
#include <pca9635.h>
#define I2C_DEVICE_DEBUG
// Container for the device
pca9635 driver;

void setup()
{
    Serial.begin(115200);
    // Use the SW-Reset address to reset all pca9635 ICs on the bus

    // Initialize I2C library manually
    I2c.begin();
    I2c.timeOut(500);
    PCA9635.reset(); // Incidentally this global name is a pca9635 instance bound to the generla "all-call" address so it can be used to set global parameters
    // Wake oscillators on all-call
    PCA9635.set_sleep(0x0);
    delayMicroseconds(500);
    // Enable PWM controls for all leds on all-call
    PCA9635.set_led_mode(3);
    /**
     * This will set the driver to open-drain mode, 0xff will set it to the default totem-pole mode
    PCA9635.set_driver_mode(0x0);
     */

    // Set device address and call I2c.begin() (note: your need to change the address to correspond to your device)
    driver.begin(96, false); // ******** You need to change the device address here, 0x70 is the 7-bit ALL-CALL address for PCA9635 ***********
    driver.begin(97, false); // ******** You need to change the device address here, 0x70 is the 7-bit ALL-CALL address for PCA9635 ***********

    Serial.println(F("Booted"));
}

void loop()
{
    // Scan the bus, output addresses (just 
    I2c.scan();
    delay(1000);
    
    // Blink leds in turn on all-call
    Serial.println(F("Blinking all drivers leds' in turn (via ALL-CALL)"));
    for (byte ledno = 0; ledno < 16; ledno++)
    {
        // Using half-brightness to make the led light up regardless of whether the drver is wired to source or sink current through it (when in totem-pole mode, open drain naturally is sink-only)
        PCA9635.set_led_pwm(ledno, 0x80);
        delay(250);
        PCA9635.set_led_pwm(ledno, 0x0);
    }
    delay(1000);
    Serial.println(F("Blinking addressed drivers leds' in turn"));
    // Blink leds in turn on the addressed instance
    for (byte ledno = 0; ledno < 16; ledno++)
    {
        driver.set_led_pwm(ledno, 0x80);
        delay(250);
        driver.set_led_pwm(ledno, 0x0);
    }

    // Dump device registers
    driver.dump_registers(0x0, 0x1b);

    delay(5000);
}

Any ideas on how to talk to a second PCA9635? It would seem that I need the ability to send an additional variable to the set_led_pwm function; namely an address variable.

Hi,

JacksonAudio:
The issue is that I can't find a way to address a second unit. I mean I CAN but if I define a second unit right below the initial definition as shown below (driver.begin(97, false):wink: the code for 96 stops working.

I have not used that device or the driver, but it seems that you'll need two instances of the driver like:

// Container for the devices
pca9635 driver1;
pca9635 driver2;

    driver1.begin(96, false); 
    driver2.begin(97, false);

...then duplicate the "// Blink leds in turn on the addressed instance" loop, one for driver1 and the other for driver2

Yours,
TonyWilk

Wrote a PCA9635 library a long while ago, it should support multiple instances.
The syntax of the lib is slightly different than Rambo's one

you can give it a try

The test sketch below compiles but I do not have the hardware nearby to test.
If it fails I am willing to help (please adjust addresses)

//
//    FILE: PCA9635_test_multiple.ino
//  AUTHOR: Rob Tillaart
//    DATE: 2018-02-18
// VERSION: 0.1.0
// PUPROSE: test PCA9635 library
//

#include "PCA9635.h"
#include <Wire.h>

PCA9635 ledArray(0x20);
PCA9635 ledArray2(0x21);

void setup()
{
    Serial.begin(115200);
    Serial.print("PCA9635 LIB version: ");
    Serial.println(PCA9635_LIB_VERSION);
    Serial.println();

    Serial.print(millis());
    Serial.print("\t");
    Serial.println("Test - write1 - I");
    for (int channel = 0; channel < 16; channel++)
    {
        for (int pwm = 0; pwm < 256; pwm++)
        {
            ledArray.write1(channel, pwm);
            ledArray2.write1(channel, pwm);
        }
    }
    Serial.print(millis());
    Serial.print("\t");
    Serial.println("Test - write 1 - II");
    for (int pwm = 0; pwm < 256; pwm++)
    {
        for (int channel = 0; channel < 16; channel++)
        {
            ledArray.write1(channel, pwm);
            ledArray2.write1(channel, pwm);
        }
    }
    Serial.println("done...");
}

void loop()
{}

TonyWilk:
Hi,
I have not used that device or the driver, but it seems that you'll need two instances of the driver like:

// Container for the devices

pca9635 driver1;
pca9635 driver2;

driver1.begin(96, false);
    driver2.begin(97, false);




...then duplicate the "*// Blink leds in turn on the addressed instance*" loop, one for *driver1* and the other for *driver2*

Yours,
TonyWilk

That worked beautifully Tony! Thank you so much! :slight_smile: