TCA9548a Channel switch speed

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
antoniosgeme
 
Posts: 5
Joined: Mon Dec 05, 2022 12:36 pm

TCA9548a Channel switch speed

Post by antoniosgeme »

I am working on a raspberri pi project with 6 TCA9548a multiplexers and 48 MS5837 sensors which have identical addresses and thus the need for the multiplexers. I use smbus.write_byte many times to open and close the channels of the multiplexers to communicate with each sensor individually. The project requires a fast sample rate from the sensors but the repeated use if smbus.write_byte is not fast enough at the moment. When I increase the bus baudrate to 400Kb/s the performance improves. Can someone shed light on what the limitations of the system are and is there a way to improve the sample rate? What factors can affect it? Perhaps all the pull up resistors I have for the sensors? I also have really long lines to each sensor (6 FT). Does this play a role! Thanks!

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: TCA9548a Channel switch speed

Post by adafruit_support_bill »

Cable length and pullup resistors can influence max communication speed. All your devices are rated for up to 400KHz. So if they are communicating reliably at that speed, no changes should be required.

Python is not the optimal language to use when high-performance is required. You would likely see some improvement using C/C++.

User avatar
antoniosgeme
 
Posts: 5
Joined: Mon Dec 05, 2022 12:36 pm

Re: TCA9548a Channel switch speed

Post by antoniosgeme »

Would switching to from python to C/C++ improve the smbus communication? (which I think is written in C) I have checked that my python code is not the bottleneck here but the write_byte command.

Also would you recommend I try to increase the baudrate to a higher value? I have seen posts were users increased it to 1 MHz

https://raspberrypi.stackexchange.com/q ... imum-speed

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: TCA9548a Channel switch speed

Post by adafruit_support_bill »

The low-level bus communication code is probably in C. But the high-level call overhead can add up.

Both the TCA9548a and MS5837 chips are rated for up to 400KHz. You might get them to work at a faster speed. But that is not guaranteed. Faster bus speeds are also more difficult to achieve over longer wiring runs.

What is the application and what sample rates are you attempting to achieve?

User avatar
antoniosgeme
 
Posts: 5
Joined: Mon Dec 05, 2022 12:36 pm

Re: TCA9548a Channel switch speed

Post by antoniosgeme »

This is for an experimental apparatus which consists of a aircraft wing with the 48 pressure sensors embedded inside. The experiments are done underwater and there are 6 FT long wires connected to each sensor (4 per sensor). I have a 100nF 0805 capacitor soldered on the back of each sensor as a buffer between ground and power. Each sensor has 10 KOhm pull up resistors. I use the multiplexers to cycle through all the sensors.

If I want to use separate buses on the Pi, do I have to add pull up resistors?

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: TCA9548a Channel switch speed

Post by adafruit_support_bill »

The multiplexer boards and the sensor boards all have pullup resistors on them. You probably don't need more of them.

You didn't specify what sample rate you are trying to achieve. But you are really pushing the physical limits of i2c. You are probably bumping up against the bandwidth limits as well.

User avatar
antoniosgeme
 
Posts: 5
Joined: Mon Dec 05, 2022 12:36 pm

Re: TCA9548a Channel switch speed

Post by antoniosgeme »

I would be happy with 200 Hz sampling from the whole system. How about distributing the sensors across different I2C buses on the Pi and then using threading or multiprocessing? Could that work? I have timed the write_byte command and it takes about 70 microseconds. Is there a way to check if I am reaching I/O timing limits? For example if the baud rate is 400 kb/s, can I calculate if that 70 microseconds is close to the theoretical limit given the TCA communication protocol?

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: TCA9548a Channel switch speed

Post by adafruit_support_bill »

It takes 2 bytes on the wire to address the device and set the channel selections on the TCA9548a. There is some additional overhead for synching and acks. Say roughly a total of about 20 clock cycles on the wire. At 400KHz, 20 cycles would be 50 microseconds. So it sounds like you are approaching that.

If the processor has hardware support for multiple i2c buses, you could see some speedup. Although multithreading adds some scheduling latency and context switching overhead.

Another approach is to distribute the first-level data acquisition to a few smaller microcontrollers and have them stream the data back via a faster channel such as SPI or even USB.

User avatar
antoniosgeme
 
Posts: 5
Joined: Mon Dec 05, 2022 12:36 pm

Re: TCA9548a Channel switch speed

Post by antoniosgeme »

I did not realize I was this close to the I/O limit. That calculation is very instructive. This explains why increasing the baudrate translates to acquisition speed.

Regarding your alternative solution, do you mean connecting a microcontroller to each sensor or to each multiplexer? If so what microcontroller would you recommend? Then the issue becomes how to connect all those microcontrollers to the Pi? Can SPI handle multiple devices like I2C can?

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: TCA9548a Channel switch speed

Post by adafruit_support_bill »

A processor per multiplexer should give you a significant increase in maximum poll-rate per sensor. But you still need to get that data back to the Pi. And you probably want that to be reasonably well correlated timewise across the total array.

SPI is also a bus, but it is capable of much higher data rates compared with i2c. https://www.totalphase.com/blog/2021/07 ... ilarities/

Instead of addressing, SPI uses a dedicated "Chip Select" (CS) pin per device - in addition to the shared MOSI, MISO and SCK signal pins. If you have 6 pins to spare on your Pi, you could handle a microcontroller per multiplexer and poll each of them for consolidated readings from all the attached devices.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”