Hi everybody,
I'm building myself a balancing robot using these components:
- MPU6050 IMU breakboard by Sparkfun (libraries provided by Jeff Rowberg)
- Tamiya Motor DC with differential gearbox
- Arduino Mega
- 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:
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:
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:
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:
- Turning off the internal pull-up resistor activated by the Wire library and use 4k7 external pull-up resistors instead.
- Using a smaller external pull-up resistor (2k2 Ohms).
- Decoupling by putting ceramic capacitor on the VCC and GND of the IMU and motor.
- Using different baud rates from 115200 to 9600
- Tried using a different IMU: Digital combo board (ITG3200 & ADXL345) from Sparkfun
- I've also tried separating all the power supply (before I powered everything from the arduino).
- 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.