Arduino Forum

Topics => Robotics => Topic started by: r0yy on Oct 11, 2020, 11:01 pm

Title: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 11, 2020, 11:01 pm
I have been working on a self balancing with the MPU6050 using DMP to get pitch values which I use it with a PID controller. The code seems to be working fine as shown in the video. But as soon as I put the bot on the ground to do its thingy MPU starts giving erratic values and the bot goes ham mostly causing arduino to freeze or the motor jitters.

I have gone through numerous articles and forums trying to debug.
I am powering the arduino uno from the USB and the motor controller(L298N) is powered by a 3S Lipo.
To eliminate the issue caused by motor noise I have added 3 0.1uf caps to both the motors. I am pretty inclined that its a hardware issue and not an issue on the code side. Would like to hear what do you guys think, pretty much stuck on this for couple of days now. Thanks for the help!


link to the video: https://youtu.be/diJG3d6wrv0

I have also attached the complete code if anyone wants to go through that
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Oct 13, 2020, 06:07 pm
Hi r0yy,

Have you tried enabling the MPU6050's digital low pass filter?

For example the following line of code sets the DLPF to bandwidth to 20Hz:

Code: [Select]
mpu.setDLPFMode(MPU6050_DLPF_BW_20);
The options are:

Code: [Select]
// Bandwidth | Accel BW (Hz) | Accel Delay (ms) | Gyro Bandwidth (Hz) | Gyro Delay (ms) | Fs(kHz)
// ----------+---------------+------------------+---------------------+-----------------+---------
//     0     |      260      |         0        |         256         |      0.98       |    8
//     1     |      184      |       2.0        |         188         |       1.9       |    1
//     2     |       94      |       3.0        |          98         |       2.8       |    1
//     3     |       44      |       4.9        |          42         |       4.8       |    1
//     4     |       21      |       8.5        |          20         |       8.3       |    1
//     5     |       10      |      13.8        |          10         |      13.4       |    1
//     6     |        5      |      19.0        |           5         |      18.6       |    1
//     7     |    RESERVED   |    RESERVED      |        RESERVED     |    RESERVED     |    8
  
// void MPU6050::setDLPFMode(uint8_t bandwidth)
// #define MPU6050_DLPF_BW_256         0x00
// #define MPU6050_DLPF_BW_188         0x01
// #define MPU6050_DLPF_BW_98          0x02
// #define MPU6050_DLPF_BW_42          0x03
// #define MPU6050_DLPF_BW_20          0x04
// #define MPU6050_DLPF_BW_10          0x05
// #define MPU6050_DLPF_BW_5           0x06
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 14, 2020, 12:17 am
Thanks for the reply,
I haven't done what you suggested. But the thing is the code behaves properly when the motor is not put under load(when the bot is left itself to balance). When testing the code with the bot kept on a box(motors arent under load), it behaves exactly it should: As shown in the video link I posted

Anyways i'll try this and revert back here. Hopefully this solves the issue! Also I've ordered another MPU6050 to cross out the possibility of a faulty sensor which might be too prone to the slightest electrical noise produced by the motors
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Oct 14, 2020, 09:58 am
Hi r0yy,

I only mention this, because I encountered glitches on my quadcopter motors, until I discovered that it was necessary to filter the gyroscope/accelerometer outputs. Setting the filter to 20Hz or 42Hz resolved the issue.

Another issue could be the motor PWM frequency. I recently had problems controlling DC motors using the DRV8833 dual DC motor driver that caused the microcontroller on my radio controlled tracked vehicle to randomly reset. The solution was to change the PWM frequency from 490Hz to 31.25kHz. This also had the advantage of taking the PWM out of the audible range that stopped the motors from humming, (well,... at least took it out of my hearing range).

To change analogWrite() to operate at 31.25kHz on digital pins 3, 9, 10 and 11 of my Arduino ProMini, (compatible with the Uno/Nano), I simply added these lines of code to the setup() function in my sketch:

Code: [Select]
TCCR1B &= ~_BV(CS11);                               // Increase the PWM frequency of timers 1 and 2 to 31.25kHz
TCCR2B &= ~_BV(CS22);
TCCR2B |= _BV(CS20);

After adding these lines, analogWrite() will output PWM at this higher frequency. Note that this only works on AVR Arduino boards. Note that digital pins 9 and 10 use timer 1 (TCCR1B), while digital pins 3 and 11 use timer 2 (TCCR2B).
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 15, 2020, 01:32 am
So I tried adding this 

mpu.setDLPFMode(MPU6050_DLPF_BW_20);

