[SOLVED] My accelerometer is hanging for some reason

Hi! I've got this for an MPU6050 accelerometer/gyro that controls a servo.

QSTN: The sensor simply stops reading after an arbitrary time, usually between 2-4 minutes. I have to reinitialize the serial monitor/MPU to get it to work again which is not good. I suspect it has something to do with getMotion but I can't put my finger on it.

Any ideas? Thanks. Code is below:

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Servo.h"

MPU6050 mpu;

int16_t ax, ay, az;
int16_t gx, gy, gz;

Servo servo1;

int val;
int prevVal;

void setup() 
{
    Wire.begin();
    Serial.begin(38400); //38400

    Serial.println("Initializing MPU6050");
    mpu.initialize();
    Serial.println(mpu.testConnection() ? "CONNECTED" : "Connection failed");
    servo1.attach(9);
}

void loop() 
{
  
  
    mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    val = map(ay, -17000, 17000, 0, 179);
       
    
    if (val != prevVal)
    {
        servo1.write(val);
        prevVal = val;
        Serial.println(val);
    }
    
    delay(75);
   
}

SOLUTION: I just restarted my Arduino. Not sure what was going on but I haven't seen any issues since. I would still like to know what the issue was and I still suspect it's a Structure issue in the MPU6050 library.

Things happen if you try to power a servo from the 5V output of the Arduino.

Use a separate servo power supply (e.g. 4xAA batteries) and connect the grounds.

jremington: Things happen if you try to power a servo from the 5V output of the Arduino.

Use a separate servo power supply (e.g. 4xAA batteries) and connect the grounds.

Are you saying that the servo is providing feedback to the board that is killing the process?

Because in the serial monitor you can see that the MPU6050 is not communicating anything at all which means that it is being killed for some reason.

Are you attempting to power the servo using the Arduino 5V output? If so, that could actually destroy the Arduino.

jremington: Are you attempting to power the servo using the Arduino 5V output? If so, that could actually destroy the Arduino.

It's a micro servo. It should be fine powered off the Mega. I don't think that's the problem.

How long are the wires between the accel and the Arduino?

groundFungus: How long are the wires between the accel and the Arduino?

Around 6 inches. They're standard hobby jumper wires.

ardoyouknowwhoiam: It's a micro servo. It should be fine powered off the Mega. I don't think that's the problem.

No, that's not the case at all. Servos need separate high current supply, even micro servos pull around 1A peak, which is going to cause havoc from the logic supply (as your problem suggests).

MarkT: No, that's not the case at all. Servos need separate high current supply, even micro servos pull around 1A peak, which is going to cause havoc from the logic supply (as your problem suggests).

I've measured these particular micro servos in the past and they draw about 700mA which is quite high you are right. But that being said there are a few reasons why I know that that's not the problem:

1) the same exact freezing thing happens if I completely remove the servo from the board and just read my MPU6050 output.

2) if I operate using Wire and simply do not use MPU6050 in my code I don't have this problem which tells me it has something to do with a Structure in that library.

Do you have any ideas?

It should be fine powered off the Mega.

Dead wrong, regardless of whether that is causing the freezing.

How are you powering the MPU6050? As far as I can see, it's a 3.3v device.

leongjerland: How are you powering the MPU6050? As far as I can see, it's a 3.3v device.

I am using 3.3V but it would appear that this particular module is also 5v tolerant because I tried it as well. I have a few of them.

ardoyouknowwhoiam: I am using 3.3V but it would appear that this particular module is also 5v tolerant because I tried it as well. I have a few of them.

Please post a link to the datasheet that specifies 5V tolerant. I'm not finding one.

Datasheet link

adwsystems: Please post a link to the datasheet that specifies 5V tolerant. I'm not finding one.

Datasheet link

It specifies it in the datasheet you posted. I had only said that it "appears" to be tolerant because I experimented with 5v and it worked fine. Now it is confirmed. On page 20 it says absolute max supply is 6v. Makes sense that they would design it that way as not everyone has access to 3V3 at all times.