Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / MPU6050 I2c Serial crashes/freezes whenever motor turns on on: November 15, 2012, 01:42:17 am
Hi everybody,

I'm building myself a balancing robot using these components:
1. MPU6050 IMU breakboard by Sparkfun (libraries provided by Jeff Rowberg)
2. Tamiya Motor DC with differential gearbox
3. Arduino Mega
4. A TTL motor driver board with L298N chip

Using the MPU6050 library, I was able to get Euler angles data from the IMU with no problem whatsoever. However, whenever I connected the motor, it will freeze. And the by freezing, I mean the code just executes the last line and stays that way. I had to either press the reset button on the Arduino or plug off the power supply of the board.

The code in loop function I used to control the motor:
Code:
output= map(abs(euler[1]*180/M_PI),0,50,0,255);
    //forward direction if the tilt angle is positive
    if ((euler[1]*180/M_PI)>0) {
      digitalWrite(motor1Pin1, LOW);  
      digitalWrite(motor1Pin2, HIGH);  
      analogWrite(enablePin1,output); //PWM, the motor goes faster if the IMU is tilted more
    }
    //reverse direction if the tilt angle is negative
    else if ((euler[1]*180/M_PI)<0) {
      digitalWrite(motor1Pin1, HIGH);  
      digitalWrite(motor1Pin2, LOW);  
      analogWrite(enablePin1,output); //PWM, the motor goes faster if the IMU is tilted more
    }
    else {
      digitalWrite(motor1Pin1, LOW);  
      digitalWrite(motor1Pin2, LOW);  
      digitalWrite(enablePin1, LOW);
    }

At first, I thought maybe there's something wrong with my code. And so, I simplified it even more by turning on the motor with constant voltage and just reading the angle data (so, no correlation between the tilt angle with the motor). The code in the loop function:
Code:
mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetEuler(euler, &q);
            Serial.print("euler\t");
            Serial.print(euler[0] * 180/M_PI);
            Serial.print("\t");
            Serial.print(euler[1] * 180/M_PI);
            Serial.print("\t");
            Serial.println(euler[2] * 180/M_PI);

           digitalWrite(motor1Pin1, HIGH);
           digitalWrite(motor1Pin2, LOW);
           digitalWrite(enablePin1, HIGH);
After just one or two seconds, it freezes.

To make sure that this really is a problem whenever the motor is connected, I used the same code but this time, I used it to control the brightness of an LED. The code:
Code:
output= abs((euler[1] * 180/M_PI));
      analogWrite(LED_PIN,(output); // the brightness of the LED will increase if the tilt angle increases
This works perfectly for hours-- no freezing, no crashes, everything was fine. And this serves as proof that my code is okay and the IMU is also okay. So I'm guessing there's a problem with the Arduino's i2c or serial?

Things I've tried that didn't solve the problem:
1. Turning off the internal pull-up resistor activated by the Wire library and use 4k7 external pull-up resistors instead.
2. Using a smaller external pull-up resistor (2k2 Ohms).
3. Decoupling by putting ceramic capacitor on the VCC and GND of the IMU and motor.
4. Using different baud rates from 115200 to 9600
5. Tried using a different IMU: Digital combo board (ITG3200 & ADXL345) from Sparkfun
6.  I've also tried separating all the power supply (before I powered everything from the arduino).
7. Tried not using the serial monitor, instead just executes the code once the board is powered.

Any help on this would be greatly appreciated. I've been dealing with this problem for weeks now, and I've also searched on numerous forums regarding this problem but to no avail.

If anything is not clear enough, don't hesitate to ask me.

Thank you in advance. smiley
Pages: [1]