Two VCNL4010 with SoftI2cMaster without I2C multiplexer

Hello,

I have two VCNL4010 sensors. Both have same slave address and I want use them both with one Arduino but without I2C Multiplexer. I found interesting code (originaly created for VCNL4000) but I not get valid results - getting all-time static value (65278). Any idea how to make it working? Thank you!

PS: All neccessary files are attached.

#include <SoftI2cMaster.h>

word ch1 = 0x00;
word ch2 = 0x00;
uint8_t data1 = 0x00;
uint8_t data2 = 0x00;

SoftI2cMaster i2c;

#define SCL_PIN1 8     
#define SCL_PIN2 10
#define SDA_PIN1 9
#define SDA_PIN2 11

void i2c_transaction(uint8_t n, uint8_t reg, uint8_t dat){
    switch (n){
        case 1: 
        
        TwoWireBase *bus1;
        bus1 = &i2c;
        i2c.init(SCL_PIN1, SDA_PIN1);
        
        bus1->start(0x26);
        bus1->write(reg);
        bus1->write(dat);
        bus1->stop();
        
        bus1->start(0x26);
        bus1->write(0x87);
        bus1->restart(0x27);
        data1 = bus1->read(0);
        data2 = bus1->read(1);
        bus1->stop();

        ch1 = word(data1,data2);
        break;
        
        case 2:
        
        TwoWireBase *bus2;
        bus2 = &i2c;
        i2c.init(SCL_PIN2, SDA_PIN2);
        
        bus2->start(0x26);
        bus2->write(reg);
        bus2->write(dat);
        bus2->stop();
        
        bus2->start(0x26);
        bus2->write(0x87);
        bus2->restart(0x27);
       
        data1 = bus2->read(0);
        data2 = bus2->read(1);
        bus2->stop();
        
        ch2 = word(data1,data2);
        break;
    }
}

void setup(void)
{
    Serial.begin(9600);
    
    i2c_transaction(1,0x83,20);
    i2c_transaction(1,0x89,3);
    
    i2c_transaction(2,0x83,20);
    i2c_transaction(2,0x89,3);
}

void loop(void)
{
    i2c_transaction(1,0x80,8);
    i2c_transaction(2,0x80,8);
    
    Serial.print(" (1). ");
    Serial.print(ch1);
    Serial.print(" (2). ");
    Serial.println(ch2);
}

TwoWireBase.h (1.69 KB)

SoftI2cMaster.h (1.6 KB)

SoftI2cMaster.cpp (3.72 KB)

I found a problem here:

bus1->start(0x26);
bus1->write(0x87);
bus1->restart(0x27);
data1 = bus1->read(0);
data2 = bus1->read(1);
bus1->stop();

Changing red line with this make project working:

bus1->stop();
bus1->start(0x27);

Topic can be closed now. :slight_smile: