I hade my MPU6050 arrived today however It will not function

Today my MPU6050 finally arrived, however it isn't functioning as expected. it has been plugged in correctly to my Arduino Nano, it seems to be getting power as the LED is green, however the program isn't functioning, it isn't printing any data and I don't believe it's even receiving any. I think my code to be correct but it may be flawed in some way, I'm not confident in my capabilities as of the moment. Something I noticed with it is that when I remove the MPU6050 the code still runs without any present error even though it is programmed to print an error message if there is a problem with the IMU, leading me to believe that it isn't connecting to it. Note that I have not yet soldered the IMU to the pins and it is simply sitting on them as of the moment, could that effect it?

code:

#include <MPU6050.h>
#include <Wire.h> 
#include <SPI.h> 

MPU6050 mpu;

#define USE_MPU6050_I2c
   
#define GYRO_250DPS

#define ACCEL_2G



#if defined USE_MPU6050_I2C
  #define GYRO_FS_SEL_250    MPU6050_GYRO_FS_250
  #define GYRO_FS_SEL_500    MPU6050_GYRO_FS_500
  #define GYRO_FS_SEL_1000   MPU6050_GYRO_FS_1000
  #define GYRO_FS_SEL_2000   MPU6050_GYRO_FS_2000
  #define ACCEL_FS_SEL_2     MPU6050_ACCEL_FS_2
  #define ACCEL_FS_SEL_4     MPU6050_ACCEL_FS_4
  #define ACCEL_FS_SEL_8     MPU6050_ACCEL_FS_8
  #define ACCEL_FS_SEL_16    MPU6050_ACCEL_FS_16
#endif



//IMU 
float AccX, AccY, AccZ; 
float AccX_prev, AccY_prev, AccZ_prev; 
float GyroX, GyroY, GyroZ;
float GyroX_prev, GyroY_prev, GyroZ_prev; 
float AccErrorX, AccErroY, AccErrorZ, GyroErrorX, GyroErrorY, GyroErrorZ;
int loopRate;
int ACCEL_SCALE_FACTOR = 16384; 
int GYRO_SCALE_FACTOR = 131;


float B_accel = 0.14; //Accelerometer filter parameter
float B_gyro = 0.1; //Gyroscope filter parameter


void setup() {
  // put your setup code here, to run once:
  Serial.begin(500000); 

  delay(3000); 

  IMUinit();

}

