Arduino, INA219 and Pi communicating over i2c

Hi,

This is my first post so don't be too hard :slight_smile:

I have an arduino that controls a Bi Directional DC-DC Buck converter by means of PID control. To implement PID, I get current and voltage readings from INA 219 through i2c at a very fast rate. All of that works fine. Now I am trying to interface a Pi on the same i2c bus. The Pi is going to give new setpoints to the PID controller on arduino via i2c. At the same time it will read voltage and current values from arduino as well. I have tried to implement this but I am frequently getting IO ERROR on pi. Plus often times, the INA219 sensor readings stop updating as well. I have a hunch that why this might be happening:

Since pi is polling arduino at random instances, there might be data collision occurring on the i2c lines. Is that possible in the setup I have described above?

If yes, can anybody provide a workaround this?

Turning on soft and fluffy mode :grin:

With two Masters on a I2C bus, there will always be problems sooner or later.
You can read my opinion in the paragraph: "Don't try a Multi Master I2C bus".

If the Arduino requests data from the INA219 at a very fast rate, the collisions occur at a fast rate as well.

The Raspberry Pi has 3.3V signals. The basic Arduino boards are 5V boards. That is a mismatch.

You could try a serial communication between the Raspberry Pi and the Arduino.
Some Arduino boards have a spare serial port and if you select a 3.3V Arduino board, the voltage level mismatch is solved without the need of a level shifter.

When your Arduino board is a Arduino Uno, you might also connect it to the Raspberry Pi via the USB and use the serial port over USB to communicate with the Arduino Uno.

Thanks for the reply. The issue is I have 3 Arduinos each with an INA219 sensor interfaced with a Pi. What communication protocol would you recommend? Any wireless options available?

Use another bus. You can choose SPI if all three Arduinos are within about half a meter. If you need longer cables I recommend RS-485 or any of the wireless solutions. But to get a recommendation for a wireless solution you need to provide much more information about your environment. What distances between the board, what kind of obstacles between them, what reliability do you expect, etc.

For now this is a prototype, so distances and obstacles aren't an issue. I am working in a lab environment so I can essentially assume that all arduinos and pi are close to each other (its like a proof of concept project). For wireless, I feel like wifi will be an over kill. Can I have communication with multiple arduinos using Bluetooth?

From what I have seen the Raspberry Pi does not seem to do I2C repeated starts, and without that I2C atomic transactions and Multi-Master are probably a wasted effort. The type of communication SMBus does is fine, but that means keep the interface the Pi is on simple with one master and SMBus compliant devices (unfortunately lots of sensors seem to require atomic transactions to read them).

Another option is the ATmega328pb which has two I2C ports, one could be a simple smbus (read as dumb I2C) interface, and the other a more full-featured i2c master that can do atomic transactions.

I had better let you know that the Raspberry Pi compiler/toolchain for AVR does not have the needed updates for building ATmega328pb programs.