Accelerometer code not executing

Hi there,

I am testing an accelerometer (MPU-6050) however since I am new to arduino I followed a step by step tutorial on youtube on how to code it.

Youtube video: Ep. 57 Arduino Accelerometer & Gyroscope Tutorial MPU-6050 6DOF Module - YouTube

However the code gets stuck in the "void recordAccelRegisters()" function. I have identified the exact line that doesnt execute by putting test serial.println around it:

Serial.println("D");
while(Wire.available() < 6);
Serial.print("E");

The E does not print in the serial monitor.

Does anyone know whats going wrong?

Kind regards,

Duncan

code:

#include <Wire.h>

long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;

long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  setupMPU();
}


void loop() {
  recordAccelRegisters();
  recordGyroRegisters();
  printData();
  delay(100);
}

void setupMPU(){
  Serial.println("A");        //Test code to see if line executes
  Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
  Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
  Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
  Wire.endTransmission();  
  Wire.beginTransmission(0b1101000); //I2C address of the MPU
  Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4) 
  Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s 
  Wire.endTransmission(); 
  Wire.beginTransmission(0b1101000); //I2C address of the MPU
  Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5) 
  Wire.write(0b00000000); //Setting the accel to +/- 2g
  Wire.endTransmission(); 
  Serial.println("B"); //Test code to see if line executes
}

void recordAccelRegisters() {
  Serial.println("C"); //Test code to see if line executes
  Wire.beginTransmission(0b1101000); //I2C address of the MPU
  Wire.write(0x3B); //Starting register for Accel Readings
  Wire.endTransmission();
  Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B - 40)
  Serial.println("D");            //test code
  while(Wire.available() < 6);      //problem line of code!!!!!!!!!!!!!!!!!!!!
  Serial.println("E");          //does not print to serial monitor
  accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
  accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
  accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
  processAccelData();
}

void processAccelData(){
  gForceX = accelX / 16384.0;
  gForceY = accelY / 16384.0; 
  gForceZ = accelZ / 16384.0;
}

void recordGyroRegisters() {
  Wire.beginTransmission(0b1101000); //I2C address of the MPU
  Wire.write(0x43); //Starting register for Gyro Readings
  Wire.endTransmission();
  Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48)
  while(Wire.available() < 6);
  gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
  gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
  gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
  processGyroData();
}

void processGyroData() {
  rotX = gyroX / 131.0;
  rotY = gyroY / 131.0; 
  rotZ = gyroZ / 131.0;
}

void printData() {
  Serial.print("Gyro (deg)");
  Serial.print(" X=");
  Serial.print(rotX);
  Serial.print(" Y=");
  Serial.print(rotY);
  Serial.print(" Z=");
  Serial.print(rotZ);
  Serial.print(" Accel (g)");
  Serial.print(" X=");
  Serial.print(gForceX);
  Serial.print(" Y=");
  Serial.print(gForceY);
  Serial.print(" Z=");
  Serial.println(gForceZ);
}

sketch_may29c.ino (3.03 KB)

The first thing you should do is run the I2C scanner sketch (Mr. Google can help you find it). Make sure that your device is connected correctly. You'll know that because the I2C scanner will tell you the device's address, or will also hang waiting for a response from the device. See an address, the device is connected correctly. No address, and no messages about how many devices are connected, and you have a hardware/wiring problem.

Hi thanks for your response

I got no address and no messages, just got stuck on "searching..." for infinity!

I think my wiring is correct:
Arduino MPU6050

VCC<<3.3V
GND<<GND
SCL<<A5
SDA<<A4

Does that mean my MPU6050 is broken? I have tried this on two different Arduino Uno R3's and updated my arduino IDE to the latest version, nothing seems to work.

Kind regards,

Duncan

Does that mean my MPU6050 is broken?

More likely, it means that your device is not connected correctly. Post a schematic showing how you think you connected it, and a photo - of reasonable size, properly lit, in focus, with short wires, of 4 different colors, showing how you actually connected it.

All the details about the kind of photo are because we too often see dark, fuzzy pictures where the device was connected using 4 wires, 3 meters long, all the same color, running in and out of the edge of the picture. In other words, useless pictures.

Always post ALL your code, using code tags ("</>" button).

VCC<<3.3V
...
Does that mean my MPU6050 is broken?

Post a link to the exact module you are using.

If it has a 5V regulator on it, most of those will not work if powered by 3.3V, because of the regulator "dropout voltage".

That code looks very similar to what I am using, I can't see anything obviously wrong with it.

As above check your wiring and run it at 5V instead - what colour is the LED on your MPU board.

Also note I bought a bag of four of these off Amazon and two of them never worked so don't rule out dodgy hardware.