TCA9548a Channel switch speed
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- antoniosgeme
- Posts: 5
- Joined: Mon Dec 05, 2022 12:36 pm
TCA9548a Channel switch speed
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!
- adafruit_support_bill
- Posts: 88086
- Joined: Sat Feb 07, 2009 10:11 am
Re: TCA9548a Channel switch speed
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++.
Python is not the optimal language to use when high-performance is required. You would likely see some improvement using C/C++.
- antoniosgeme
- Posts: 5
- Joined: Mon Dec 05, 2022 12:36 pm
Re: TCA9548a Channel switch speed
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
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
- adafruit_support_bill
- Posts: 88086
- Joined: Sat Feb 07, 2009 10:11 am
Re: TCA9548a Channel switch speed
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?
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?
- antoniosgeme
- Posts: 5
- Joined: Mon Dec 05, 2022 12:36 pm
Re: TCA9548a Channel switch speed
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?
If I want to use separate buses on the Pi, do I have to add pull up resistors?
- adafruit_support_bill
- Posts: 88086
- Joined: Sat Feb 07, 2009 10:11 am
Re: TCA9548a Channel switch speed
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.
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.
- antoniosgeme
- Posts: 5
- Joined: Mon Dec 05, 2022 12:36 pm
Re: TCA9548a Channel switch speed
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?
- adafruit_support_bill
- Posts: 88086
- Joined: Sat Feb 07, 2009 10:11 am
Re: TCA9548a Channel switch speed
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.
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.
- antoniosgeme
- Posts: 5
- Joined: Mon Dec 05, 2022 12:36 pm
Re: TCA9548a Channel switch speed
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?
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?
- adafruit_support_bill
- Posts: 88086
- Joined: Sat Feb 07, 2009 10:11 am
Re: TCA9548a Channel switch speed
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.
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.
Please be positive and constructive with your questions and comments.