Sensor not found intermittently


Around 50% of the time on booting I get "could not find a valid sensor, check wiring" messages. I have the code setup to restart the ESP8266 in this case, but it never connects once 'not found'.

Only thing that works is removing power to the sensor, reapplying and then it ALWAYS works.

So I don't think it's a wiring issue. As it's going to be battery backed up (due to being a motion alarm, it could be unplugged), and it's inaccessible, this is a real pain.

***** It's the same with no battery, just direct USB power to the Wemos (no battery shield), so I don't think it's a low volts thing. *****

Options as I see it:

  • Try another sensor, cheapskate!!
  • Some hardware way to restart power to the sensor, and replace ESP.restart with that.
  • A NC magnetic reed switch in line with 5V to the sensor, which can be externally operated

Any comments would be appreciated!

    v001 test version

    MPU6050 Triple Axis Gyroscope & Accelerometer. Motion detection.
    Read more:
    (c) 2014 by Korneliusz Jarzebski

#include <Wire.h>       //This library allows you to communicate with I2C devices. 
#include <MPU6050.h>    //

MPU6050 mpu;

void setup()

  Serial.println("Initialize MPU6050");

 while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_16G))
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(5000);    //allow time for OTA flashing
    ESP.restart();  //if sensor is not found, reboot the device


  mpu.setIntFreeFallEnabled(false);         //default false
  mpu.setIntZeroMotionEnabled(false);       //default false
  mpu.setIntMotionEnabled(false);           //default false


  mpu.setMotionDetectionThreshold(2);      //default 2
  mpu.setMotionDetectionDuration(5);        //default 5

  mpu.setZeroMotionDetectionThreshold(4);   //default 4
  mpu.setZeroMotionDetectionDuration(2);	  //default 2


  pinMode(D4, OUTPUT);      //set the LED output
  digitalWrite(D4, HIGH);   //Set the LED off (pull low to enable)

  //pinMode(7, OUTPUT);
  //digitalWrite(7, LOW);

void checkSettings()

  Serial.print(" * Sleep Mode:                ");
  Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");

  Serial.print(" * Motion Interrupt:     ");
  Serial.println(mpu.getIntMotionEnabled() ? "Enabled" : "Disabled");

  Serial.print(" * Zero Motion Interrupt:     ");
  Serial.println(mpu.getIntZeroMotionEnabled() ? "Enabled" : "Disabled");

  Serial.print(" * Free Fall Interrupt:       ");
  Serial.println(mpu.getIntFreeFallEnabled() ? "Enabled" : "Disabled");

  Serial.print(" * Motion Threshold:          ");

  Serial.print(" * Motion Duration:           ");

  Serial.print(" * Zero Motion Threshold:     ");

  Serial.print(" * Zero Motion Duration:      ");

  Serial.print(" * Clock Source:              ");
  switch (mpu.getClockSource())
    case MPU6050_CLOCK_KEEP_RESET:     Serial.println("Stops the clock and keeps the timing generator in reset"); break;
    case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break;
    case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break;
    case MPU6050_CLOCK_PLL_ZGYRO:      Serial.println("PLL with Z axis gyroscope reference"); break;
    case MPU6050_CLOCK_PLL_YGYRO:      Serial.println("PLL with Y axis gyroscope reference"); break;
    case MPU6050_CLOCK_PLL_XGYRO:      Serial.println("PLL with X axis gyroscope reference"); break;
    case MPU6050_CLOCK_INTERNAL_8MHZ:  Serial.println("Internal 8MHz oscillator"); break;

  Serial.print(" * Accelerometer:             ");
  switch (mpu.getRange())
    case MPU6050_RANGE_16G:            Serial.println("+/- 16 g"); break;
    case MPU6050_RANGE_8G:             Serial.println("+/- 8 g"); break;
    case MPU6050_RANGE_4G:             Serial.println("+/- 4 g"); break;
    case MPU6050_RANGE_2G:             Serial.println("+/- 2 g"); break;

  Serial.print(" * Accelerometer offsets:     ");
  Serial.print(" / ");
  Serial.print(" / ");

  Serial.print(" * Accelerometer power delay: ");
  switch (mpu.getAccelPowerOnDelay())
    case MPU6050_DELAY_3MS:            Serial.println("3ms"); break;
    case MPU6050_DELAY_2MS:            Serial.println("2ms"); break;
    case MPU6050_DELAY_1MS:            Serial.println("1ms"); break;
    case MPU6050_NO_DELAY:             Serial.println("0ms"); break;


void loop()
  Vector rawAccel = mpu.readRawAccel();
  Activites act = mpu.readActivites();

  if (act.isActivity)
    digitalWrite(D4, LOW);  //turn on the LED
  } else
    digitalWrite(D4, HIGH);  //turn off the LED


    if (act.isInactivity)
    digitalWrite(7, HIGH);
    } else
    digitalWrite(7, LOW);


  Serial.print(" ");
  Serial.print(" ");

  Serial.print(" ");


So I don't think it's a wiring issue.

Sure sounds like a wiring issue to me.
I would take a hard look at those soldered joints.

Sure sounds like a wiring issue to me.
I would take a hard look at those soldered joints.

Thanks, I'll replace them completely and see.