i2C communication problem with multiple devices at the same time
1384 Views - Created 15/07/2020
We are using the ExpanderPI in combination with BME680 sensor for reading temperature and humidity. They both communicate through i2C and there are problems when we try to read data from both of them simultaneously. In case reading happens at the same time, error occurs: EIO, Input/output error.
Short summary of our system:
RaspberryPI is running software similar to "stopwatch". We have 3 physical buttons connected to ExpanderPI from where we control the stopwatch. To detect the button changes (since there are no interrupt callbacks available), we are using a interval of 200ms to check for button presses all the time, which works nicely. The problem is when we try to read from BME sensor at some interval (10 seconds e.g), that's when the error above shows. If we comment / remove ExpanderPI code from the software and only leave BME running, it works normally.
After some investigating we noticed both libraries are using different i2c libraries in the back - could this maybe be the cause? Also couldn't notice anywhere in the libraries that the connections are getting closed at any point in time. Does this somehow "reserve" the i2C communication for let's say ExpanderPI library only, which then makes it impossible for BME's library to use it (that's why the error)?
Thanks, any help would be appreciated!
The problem could be that you are using two different libraries for accessing the I2C bus.
Are you using a single process thread to access the Expander Pi and the BME680 or are they running in independent threads? The Expander Pi library should close its connection automatically after each data read or write but if each device has its own thread then there could be a conflict where both threads try to access the I2C bus at the same time. When accessing the I2C bus it is best to keep everything in a single thread with one I2C library to reduce the chance of I/O errors.
It is possible to make an interrupt callback with the Expander Pi by using the interrupts built into the MCP23017 IC to monitor the status of the input pins and connect the output from the INTA or INTB pins to a Raspberry Pi GPIO pin via a voltage divider. You can then add an event to the GPIO pin to trigger a callback routine when a button is pressed.
We have a tutorial for our IO Pi board that explains how to set this up in hardware and software. The Expander Pi uses the same IO chip as the IO Pi so you can use the code from the tutorial with the Expander Pi.
We decided to translate existing library and move it inside ExpanderPI library. Now everything is working as expected.
Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view, download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.