I2c Freezes DUE Upon Initiation

Hey,

First off, I know that freezing on I2C is a common problem, and I have already spent 3 days reading the existing threads on this issue and trying the suggested solutions but to no avail. Plus, my case is slightly different as the freezing occurs immediately after the beginning of the communication. So I decided to start a new topic.

I’m trying to use Arduino to emulate an MPU-6050 which is sending data over I2C to a master MCU on an existing board. I have desoldered the MPU and used wires to connect SDA, SCL and GND lines to Arduino, and then set up the Arduino as a slave with the same address as the MPU (0x68). The voltage level of the existing board is 3.3 volts.

At first I used an Arduino Mega. I was able to read the data being sent by the master MCU but it didn’t make sense, the received numbers were not in the range 13‍‍‍‍~117 (the range of register addresses of the MPU-6050).

Then I tried with Arduino Due which is a 3.3v board. Now the data makes sense but Arduino freezes less than a second after powering up the board and initiation of the i2c communication. My code is very simple and the very same code was running on Mega without a problem, so I’m almost sure the code is not the culprit.

I have tried:

  1. Adding external 4k7 pull-ups, which delayed the freezing by a few milliseconds.
  2. Decreasing the pull-up resistors to 1k5, slightly improved the situation but still freezes in less than a second.
  3. Using the second i2c terminal (SCL1 and SDA1 pins), same result.
  4. Using shorter wires (no more than 10cm long), didn’t help.

My Code:

#include <Wire.h>


unsigned long currentTime, prevTime = millis();

void setup() {
  Wire.begin(0x68);                  // join i2c bus with address #0x68
  Wire.onReceive(receiveEvent); // register event
  Wire.onRequest(requestEvent);
  Serial.begin(115200);             // start serial for output
  Serial.println("Hi!");    
}   

void loop() {‍                          // Just to make sure the code is running
  currentTime = millis();
  if( currentTime - prevTime > 500)
  {
    Serial.print(".");
    prevTime = currentTime;
  }
}


void receiveEvent(int howMany) {
  Serial.print("Receive Event - ");
  Serial.println(howMany);
  while (0 < Wire.available()) { // loop through all but the last
    Serial.println(Wire.read());         // print the character
  }
}

void requestEvent(){
    Serial.println("Request Event");
}

And this is the output I’m getting in serial monitor:

Hi!

Receive Event - 2
107
0
Request Event
Receive Event - 2
59
59
Request Event
Request Event
Receive Event - 1
59
Request Event
Receive Event - 1
59
Request Event
Recei

Any help will be much appreciated!

What about the master I2C code? What device is that? You should post that code also.

I use a Due as an I2C slave with my RPi as the master with no problems. It takes a small delay between sending data and requesting data (10us). The more serial stuff you have in the slave, the more time you'll have to give it.

SurferTim: What about the master I2C code? What device is that? You should post that code also.

The master is a pre-programmed MCU on a PCB. The board is taken from a toy self-balancing robot, I'm trying to hack it for another project. So I don't have the code for the master, all I know is that it is reading data from MPU-6050.

Then reduce the serial output on the slave to a minimum.

SurferTim: Then reduce the serial output on the slave to a minimum.

Thanks, I will try reducing the serial outputs and report the result soon.

But I wonder can the serial outputs be causing the freeze? Because I had used the same serial outputs on Mega and the sketch was running without any problems. I think the noisy I2C signal is causing the freeze!

Ifr you think it is noise, then you should analyze the signals with an oscilloscope.

SurferTim: Then reduce the serial output on the slave to a minimum.

Okay so I tried this suggestion today and reduced my serial outputs. To my surprise, it fixed the freezing problem! Thanks a lot, SurferTim!

I still don't really understand how could slightly longer serial outputs cause the freezing! ::)

SurferTim: Ifr you think it is noise, then you should analyze the signals with an oscilloscope.

I actually did this, SCL & SDA looked noisy. Although I'm not sure if it was noise, because there weren't any spikes with voltages higher than 3.3v, but the frequency of the SCL signal was more than 4 MHz, maximum i2c frequency of MPU-6050.

I may be wrong, but both the serial and I2C involve interrupts, and it appears they don't work well together.