and also this

TCCR1B &= ~_BV(CS11);                               // Increase the PWM frequency of timers 1 and 2 to 31.25kHz
TCCR2B &= ~_BV(CS22);
TCCR2B |= _BV(CS20);

but the same isssue persists, I tried changing the MPU 6050 but the issue remains the same.

I ran the program without the BO Motors, basically the L298N motor driver disconnected from the Arduino, the program ran without any issues or any freezing.

As soon as I connect back the motors and motor driver back to the UNO the program runs for a couple of seconds and then freezes up or it receives garbage value from the MPU6050 causing the motor to spin at full speed.

I tried changing motors as well but it did not help. I suspect the motors are causing the Arduino to freeze up. i have tried soldering 0.1uf caps on the motors but the issue doesnt go away. At this point I am really confused and out of options.

Do you have any idea what might be going on?

Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Oct 15, 2020, 10:59 am
Hi r0yy,

I ran you code on my Arduino Uno with a MPU6050. Using the micros() function I measured your loop time. This is around 100Hz (10ms), even though your PID sample time is set to 200Hz (5ms).
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 15, 2020, 05:30 pm
oh I just noticed that myself. I adjusted the sample time according to the loop time now.

I added these lines of code to check my loop time in the void loop()

prevLoopTime = 0;
loopTime = millis();

//DMP code begins
//DMP code ends

currLoopTime = loopTime - prevLoopTime;

//PID code
//Motor state update code

prevLoopTime = loopTime


I noticed a weird thing. When the code is run without the motor, currLoopTime value is 20. When motors are connected and the bot is put to test the currLoopTime value is no longer constant it starts varying a lot and in the end leading to the crash.

I suspect something is going wrong in the DMP code when the motor load is added into the system, which is causing the crash.

At this point I have crossed out the following things that could have been factors for this issue

1) Faulty MPU: I have tried 3 different sensors all yield the same result
2)noisy power to the sensor: I have added 0.1uf along with some 10uf caps in the power rail
3)incompetent power source: I am using a 4S 1550mAh Lipo that are used for racing drone so I am pretty sure voltage sag isn't a factor.


Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 15, 2020, 06:11 pm
To cross out the issue caused by DMP, I started writing a code to get the raw values from the accelerometer and gyro and use complementary filter to get a usable angle that I could use it with the PID. The bot atleast works for a good amount of time trying to balance itself and then the MCU freezes again and the motor rotates at the last value of PWM before the MCU got stuck

So i think the DMP shouldn't be the issue. Something is causing the MCU to freeze up but I am not sure what it is. I am using 20cm long jumpers to connect the motor driver pins to the arduino and 4cm long wires to connect the MPU to the UNO. Could it be possible that the PWM noise from the long wires are causing the MCU to freeze up?
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Oct 15, 2020, 07:16 pm
Hi r0yy,

Are you using a 5V to 3.3V level shifter between your Uno and MPU6050?
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 15, 2020, 11:30 pm
I am using the GY521 breakout board for the MPU so no, I am directly connecting it to the arduino WITHOUT a level shifter
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Oct 16, 2020, 07:33 pm
Hi r0yy,

Quote
I am using the GY521 breakout board for the MPU so no, I am directly connecting it to the arduino WITHOUT a level shifter
I know that there are a lot of tutorials out there that describe connecting the MPU6050 GY521 board directly to the SCL (A5) and SDA (A4) pins of the Uno. In most cases this works OK, however the minimum high level input voltage for I2C is marginal at 0.7 * Vcc, that's a threshold of 0.7 * 5.0V = 3.5V.

By default the Wire library activates the microcontroller's internal pull-up resistors at 5V, which will pull your SCL and SDA lines, (most likely already pulled-up with 10k resistors to 3.3V on your GY521 board) a little higher.

To ensure that voltage levels are within spec, it's possible to use a level shifter, this can be two 2N7000 N-channel MOSFET transistors, or an I2C level shifter board, (note that the image uses the BSS138 and that your GY521 board already has 10k resistors or thereabouts):

