MPU6050 with Arduino

I have written a code for getting the IMU data for MPU6050. Code is working fine . but when i upload the code and open the serial monitor readings stop after certain time , either i have to re-compile or press the button on arduino to get the readings again .

Can anyone tell me what am I doing wrong or can suggest any changes in the code ?

Welcome to the forum

What code would that be ?

Please post the sketch that you are running, using code tags when you post it

Which Arduino board are you using ?

using Arduino Nano

Code:

#include "Wire.h"
#include <MPU6050_light.h>

MPU6050 mpu(Wire);


unsigned long lastTime,lastInterval;
float accelX, accelY, accelZ,                // units m/s/s i.e. accelZ if often 9.8 (gravity)
      gyroX,  gyroY, gyroZ,                         // units dps (degrees per second)
      gyroDriftX, gyroDriftY,                // units dps
      accRoll, accPitch,                     // units degrees (roll and pitch noisy, yaw not possible)
      complementaryRoll, complementaryPitch;


void setup() {
  Serial.begin(9600);
  Wire.begin();
  
  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while(status!=0){ } // stop everything if could not connect to MPU6050
  
  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  mpu.calcOffsets(true,true); // gyro and accelero
  Serial.println("Done!\n");
  
}

void loop() {
  
  mpu.update();
  accelX=mpu.getAccX();
  accelY=mpu.getAccY();
  accelZ=mpu.getAccZ();
  gyroX=mpu.getGyroX();
  gyroY=mpu.getGyroY();
  gyroZ=mpu.getGyroZ();

    unsigned long currentTime = micros();
    lastInterval = currentTime - lastTime; // expecting this to be ~104Hz +- 4%
    lastTime = currentTime;
    doCalculations();

  Serial.println("Roll=");
  Serial.println(complementaryRoll);
  Serial.print("Pitch=");
  Serial.print(complementaryPitch);
  delay(100);

}

void doCalculations() {
  accRoll = atan2(accelY, accelZ) * 180 / M_PI;
  accPitch = atan2(-accelX, sqrt(accelY * accelY + accelZ * accelZ)) * 180 / M_PI;

  float lastFrequency = (float) 1000000.0 / lastInterval;

  complementaryRoll = complementaryRoll + ((gyroX - gyroDriftX) / lastFrequency);
  complementaryPitch = complementaryPitch + ((gyroY - gyroDriftY) / lastFrequency);

  complementaryRoll = 0.98 * complementaryRoll + 0.02 * accRoll;
  complementaryPitch = 0.98 * complementaryPitch + 0.02 * accPitch;
}

I did not see any serious problems in your code that could cause the board to freeze.
Probably, the reasons must be sought in the hardware.
Show your wiring diagram with pin numbers including power.

HI,
Change

Serial.begin(9600).;

To;

Serial,begin(115200);

And change your IDE serial monitor baud rate to 115200.

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

What is the reason behind this change ? Except "Faster is better... as long as it works"

The data sent to the Serial Monitor has printed "below" the viewing panel. Scroll the Serial Monitor up. I have not found a solution to this, only an work-around. That is to put delay(100) in your code, just after the Serial.print();

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.