When connecting A4 and A5 to MPU-6050 Serial Monitor Stops Working

The Problem: The serial monitor stops printing data immediately when the MPU-6050 SDA and SCL pins are connected to the Arduino.

Here is the schematic.
3.3V --> VCC
GND --> GND
A5 --> SCL
A4 --> SDA
DIgital Pin 2 - INT

The code I am using is as follows:

// I2Cdev and MPU6050 must be installed as libraries
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

///////////////////////////////////   CONFIGURATION   /////////////////////////////
//Change this 3 variables if you want to fine tune the skecth to your needs.
int buffersize=1000;     //Amount of readings used to average, make it higher to get more precision but sketch will be slower  (default:1000)
int acel_deadzone=8;     //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge  (default:8)
int giro_deadzone=1;     //Giro error allowed, make it lower to get more precision, but sketch may not converge  (default:1)

// default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
//MPU6050 accelgyro;
MPU6050 accelgyro(0x68); // <-- use for AD0 high

int16_t ax, ay, az,gx, gy, gz;

int mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0;
int ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;

///////////////////////////////////   SETUP   ////////////////////////////////////
void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  Wire.begin();
  // COMMENT NEXT LINE IF YOU ARE USING ARDUINO DUE
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.

  // initialize serial communication
  Serial.begin(115200);

  // initialize device
  accelgyro.initialize();

  // wait for ready
  while (Serial.available() && Serial.read()); // empty buffer
  while (!Serial.available()){
    Serial.println(F("Send any character to start sketch.\n"));
    delay(1500);
  }
  while (Serial.available() && Serial.read()); // empty buffer again

  // start message
  Serial.println("\nMPU6050 Calibration Sketch");
  delay(2000);
  Serial.println("\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \nDon't touch it until you see a finish message.\n");
  delay(3000);
  // verify connection
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  delay(1000);
  // reset offsets
  accelgyro.setXAccelOffset(0);
  accelgyro.setYAccelOffset(0);
  accelgyro.setZAccelOffset(0);
  accelgyro.setXGyroOffset(0);
  accelgyro.setYGyroOffset(0);
  accelgyro.setZGyroOffset(0);
}

///////////////////////////////////   LOOP   ////////////////////////////////////
void loop() {
  if (state==0){
    Serial.println("\nReading sensors for first time...");
    meansensors();
    state++;
    delay(1000);
  }

  if (state==1) {
    Serial.println("\nCalculating offsets...");
    calibration();
    state++;
    delay(1000);
  }

  if (state==2) {
    meansensors();
    Serial.println("\nFINISHED!");
    Serial.print("\nSensor readings with offsets:\t");
    Serial.print(mean_ax);
    Serial.print("\t");
    Serial.print(mean_ay);
    Serial.print("\t");
    Serial.print(mean_az);
    Serial.print("\t");
    Serial.print(mean_gx);
    Serial.print("\t");
    Serial.print(mean_gy);
    Serial.print("\t");
    Serial.println(mean_gz);
    Serial.print("Your offsets:\t");
    Serial.print(ax_offset);
    Serial.print("\t");
    Serial.print(ay_offset);
    Serial.print("\t");
    Serial.print(az_offset);
    Serial.print("\t");
    Serial.print(gx_offset);
    Serial.print("\t");
    Serial.print(gy_offset);
    Serial.print("\t");
    Serial.println(gz_offset);
    Serial.println("\nData is printed as: acelX acelY acelZ giroX giroY giroZ");
    Serial.println("Check that your sensor readings are close to 0 0 16384 0 0 0");
    Serial.println("If calibration was succesful write down your offsets so you can set them in your projects using something similar to mpu.setXAccelOffset(youroffset)");
    while (1);
  }
}