(https://playground.arduino.cc/uploads/Main/i2c-level-shift-mosfet/index.png)

Standard GPIO on the Uno have a minimum high level input voltage at 0.6 * Vcc @5V, which sets the threshold at 3.0V. This means that GPIO will receive and accept 3.3V input levels OK.

Saying that, it could just be that vibration is causing connection problems. It might be just the jumper wires from your Arduino to the GY521?
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Oct 16, 2020, 07:48 pm
I should've just posted a link to this article: https://playground.arduino.cc/Main/I2CBi-directionalLevelShifter/ (https://playground.arduino.cc/Main/I2CBi-directionalLevelShifter/).
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 16, 2020, 08:02 pm
Saying that, it could just be that vibration is causing connection problems. It might be just the jumper wires from your Arduino to the GY521?
To solve this I have direcly soldered the GY521 breakout board to Uno but it doesnt seem to change anything
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 16, 2020, 08:12 pm
I do have  level shifter board laying around will give it a try and see if anything changes. I have seen many forums asking to connect the GY521 board directly some recommend using pull up resistors I'll give it a try though.
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 17, 2020, 11:23 pm
So I think I have narrowed down the issue to the Arduino. I was suspecting the Arduino to reset on motor start, so I wrote a simple code to spin the motors at 75% rpm for a brief 10s and then shut off for 3s and added Serial.print("MOTOR ON") and Serial.print("MOTOR ON") before executing the Motor ON and Motor OFF lines. I used 2 of the Arduino UNOs I had

Arduino 1: ran the code properly for a single loop and then it gave up, completely froze and the motors spun at the last pwm received.

Arduino 2: ran the code a bit longer than Arduino 1 before causing random resets and freezes.

I am powering the Arduino from my USB and obviously the Motor shield has a separate LiPo powering it. I tried powering the arduino from its DC jack with a separate 3s LiPo but things did not change.

Anyone knows what's going on here?
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Oct 18, 2020, 11:18 am
Hi r0yy,

Here's a link to Pololu's webpage on dealing with motor noise: https://www.pololu.com/docs/0J15/9 (https://www.pololu.com/docs/0J15/9). 

As you've already tried point 1 on the linked page, it might be worth trying points 2 and/or 4.
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 20, 2020, 10:27 pm
At this point I am able to get the I2C working at a stable rate by adding 10k resistors on SDA SCL as pull ups

The bot is working stable as intended with a single lipo battery powering the whole thing. However when I mount the electronics into the bot adruino starts freezing again sometimes it can't read the sensor any more. 


The bot works best with the following setup motor controller and arduino away from the motors as shown in the picture
https://imgur.com/KdvtUpn

with the other two setups the bot freezes within 5-10 seconds on powering on
https://imgur.com/p17sL6b
https://imgur.com/vlLfEOw

I am using fiber glass copper clad board as the base of the bot with the copper intact could that be an issue?

 can I use it as a ground shielding(i dont know how it works but was planning to solder a ground wire to the copper clad) 

Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Oct 21, 2020, 06:32 pm
Hi r0yy,

Thanks for the images.

It might be worth adding extra support to your MPU6050 breakout board. It's just that motor vibration can disturb the gyroscope and acceleromter, especially when the board's only physical support is a female pin header.

On my quadcopter I used two, 8mm, nylon hex threaded spacers and nuts, both with M3 threads, plus two small squares of double sided foam tape at the base of each of them, to provide cushioning from vibration:

(https://forum.arduino.cc/index.php?action=dlattach;topic=708716.0;attach=386314)

The quadcopter also required the MPU6050's DLPF to be set to 20Hz. Note that the two 2N7000 N-Channel MOSFETs provide the I2C 5V to 3.3V level shifting.
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Oct 29, 2020, 05:37 pm
I built a circuit all from scratch on a perf board,
1) added spacers to support the MPU 6050
2) added power filtering with 470uf and 0.01uf caps
3) added pull up 10k resistors on the SDA SCL lines
4) shortened all the wires as much as possible
6) when through numerous arduinos and motor drivers to cross out the problems due to a faulty board
5) Changed motors to a different kind of 12v motors

after doing all this the problem still exists. THE ARDUINO STILL FREEZES ON MOTOR START!

At this point I want to give up on this project(out of frustration) but I still want to see this through the end(The bot working). I have seen videos and tutorials with people getting away with shabby wiring, no filtering, cheap motors or parts I really dunno what is wrong. ANY HELP WOULD BE REALLY APPRECIATED.

https://imgur.com/2fWI7QO

Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Oct 30, 2020, 07:56 pm
Hi r0yy,

Quote
At this point I want to give up on this project(out of frustration) but I still want to see this through the end(The bot working). I have seen videos and tutorials with people getting away with shabby wiring, no filtering, cheap motors or parts
I know that it's really frustrating. It doesn't seem like it now, but it's actually all a learning process. Those who have it easy, don't necessarily learn how to do it right. You've gone through most of the obvious steps that would normally solve the problem.

