MPU-6050 stops working when motors are running

Hi!
I want to change the direction of two small motors according to acceleration measured by my MPU-6050.
The control of the motors with an IC witout an external battery is no problem and the sensor works fine, too.
But as soon as the motors start running, the sensor stops sending values...

Any ideas what I'm doing wrong?

Might be a power issue - generally running motors takes more current than the arduino can supply, it may be causing the other device to brown-out.

Thanks for your fast reply!
This was my first thought, too.
So I tried it first with only one motor and now again with the motor powered external, but it still doesn't work.
Maybe it's a bug in my code?

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

MPU6050 accelgyro;

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

int mlPin1 = 5;
int mlPin2 = 12;
int mrPin1 = 6;
int mrPin2 = 13;
int speedL = 170;
int speedR = 170;


void setup() {
    Wire.begin();
    Serial.begin(38400);
    accelgyro.initialize();

    pinMode(mlPin1, OUTPUT);
    pinMode(mlPin2, OUTPUT);  
    pinMode(mrPin1, OUTPUT);
    pinMode(mrPin2, OUTPUT);
}

void loop() {
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    ax = map(ax, -32767, 32767, -255, 255);
    ay = map(ay, -32767, 32767, -255, 255);
    az = map(az, -32767, 32767, -255, 255);
 
    Serial.print(ax); Serial.print("\t");
    Serial.println(ay); Serial.print("\t");
    
  if (ax > 5)
  {
    analogWrite(mlPin1, speedL);
    analogWrite(mrPin1, speedR);
    digitalWrite(mlPin2, LOW);
    digitalWrite(mrPin2, HIGH);  
  }
  else if(ax < -5) 
  {  
    analogWrite(mlPin1, speedL);
    analogWrite(mrPin1, speedR);
    digitalWrite(mlPin2, HIGH);
    digitalWrite(mrPin2, LOW);
  }
  else
  {
    analogWrite(mlPin1, 0);
    analogWrite(mrPin1, 0);
  }
}

short time (about 0.5-1 sec) after the if or else if statement becomes true, the sensor stops working and the motors keep running.

Classic case.
Your motors generate interference and that upsets the processor chip.

Cure ---- apply supply decoupling:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

thank you! i will try that!

I was having the same problem for a couple of weeks, and I just solved it in the last hour. I'm making a self-balancing robot, with an H-bridge to control two motors. The robot is connected to my laptop through a USB cord and data is read as the robot operates.

I can read the values no problem, while only using the USB connection for power. I can run the motors only using the USB connection for power. I can't do both without the values freezing.

I was using the USB power to power the Arduino and the motors, after about 2 to 3 seconds the data would cease to be sent and the wheels would continue to spin at their last speed. Then I decided to use a 9 volt to power the Arduino, but the problem still persisted. Finally I decided to add another 9 volt battery for the motors, which I ran through a 5 volt regulator so that it would be consistent with my previous attempts using 5v and since the battery will eventually drain. Finally when I used a 9v for the Arduino, a 9v regulated down to 5v for the motors, and the USB connection, there hasn't been a freezing issue.

TL;DR: Get a separate battery for the motors and the arduino.

P.S. I added two capacitors in parallel with the voltage regulator as in figure 4 of the spec sheet: http://www.alliedelec.com/images/products/datasheets/bm/STMICROELECTRONICS/70013702.pdf
Not sure if the capacitors will make much of a difference in your case though.

I am developing a self balancing robot and using a mpu 6050 for finding the angle of lift . i too face the same problem... for a small tilt it works fine and in middle my mpu stops sending the value to arduino... and the motor continues to rotate at the last received value... could any one help me to solve this problem....

chazm:
TL;DR: Get a separate battery for the motors and the arduino.

Separate batteries is an expensive and not very efficient way of decoupling supplies.
There is nothing that separate batteries will do that proper decoupling will not do better.

I have same problem.

Hello there,
I have same problem i am building Self balancing robot, using IMU6050, i2cdev lib by Jeff Rowberg,
as i turn on motors imu stops giving output, first give wrong output then stops giving any output.
i tried coupling capacitors and also usind 2.2K ohm pull up resistance on imu.
Please help i am in big trouble.
Any help ll be appreciated.

th problem was with DC motors, motors were only 5V rating i was running those on 12V with PWM half ,
Now i got 12V DC motors it work as charm with imu6050
if anyone has similar problem i ll be happy to answer.

getting the same problem... working on a self-balancing bot..
i am using a common battery for arduino and mpu 6050. should i use a different batteries for both or use a 12v dc motor.?

Iam working on self balancing robot. The value from mpu6050 is stoping when connected to motor, that is when the motor starts rotating the reading from mpu6050 is stoping and the motor is working at last pwm value. I am using seperate power supplies for arduino and motors still it occures.

Grumpy_Mike:
Separate batteries is an expensive and not very efficient way of decoupling supplies.
There is nothing that separate batteries will do that proper decoupling will not do better.

Having said that when the motors stall and crow-bar the supply voltage, separate supplies
is going to protect your microcontroller from reseting. Certainly with 9V batteries which
can barely power motors anyway (rechargable pack of high-current rated AA's are a much
more useful motor supply)