///////////////////////////////////   FUNCTIONS   ////////////////////////////////////
void meansensors(){
  long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;

  while (i<(buffersize+101)){
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    if (i>100 && i<=(buffersize+100)){ //First 100 measures are discarded
      buff_ax=buff_ax+ax;
      buff_ay=buff_ay+ay;
      buff_az=buff_az+az;
      buff_gx=buff_gx+gx;
      buff_gy=buff_gy+gy;
      buff_gz=buff_gz+gz;
    }
    if (i==(buffersize+100)){
      mean_ax=buff_ax/buffersize;
      mean_ay=buff_ay/buffersize;
      mean_az=buff_az/buffersize;
      mean_gx=buff_gx/buffersize;
      mean_gy=buff_gy/buffersize;
      mean_gz=buff_gz/buffersize;
    }
    i++;
    delay(2); //Needed so we don't get repeated measures
  }
}

void calibration(){
  ax_offset=-mean_ax/8;
  ay_offset=-mean_ay/8;
  az_offset=(16384-mean_az)/8;

  gx_offset=-mean_gx/4;
  gy_offset=-mean_gy/4;
  gz_offset=-mean_gz/4;
  while (1){
    int ready=0;
    accelgyro.setXAccelOffset(ax_offset);
    accelgyro.setYAccelOffset(ay_offset);
    accelgyro.setZAccelOffset(az_offset);

    accelgyro.setXGyroOffset(gx_offset);
    accelgyro.setYGyroOffset(gy_offset);
    accelgyro.setZGyroOffset(gz_offset);

    meansensors();
    Serial.println("...");

    if (abs(mean_ax)<=acel_deadzone) ready++;
    else ax_offset=ax_offset-mean_ax/acel_deadzone;

    if (abs(mean_ay)<=acel_deadzone) ready++;
    else ay_offset=ay_offset-mean_ay/acel_deadzone;

    if (abs(16384-mean_az)<=acel_deadzone) ready++;
    else az_offset=az_offset+(16384-mean_az)/acel_deadzone;

    if (abs(mean_gx)<=giro_deadzone) ready++;
    else gx_offset=gx_offset-mean_gx/(giro_deadzone+1);

    if (abs(mean_gy)<=giro_deadzone) ready++;
    else gy_offset=gy_offset-mean_gy/(giro_deadzone+1);

    if (abs(mean_gz)<=giro_deadzone) ready++;
    else gz_offset=gz_offset-mean_gz/(giro_deadzone+1);

    if (ready==6) break;
  }
}

I am using Jeff Rowberg's Library for this.

I am using an Arduino UNO R3.
The MPU-6050 has a set of male header pins soldered to it, and those are connected to the Arduino via 10cm female-to-male jumper cables.

If anybody wants to see a picture (Fritzing or Real) let me know.

I checked the various other forum posts on this topic but they dont seem to adress this specific issue. Another noteworthy thing is that I am using an Arduino clone with the CH340 USB to Serial Chip - and the same was working earlier with an original Arduino which had the Atmega16U2 USB to Serial Chip. Does using I2C somehow prevent the usb-to-serial interface from working.

Then it's not guaranteed that I2C communication works with a 3.3V sensor. Or the sensor has benn damaged by the 5V from Uno?

I'd use a level shifter for proper I2C operation.

Are you using Adafruit's MPU6050?
If so, they recommend you power it through Vin with the same voltage as your CPU (5v for you).
Probably has something to do with the pullups, even tho the schematic shows the pullups connected to 3v.

Do you have some confirmation that only serial monitor stops working? Everything else keeps on running...?

The same sensor was working with another Official Arduino Board using the same wiring. So is a level shifter really necessary?

This is the link to the sensor I am using.

thanks for suggesting that. tested using the following code.
the led is supposed to exhibit the following behavior

  1. HIGH
  2. LOW
  3. Repeat 2 more times

And the serial monitor is supposed to print 1, 2, 3 and 4 in order, after various steps throughout the setup.

