Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
94 views

Arduino - TCA9548a (I2C Multiplexer) C++ Integration - Stack Overflow

This document discusses using a TCA9548a I2C multiplexer with four VL680 laser distance sensors connected to a Raspberry Pi. The author is able to select channels on the multiplexer but is unable to successfully read from or write to the sensors when going through the multiplexer. Code is provided to initialize communication with an I2C device and select channels on the multiplexer, but the author is seeking help with how to properly address the sensors after channel selection to read and write their registers.

Uploaded by

zzzzzz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Arduino - TCA9548a (I2C Multiplexer) C++ Integration - Stack Overflow

This document discusses using a TCA9548a I2C multiplexer with four VL680 laser distance sensors connected to a Raspberry Pi. The author is able to select channels on the multiplexer but is unable to successfully read from or write to the sensors when going through the multiplexer. Code is provided to initialize communication with an I2C device and select channels on the multiplexer, but the author is seeking help with how to properly address the sensors after channel selection to read and write their registers.

Uploaded by

zzzzzz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

TCA9548a (I2C multiplexer) C++ integration

Asked 3 years, 8 months ago Modified 3 years, 8 months ago Viewed 844 times

I have 4 VL680 laser distance sensors to use with à Raspberry. It is a I2C bus compatible sensor but we can't change the address. So, I use the TCA9548a
Adafruit board I2C multiplexer. I can run only one VL680 directly on my I2C bus but it won't work trough the multiplexer. Datasheet is not clear and I
0 find only code for Arduino or bad Python example.

Here I am :

char filename[20];
const int adapter_nr = 1;
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
file = open(filename, O_RDWR);
if (file < 0)
{
printf("Unable to connect reach I2C bus \n");
exit(EXIT_FAILURE);
}

// multiplex address
const int addr = 0x70;

if(ioctl(file, I2C_SLAVE, addr) < 0)


{
printf("Fail to reach multiplex laser \n");
exit(EXIT_FAILURE);
}

char buf[10];
buf[0] = 0x01; // to select channel 0
write(file, buf, 1);
I2C_init();

doTheSameAsOneOnlyVL680();
...

I don't know how I must do and nothing help. Is that the way to select the channel ? How write and read then ?

void I2C_init()
{
const int addr = 0x29;

if(ioctl(file, I2C_SLAVE, addr) < 0)


{
printf("Fail to reach laser \n");
exit(EXIT_FAILURE);
}

if(read_byte(file, 0x000) != 0xB4)


{
printf("Problem with VL6180X\n");
//exit(EXIT_FAILURE);
}

int setup = read_byte(file, 0x016);

if(setup == 1)
{
printf("Init all registers \n");
write_byte(file, 0x0207, 0x01);
...

}
else
Join Stack Overflow to find
{ the best answer to your technical question, help
Sign up with email Sign up with Google Sign up with GitHub Sign up with Facebook
others answer theirs. printf("Fail \n");
}

set_scaling(file, 1);
}

c++ arduino raspberry-pi i2c sensors

Share Follow edited Apr 18, 2019 at 13:40 asked Apr 17, 2019 at 20:48
NicolasColsoul
31 1 5

Totally unrelated to your problem, but the size you pass to the snprintf function is including the terminator. Therefore you can pass e.g. sizeof filename instead of
using a magic number. Also, if you program in C++, why don't you use std::string ? – Some programmer dude Apr 17, 2019 at 21:02

1 Answer Sorted by: Highest score (default)

When ioctl(file, I2C_SLAVE, addr) runs you're saying "On I2C talk to the multiplexer."

1 Then, with

buf[0] = 0x01; // to select channel 0


write(file, buf, 10);

you make the multiplexer connect the VL680 that's connected to multiplexer first port to the I2C bus.

After you talked to the multiplexer with

write(file, buf, 10);

and before you talk to the distance sensor with

doTheSameAsOneOnlyVL680();

you need to run some code in order to say "On I2C talk to the distance sensor."

Something like:

if(ioctl(file, I2C_SLAVE, vl680Addr) < 0)


{
printf("Fail to reach distance sensor \n");
exit(EXIT_FAILURE);
}

, where vl680Addr is the address (you'll need to define that) for the VL680 laser distance sensor,

Share Follow answered Apr 18, 2019 at 2:40


Alex Baban
11k 4 28 40

Thanks for your answer. I thing we are in the right direction. I am able to connect to the sensors because ioctl is showing no error. But I can't read/write to sensor regs.
Here is the initialize code of each sensor (first step of doTheSameAsOneOnlyVL680). I have edited my question. –  NicolasColsoul Apr 18, 2019 at 13:37

I change in my I2C_init() function file to file2 for the test and there it says unable to reach laser... I am bad with my channel select. –  NicolasColsoul Apr 18, 2019 at 13:59
Join Stack Overflow to I'm
findnot
theclear...,
best without
answerthe
to multiplexer,
your technical question, help
are you able to write/read to/from
Signone
up sensor? If not, I think you'll need to get that working before connecting more sensors.
with email
others answer theirs.
– Alex Baban Apr 18, 2019 at 18:04

Yes. I can easily. –  NicolasColsoul Apr 19, 2019 at 14:05

Maybe try I2C_SLAVE_FORCE instead of I2C_SLAVE – Alex Baban Apr 19, 2019 at 16:48

Join Stack Overflow to find the best answer to your technical question, help
Sign up with email
others answer theirs.

You might also like