void IMUinit() {

   #if defined USE_MPU6050_I2C
    Wire.begin();
    Wire.setClock(1000000); 

    mpu6050.initialize(); 

    if (mpu6050.testconnection() == false) {
      Serial.println("MPU6050 initialization unsuccessful");
      Serial.print("oh man"); 
      while(1) {}

      #endif
  
  }

void loop() {
  // put your main code here, to run repeatedly:

   Serial.print("AccX: ");
  Serial.print(AccX);
  Serial.print(" AccY: ");
  Serial.print(AccY);
  Serial.print(" AccZ: ");
  Serial.println(AccZ);

  Serial.print("GyroX: ");
  Serial.print(GyroX);
  Serial.print("GyroY: ");
  Serial.print(GyroY);
  Serial.print("GyroZ: ");
  Serial.print(GyroZ);
  

  get_IMU_data();

  loopRate = 2000;  
  

}

void get_IMU_data() {
   int16_t AcX,AcY,AcZ,GyX,GyY,GyZ; 

   #if defined USE_MPU6050_I2C
    mpu6050.getMotion6(&AcX, &AcY, &AcZ, &GyX, &GyY, &GyZ);
  #endif

  AccX = AcX / ACCEL_SCALE_FACTOR; // in G's
  AccY = AcY / ACCEL_SCALE_FACTOR;
  AccZ = AcZ / ACCEL_SCALE_FACTOR;

  //Low-pass filter applied to accelerometer data
  AccX = (1.0 - B_accel) * AccX_prev + B_accel * AccX;
  AccY = (1.0 - B_accel) * AccY_prev + B_accel * AccY;
  AccZ = (1.0 - B_accel) * AccZ_prev + B_accel * AccZ;
  AccX_prev = AccX;
  AccY_prev = AccY;
  AccZ_prev = AccZ;

  //Gyro values

  GyroX = GyX / GYRO_SCALE_FACTOR; // converts to degrees per second
  GyroY = GyY / GYRO_SCALE_FACTOR;
  GyroZ = GyZ / GYRO_SCALE_FACTOR;

  //Low-Pass filter for Gyro data
  GyroX = (1.0 - B_gyro) * GyroX_prev + B_gyro * GyroX;
  GyroY = (1.0 - B_gyro) * GyroY_prev + B_gyro * GyroY;
  GyroZ = (1.0 - B_gyro) * GyroZ_prev + B_gyro * GyroZ;
  GyroX_prev = GyroX;
  GyroY_prev = GyroY;
  GyroZ_prev = GyroZ;

  
}

images:

Yes

How is the MPU6050 connected to the Arduino (which pins) ?

Where did you get the code ?

That's guaranteed to give problems. Fix this first, then look for other problems.

:Troubleshooting Hints:
1. Are you using this 5V Supply 5V logic breakout board (Fig-1) with pins soldered? If pins are not soldered, solder them and then go to Step-2.

MPU6050withPin
Figure-1:

2. Connect AD0-pin to GND to assign I2C address at 0x68 and then upload the following sketch to see the presence of the sensor in the I2C Bus.

#include<Wire.h>

void setup()
{
     Serial.begin(9600);
     Wire.begin();
     //---------------------------------
    Wire.beginTransmission(0x68);
    byte busStatus = Wire.endTransmission();
    if(busStatus !=0 )
    {
        Serial.print("Sensor is not found on I2C Bus!");
        while(1);  //wait for ever
    }
    Serial.println("Sensor is found on I2C Bus.");
}

void loop()
{

}

3. If Step-2 works, then upload the following sketch to see that the built-in temperture sensor of MPU6050 is responding.

#include<Wire.h>
#define MPUaddr 0x68

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

void loop()
{
  Wire.beginTransmission(MPUaddr);                     //Start communication with the MPU-6050.
  Wire.write(0x6B);                                   //We want to write to the PWR_MGMT_1 register (6B hex).
  Wire.write(0x00);   //enable temp sensor             //Set the register bits as 00000000 to activate the gyro.
  Wire.endTransmission();      

  //----pointing temp sensor-----------------
  Wire.beginTransmission(MPUaddr);                        //Start communication with the MPU-6050.
  Wire.write(0x41);      //pointing Temp_Out_High Reg                                           //Set the register bits as 00000000 to activate the gyro.
  Wire.endTransmission(); 
  
  Wire.requestFrom(MPUaddr, 2); //two-byte temp data                                  //End the transmission with the gyro.
  byte x1 = Wire.read();
  byte x2 = Wire.read();
  int x = (int)(x1<<8)|(int)x2;
  
  //------compute temp from x-----
  float mpuTemp = (float)(x/340.0 + 36.53);  //formula from data sheets
  Serial.print("Temp = ");
  Serial.print(mpuTemp, 2);  //2-digit after decimal point
  Serial.println(" degC");
  delay(1000);
}

4. Upload the following Library based sketch to check the response of the temperature element of MPU6050 sensor.

#include <MPU6050.h>
#include<Wire.h>

MPU6050 mpu;   //device address 0x68 keyed in the library

void setup()
{
  Serial.begin(9600);
  Serial.println("Initialize MPU6050");
}

void loop()
{
  float temp = mpu.readTemperature();
  Serial.print("Temp = ");
  Serial.print(temp, 2);
  Serial.println(" *C");
  //-------------------------
  delay(1000);
}
1 Like

Ok so the VCC pin is connected to 5V, SCL is connected to A5 and SDL is connected to A4. The code is derived from the the code used by Nicholas Rehm on youtube for a vtol F35 stabilization computer, I simply modified it slightly as it didn't require the madgwick filter and such.

Have you tried a simple I2C scanner sketch?
Do you have pull-ups on the I2C lines?

Hi, @astroceo
Can I suggest you dump the code you have, for the moment, and logically use the Example code that comes with the library.

The example code is simple and needs no editing to get it to work.

Please try the Example code first to establish that you have communication with the MPU.

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

I've tried the example code for 3 of the MPU6050 libraries, the Adafruit library, Basic MPU6050 and the library I used, Arduino Mpu6050 master.

The basic example for the Adafruit library prints an error message when run "Error compiling for Arduino", the Basic Mpu6050 library just doesn't do anything and the library that I was using, the Arduino Mpu6050 master, the example for that fails with the message 'class MPU6050' has no member named 'begin'.

Can you post this code?
Have you soldered the pins on the module?
Can you please post an image of your project?

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

I'll bet it says a whole lot more.

Sorry for the incredibly late reply but I have finally gotten the IMU soldered and I've ran your code, it suggested that there is still no sensor detected on the I2c bus, I tried the code in step 3 however that ran with a constant temperature of 36.5 degrees Celsius if I remember correctly, which was wildly inaccurate for the conditions in the room I'm testing it in. The code in step 4 printed the area message 'class MPU6050' has no member named 'readTemperature'; did you mean 'getTemperature'? so I have no clue what to do from here.

Sir you are certainly correct, my apologies. the full error message read 'fatal error: Adafruit_Sensor.h: No such file or directory
#include <Adafruit_Sensor.h>
^~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Error compiling for board Arduino Nano.'

Please, post the picture of your MPU6050 Breakout Board Module. After that I will forward to you diagnostic sketch; where, the we will be able to see message if the Sensor has been detected at address 0x68 or not.

Where have you connected the AD0-pin of the module? It should be connected at GND-pin to select I2C address at 0x68 and at 5V to select address 0x69. Reemmeber that the MPU6050.h Library has keyed the address 0x68.

sorry if the quality of the photo is horrible, I can probably get a better image if needed. note that mere minutes ago I used an I2c scanner sketch and it didn't detect the IMU.

As is often the case, that 'photo shows nothing. It is impossible to see what is connected to what on the Arduino and the breakout

I thought that would be the case, I can agree that it is a horrible photo an I'll try to get a better quality one.

Draw a schematic on paper, take a 'photo of it and upload that

Is this good enough or do you just want the schematic?

1. Connect AD0-pin of the sensor at GND pin of NANO.
2. Upload the following sketch in your NANO and report what message you have got in the Serial Monitor.

#include<Wire.h>

void setup()
{
    Serial.begin(9600);
    Wire.begin();
    Wire.beginTransmission(0x68);
    byte busStatus = Wire.endTransmission();
    if(busStatus != 0)
    {
        Serial.print("Senor is not found on I2C Bus!");
        while(1);  //wait for ever
    }
    Serial.println("Sensor is found on the I2C Bus.");
}

void loop()
{

}

The wiring in the latest photo looks OK but the soldering of the pins on the sensor looks a bit scruffy and needs checking. It would also be a good idea to check continuity of all four connections between the Nano and the sensor to eliminate possible problems with the jumpers, which are quite common, and the breadboard, which are also not unknown. Is there an LED on the sensor board and if so, does it light up ?

Have you got any female to female jumpers so that the breadboard could be eliminated from the circuit ?

Until an I2C scanner can find the board there is no point in trying to read from the sensor