Mpu6050 not connecting to Esp32 after coming back from DeepSleep Mode

235 Views Asked by At

I am a complete noob in hardware programming. I was doing a project in which i had to calculate roll and pitch values from mpu6050 while implementing deep sleep mode in esp32 to reduce power consumption.

I followed online tutorials and managed to get the esp32 into deepsleep mode, but after i come back from deepsleep mode, i fail to reconnect to mpu6050 (mpu6050 status gives 1).

Here is my code,

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

#define uS_TO_S_FACTOR 1000000ULL  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  10        /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR bool setupOnce = false;
RTC_DATA_ATTR MPU6050 mpu(Wire);

void setup(){
  Serial.begin(115200);
  delay(1000); //Take some time to open up the Serial Monitor

  //Setup the MPU6050 Once
  if (setupOnce == false){
    setupOnce = true;

    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");
    Serial.println();
  }

  else{
    byte status = mpu.begin();
    Serial.print(F("MPU6050 status: "));
    Serial.println(status);
    while(status!=0){ } // stop everything if could not connect to MPU6050
  }

  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  //Calculate Values
  calcValues();

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  esp_deep_sleep_start();
}

void calcValues(){

  mpu.update();

  Serial.print(F("TEMPERATURE: "));
  Serial.println(mpu.getTemp());

  Serial.print(F("(Roll:Pitch:Yaw) - ("));
  Serial.print(mpu.getAngleX());
  Serial.print(":");
  Serial.print(mpu.getAngleY());
  Serial.print(":");
  Serial.print(mpu.getAngleZ());
  Serial.println(")");
  Serial.println(F("=====================================================\n"));
}

void loop(){
  //This is not going to be called
}
0

There are 0 best solutions below