2 years ago

#16670

test-img

Kamil Widzyk

Weird bug in i2c between Arduino and Raspberry Pi 4B

I have a weird bug when I'm sending data from Arduino Mega via I2C to Raspberry Pi 4B.

Arduino Mega is working as slave and Raspberry Pi as master. Raspberry Pi request data from Arduino and it sends a string of letters and numbers that look like this(no newline on the end):

xxE5xNx

but Raspberry Pi receives:

xxE5xN@

For sure that's not a issue with wiring, because I tested sending and receiving of 1000 packets without any error.

This bug does not happen when I change 'E', '5' or 'N' to another character or delete first two characters. When changing any of 'x' to another character bug still exists. Why?

Minimal code for reproducing:

Arduino Mega(slave):

#include <Wire.h>

String s = "xxE5xNx";
void i2cRequest(){
  Wire.print(s);
}

void setup() {
  Wire.begin(0x01);
  Wire.onRequest(i2cRequest);
}

void loop() {}

Raspberry Pi 4B(master):

from smbus2 import SMBus

bus = SMBus(1)

data = bus.read_i2c_block_data(0x01, 0, 32)
text = ""
for m in data:
    if(m != 255): text += chr(m)

print(text)

string

arduino

raspberry-pi

communication

i2c

0 Answers

Your Answer

Accepted video resources