How do I send multiple sensors data from an arduino Mega Slave to a Raspberry pi 4 via I2C

The arduino Mega runs the main program, I want to send sensor data depending on what specific sensor data I want to stream on my raspberry pi 4 Master. The way I see it : Raspberry pi requests specific data from the Arduino Mega, Arduino starts sending that data in a loop until raspberry pi sends a new request (equivalent to me clicking on a button in my GUI on the raspberry pi) is there a workaround ?

The Arduino can wait for a command from the RasPi and then transfers the selected data. A slave always has to wait for a read request before it can send the specified number of bytes.

1 Like

And what if there are multiple sources of data that's actually being collected within a loop (multiple sensors), should I create multiple Send_Data() functions where in each one there's the code snippet for the corresponding sensor? In this case, how should I proceed with the Wire.onRequest() function? The documentation says that the function that goes in onRequest() shouldn't return any data and shouldn't take any parameters.

The I2C bus between a 3.3V device and a 5V device is already a problem.
The I2C bus between processors has many troubles. The I2C bus is not meant for that.

Since the Arduino Mega 2560 has three spare Serial ports, can you use Serial communication ? You need a level shifter between the Arduino Mega 2560 and Raspberry Pi.

The I2C bus on the Arduino Mega 2560 can be used for sensors of course (you might need a I2C level shifter there as well if those are 3.3V sensors).

You can have only one onRequest() function. The master tells the slave which sensor data it should send prior to requesting the data. A specific command code can be sent to indicate the data record to transmit on the following request.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.