It took me a while until I discovered that I needed spacers on my first flight controller's MPU6050 breakout board, to stop the motor vibrations affecting the sensor.

Do you have some form of circuit diagram or schematic? It could just be a wiring issue?

Also, I noticed that the L298N has a jumper that should be removed if you're using more than +12V for the supply, in order to disable its on-board +5V regulator.

You mentioned that you were using 3S and 4s Lipo batteries. 3S batteries will supply more +12V when fully charged and 4S more than that. I'm wondering in that case what you're doing wih the +12V jumper and if you're using the on-board +5V regulator?
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Nov 03, 2020, 06:13 pm
Hello Martin,
I am using a 4S lipo but with a buck converter set at 12v.
Also I am using this board as my main board to minimise any sort of wiring between the motor driver and arduino.

https://robu.in/product/cytron-dual-channel-10a-motor-driver-uno-robot-controller/

i tried running the code with MPU connected to 3v3 supply and 2.7k pull up resistors. Still the same issue arduino getting stuck.

I wrote a code that scans the I2C line for new devices and also switches motor On and Off(these two function, scanning devices and motor on/off are independent of each other) to test if the motor was causing trouble to the I2C bus, but this works properly so I highly doubt anything from the I2C side or the motors are causing this issue.

Issue due to power to arduino can also be crossed out as I am using a professional quality board(as mentioned above).

however when I still run the self balancing code without the motor driver plugged in arduino never freezes, problem is with the motors being introduced into the system, arduino just freezes after couple of seconds as motors are plugged in
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Nov 06, 2020, 09:48 am
Hi r0yy,

So now you're using a single Cytron combined Arudino Uno and motor driver board?

It might be best to post an image of your circuit diagram.

Also, what is the current rating of your +12V buck converter? If your motors are attempting to draw too much current at start-up, it might cause the microcontroller to initiate a brown out reset.
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Nov 07, 2020, 12:31 pm
Hi r0yy,

The reason why I asked about the +12 buck converter's current rating, is that if you're using to power the motors in addition to the Arduino, but it's unable to supply the peak current required by the motors, then it will enter an over current condition. If there's an over current condition, the converter will most likely fold-back the current resulting in output voltage drop, this is known as fold-back current limiting:

(https://forum.arduino.cc/index.php?action=dlattach;topic=708716.0;attach=388460)

(In addition to fold-back, other current-limiting schemes that can be employed are constant current or hiccup mode).

A momentary voltage drop might affect you microcontroller's operation.

One way around this, is to supply your motor controller direct from the battery and your Arduino through the a voltage regulator in the 7V to 12V range. It's possible to prevent an over voltage condition on your motors, by limiting the PWM duty-cycle so that the average (voltage) never exceeds 12V.

Although not essential, the battery voltage can be monitored on your Arduino's analog input pin, by using a voltage divider circuit (using two resistors and possibly a small capacitor), to bring the voltage down to a 0 to +5V level so that it can be measured. This information can be used to the limit the PWM duty-cycle and also to account for any voltage drop as the battery becomes depleted. This circuit has the additional benefit that it can also be used in conjunction an LED or buzzer, to act as a battery low indicator.
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: r0yy on Nov 11, 2020, 07:26 pm
Sorry for the late reply,
I finally figured out the issue. It was neither an issue with the code nor an issue with the hardware. The main issue was the wire library. Apparently in an I2C communication if the slave disconnects the master goes into an infinite loop as there is no timeouts set by default.

I found this thread on github if anyone wants to go through thoroughly
https://github.com/arduino/ArduinoCore-avr/pull/107


setting a timeout completely solved the issue. Now as soon as a timeout is detected the connection with the MPU6050 is refreshed which is almost at par(the time to refresh the connection) with the loop time so does not cause any issue. The bot is atlast able to balance without setting the motor to full rpm due to the i2c hang up.

The mechanical build of the bot is such that if tends to tip over to one side more often than the other, that is because I did not build a great structure for the bot its heavy on one side.
Title: Re: MPU6050 erratic values with MOTOR on LOAD - HELP!
Post by: MartinL on Nov 12, 2020, 09:25 am
Hi r0yy,

Glad to hear you've found a solution that allows your board to recover from the I2C failure.

However, the issue remains as to what is causing the I2C bus to stall, since it shouldn't be happening during normal operation. The I2C recovery is simply masking the problem, but it's one big step closer to solving what's causing it.

I suggest posting your circuit diagram even if it's just hand drawn, as it's always good to have other sets of eyes go over it. Peer review is normal practice in any engineering department, as it increases the chances of any obvious mistakes being spotted.