1

SOLVED: I "solved" the problem by cycling my Arduino's power and I haven't had an issue since. I would still like to figure out what was going on. I still think it's something in that MPU6050 library with getMotion. But anyway, if anyone eventually has any ideas post it up. Thanks


Original question: I'm using an MPU6050 accelerometer/gyro that controls a servo through a Mega.

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);

}
4
  • I suspect something with the i2c bus. There can be many problems with a i2c bus. For example a bad quality breadboard or long wires. For the arduino mega you need a level shifter for the sda and scl. Can you show a photo of the wiring between the arduino mega and the mpu-6050? Could you upgrade to a 3.3v arduino board, a mpu-9250, and the ahrs filter? Then you can use this guide: learn.sparkfun.com/tutorials/mpu-9250-hookup-guide When we ask extra information, please add that information to your question. – Jot Apr 26 '19 at 3:38
  • Can you measure the voltage on the SCL and SDA lines, when the freezing happens? Despite the rather short (and supposely rare in relation to the whole time) transmission period, both lines should be HIGH. If you see one of the lines being LOW for a longer time, then the I2C communication freezes for some reason during transmission, blocking the bus. May be a good starting point for debugging. – chrisl Apr 26 '19 at 7:41
  • I suspect you may be right about the I2C but I think it may be a library issue. I tried a second breadboard and I've heard of the wire issues with I2C but these are standard hobby jumpers. Why do you think the level shifter is necessary at clock and data? I hooked it straight to the 3.3v on the Mega but it also appears to be 5v tolerant because I tried that out too and left it on all day and it was working okay. I unfortunately don't have an oscilloscope or multimeter on hand but I can look into that. – ardoknow Apr 26 '19 at 17:28
  • I "solved" the problem by cycling my Arduino's power and I haven't had an issue since. I would still like to figure out what was going on. I still think it's something in that library. But anyway, if anyone eventually has any ideas post it up. Thanks – ardoknow Apr 26 '19 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.