void setup() {
// initialize serial communication
  Serial.begin(115200);

   pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    Serial.println("1");
    delay(1000);
pinMode(13, HIGH);
  // join I2C bus (I2Cdev library doesn't do this automatically)
  Wire.begin();
  // COMMENT NEXT LINE IF YOU ARE USING ARDUINO DUE
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.
delay(1000);
  Serial.println("2");
  pinMode(13, LOW);
  // initialize device
  accelgyro.initialize();
  delay(1000);
  Serial.println("3");
  pinMode(13, HIGH);
  // wait for ready
  // start message
  Serial.println("\nMPU6050 Calibration Sketch");
  delay(2000);
  Serial.println("\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \nDon't touch it until you see a finish message.\n");
  delay(3000);
  // verify connection
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  delay(1000);
  // reset offsets
  accelgyro.setXAccelOffset(0);
  accelgyro.setYAccelOffset(0);
  accelgyro.setZAccelOffset(0);
  accelgyro.setXGyroOffset(0);
  accelgyro.setYGyroOffset(0);
  accelgyro.setZGyroOffset(0);
    delay(1000);
Serial.println("4");
    
    pinMode(13, LOW);
}

The serial monitor prints only up to 2. 3 and 4 are nowhere to be seen.
When I type in things to the serial monitor the TX LED flashes.
The led goes to HIGH, then LOW, then HIGH and gets stuck there.

SO it seems that the whole arduino is freezing. Not just the Serial Monitor.

The same behavior is exhibited by the LED when powered by an external power source. Not USB. Serial Port too, the RX LED seems to blink twice, corresponding to steps 1 and 2 likely.

What should I do. Any further advice?

EDIT: Will using pullup resistors help in any way whatsoever?

I would double check wiring, especially gnd line. Could also be powering issue, causing voltage drop on arduino. Then you could try I2C scanner sketch to see if it finds the device without freezing.

What other Arduino did it work with?
Check the voltages on the SDA and SCL pins when the device is connected.
The board datasheet states the voltage range is 3-5v. The board has a regulator on it.
I2C devices are all open collector output. They don't drive the pins HIGH, only pull the pins LOW.
Did you try using 5v on Vcc?
Maybe the device failed?

Do not use a magic numbers.
Why not use a library methods?

Wire.begin();           // Start I2C comms on SCL and SDA
Wire.setClock(400000);

Good point!

tried i2c scanner sketch.


goes till here but gets stuck after this.

tried the same i2c scanner sketch, with 5V on VCC but to no avail. same results as above.

It worked with an UNO, look below quoted from post #1

...
..
.

yes. i have another MPU6050 which i will try the same with.

i do not think that is the issue here. especially after even the I2C scanner did not work. Not even an error message showed up. and when i disconnected the mpu6050 the error message does show up.
image
still just to make sure that is not the problem which i was very sure it was not i tried what you suggested.


and it gets stuck at the same place.

How exactly do i do that, im a bit clueless so please guide me. Do i connect the probes of the multimeter from SDA to GND and then SCL to GND?

Voltmeter negative to GND, then positive lead to SDA and SCL.

Edit: My guess is SDA and SCL should show Vin voltage. Maybe 3.3v no matter Vin.

thanks. did just that. got around 1.7V from GND to SDA and 0.02 from GND to SCL

edit: does that mean there is something wrong with the MPU6050

Looks like fail.

Edit: It appears the board has pullups on SDA and SCL.
I'm not sure why that would make the Wire library freeze.

Now that you are with multimeter, check also you have solid connection between arduino and MPU, Measure also arduino 5V/GND voltage when it's crashing.

In addition to kmin's suggestion, check the voltages on SDA and SCL if they are not connected to the Uno, only Vin and GND connected.

Hi, @eyh

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you post some images of your project?
So we can see your component layout.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1.6 on sda and 0.01 on scl
to double check again with the pins connected to uno i got:
1.8 on sda and 0.01 on scl

connection between all pins appears to be good.

jut confirming before doing it, i have to connect 5V of Arduino to GND of Arduino right. So no MPU involved? And why 5V, for i have connected mpu6050 to 3.3V

Both SDA and SCL should show at least 3v on each pin. Looks like the 6050 failed.



Does this work?