Trying To Communicate From Slave To Master By I2C, Read Triggers ...

I have a Pi Zero(master) connected to an Arduino Nano by i2c. The master to slave communication works fine(in this case I'm telling which servos to move to what angle).

My issue is I can't seem to get data back from the Arduino to the Pi. I was trying to have a Python while loop that read the same wire pin but it seems to trigger the receiveEvent while running... so I'm not sure if that makes sense or what. Is what I'm trying to do possible(I hope so)?

I'm pretty much just trying to echo back the servo commands upstream so the master knows the command is done running before sending more. I will also try and want to call stuff manually like echoing back sensor values that are read from the Arduino.

The other problem I have is this code commands servos and while the python loop that's reading the i2c pin is running the servos twitch. I read that as a thing that happens, not sure if not preventable. I briefly attempted to use Servo2.h for example but library not available(could install it).

To clarify: I have two terminals open, one runs the loop that's listening/reading the bus and then another one runs a command that writes to the Arduino from Python(this part works fine, servos move). Every time I run this script I would expect the read loop to show a msg.

Hardware info

SDA I2C GPIO2 -> A4 SCL I2C GPIO3 -> A5

Python Code

Write script snippet (this part works fine)

addr = 0x8 bus = smbus(1) bus.write_i2c_block_data(addr, 0x00, ..stringToBytes);

Read script - this runs on a loop, triggers the receiveEvent method in Arduino when it shouldn't, should trigger requestEvent I believe

while...  data=bus.read_i2c_block_data(addr,0x00,16)  // this outputs but it's just [0, ... 255] 16 parts, trying to convert to string I use a char32 buffer on Nano  print(str(data))

Nano code

#include <Wire.h> String msgBack = ""; void setup() {  Serial.begin(9600);  Wire.begin(0x8);  Wire.onReceive(receiveEvent);  Wire.onRequest(requestEvents); } void requestEvents() {  Serial.println("received request");  Serial.println(msgBack);  // tried to write here  //  char buffer[32];  //  msgBack.toCharArray(buffer, 32);  //  Wire.write(buffer); } void receiveEvent(int bytes) {  while (Wire.available()) {    // use data  }  // call back to Pi  // https://forum.arduino.cc/index.php?topic=6068.0  msgBack = "custom str";  // also tried to write here  //  char buffer[32];  //  msgBack.toCharArray(buffer, 32);  //  Wire.beginTransmission( - considered doing this  //  Wire.write(buffer); } void loop() {  delay(100); }

Tag » Arduino I2c Master Read Slave Write