I have a Raspberry Pi 4B connected to a 9 DOF Razor IMU M0 (equipped with a MPU9250 IMU and a SAMD21 microprocessor).
I am trying to echo bytes back to the RPi, however, I seem to be getting corrupted data and I am unable to figure out what might be the cause of the problem.
My setup:
Wiring:
| RPi | IMU |
|---|---|
| GPIO 2 | SDA |
| GPIO 3 | SCL |
| GND (pin 9) | GND |
| 3.3V | 3V3 |
Below, you can also find the attached picture of the wiring.
Code:
First, the sensor I2C-slave code:
#include <Wire.h> #define SLAVE_ADDRESS 0x68 uint8_t data_to_echo = 0; void setup() { Wire.begin(SLAVE_ADDRESS); Wire.onReceive(receiveData); Wire.onRequest(sendData); } void loop() { } void receiveData(int bytecount) { for (int i = 0; i < bytecount; i++) { data_to_echo = Wire.read(); } } void sendData() { Wire.write(data_to_echo); } And the RPi, I2C master code in Python:
#!/usr/bin/env python3 import time import smbus2 as smbus DEVICE_BUS = 1 ADDRESS =0x68 number = 1 def main(): bus = smbus.SMBus(DEVICE_BUS) data = 4 bus.write_byte(ADDRESS, data) print("I sent: ", data) number = bus.read_byte(ADDRESS) print("I received: ", number) if __name__=="__main_": main() Expected behavior:
I am sending one byte from the RPi to the sensor and echoing the value back. So the expected behavior is that after sending the value "4", the same value should be received.
Real behavior:
However, after running the RPi code, this is the output:
I sent: 4 I received: 3 What I have tried:
First thing that I tried was to check whether the IMU is getting the correct data. I was sending the values of data_to_echo to the serial port and the data was correct.
The same code with the same wiring worked with an Arduino Nano acting as a slave and therefore I assume that the wiring and jumper cables themselves shouldn't be the cause of the problem.
I even examined the communication using an oscilloscope and it seems like the IMU is really sending 0x03, instead of 0x04 (See the attached oscilloscope screen capture).
I also believe that the IMU board is faulty, as other IMU boards I tried exhibit the same behavior.
Any ideas or suggestions on what might be wrong and why I am receiving wrong data are welcome. Also, I would be grateful for any tips on what to check next because I am running out of ideas.

