Remove noise from dc motors

Hey

Im working on a robot right now. It uses 2 dc motors with
L298n and an arduino uno with sensor shield. I also added a mpu 6050 to give the robot some orientation.
For example i want to make the robot turn exactly 90 degrees from its current position. Ive already testet the mpu by hand. It is perfectly calibrated so it shows a change of exactly 180 degrees when i turn the mpu by 180 degrees.

The problem now is that the mpu orientation change and is not correct when the dc motors are working. The orientation keeps changing significantly.

I read about dc motor noise which may affect sensors. This prob can be solved by adding capacitors to the dc motors. Consider this short article:

Did i understand it correctly? I just need to put 0.1 capacitors between the wire and the dc motor and the sensor will work correctly then? Pls correct me if i missed sth.

Im hesitating because i need to buy them first .

As always i would be very happy about any response that can help.

Your statement, "The problem now is that the mpu orientation change and is not correct when the dc motors are working. The orientation keeps changing significantly. ", gave you the clues, but you reached the wrong conclusion.

What do you get when you send DC through wires? Magnetism!

Get any power wires as far away form the MPU 6050 as possible!

Paul

Magnetic fields can be screened by Mu metal. You can get foil but it is not cheap.

Paul_KD7HB:
Your statement, "The problem now is that the mpu orientation change and is not correct when the dc motors are working. The orientation keeps changing significantly. ", gave you the clues, but you reached the wrong conclusion.

What do you get when you send DC through wires? Magnetism!

Get any power wires as far away form the MPU 6050 as possible!

Paul

Is this far enough?
I tested it again and there was no change.

Grumpy_Mike:
Magnetic fields can be screened by Mu metal. You can get foil but it is not cheap.
Mu-metal - Wikipedia

Yes that would be a good solution too. But i dont want to spend much money
and the solution with the capacitors seemed to be cheap and effective too.

So do you think the capacitors will work ?

So do you think the capacitors will work ?

No.
Capacitors only counter electrical noise. If the problem is need magnetic noise capacitors will have no effect.

Is this far enough

Well I would say not, the wires go right over the motors it is hard to see how you think that would help.

Solderless bread board on a moving robot just shouts unreliability.

Grumpy_Mike:
Well I would say not, the wires go right over the motors it is hard to see how you think that would help.

Solderless bread board on a moving robot just shouts unreliability.

Okay then what do you suggest? You wrote that the wires go right over the motors
so one idea is to put the MPU right behind the arduino, so the wires dont need to go
over the dc motors. Im not at home but i will try when i can. What do you think?
Do you have another/better suggestion?

And what do you mean with the breadboard? Why is it unreliable?

Thank you very much for your answers! I really appreciate your help.

Motors generate a lot of noise on power rails. Since it is cheap and easy I would try to add some decoupling caps close to the MPU to see if it helps.

Smajdalf:
Motors generate a lot of noise on power rails. Since it is cheap and easy I would try to add some decoupling caps close to the MPU to see if it helps.

Thank you for your answer!

Okay then how do i connect those decoupling capacitors to the mpu? How
could that help. Or how far does that differ from capacitors at the dc motors?

Connect a capacitor between GND and Vcc as close to the MPU as possible. I guess a small ceramic cap is already present on the breakout board so I would try "large" (100-1000uF) electrolyte cap.
Real wires have some non-zero inductance and resistance (and a lot of other non-ideal features). That makes placement of caps important and a cap at the motor is much less effective that a cap at the MPU in filtering power supply noise (for the MPU). But the cap at the motor is important too to prevent other forms of EMI.

It seems you have a few problems here.

Solderless breadboard: not the most reliable but fine for initial testing and getting the circuit right. If you want to keep it for longer, solder everything on perfboard or stripboard, and replace the Uno by a Nano or Pro Mini.

Motors: produce noise. A 100 nF cap between the two terminals, and between each terminal and the motor's casing is best to filter noise. If that's the same robot kit as I have, you're probably limited to the first (100 nF ceramic between the terminals). Not at the breadboard as then the noise can radiate from the wires: tackle it at the source.

Motor power wires: produce magnetic fields, just from the current flowing through them. Keep those wires away from your magnetometer. If that's far enough, is easy enough to find out: just try it.

The Arduino board has its own filtering caps, this should not be an issue.

Those electrolytics are decoupling caps, very different function from the 100 nF filtering caps at the motor. Hence the different value, different type and different placement.

The MPU6050 does not have a magnetometer, so it is not susceptible to magnetic fields. It also cannot reliably measure changes in the Yaw angle, for exactly that reason. Yaw angles always drift when measured using the MPU6050, because there is no North reference.

Do you really have an MPU6050?

Post your code, using code tags.

Smajdalf:
Connect a capacitor between GND and Vcc as close to the MPU as possible. I guess a small ceramic cap is already present on the breakout board so I would try "large" (100-1000uF) electrolyte cap.
Real wires have some non-zero inductance and resistance (and a lot of other non-ideal features). That makes placement of caps important and a cap at the motor is much less effective that a cap at the MPU in filtering power supply noise (for the MPU). But the cap at the motor is important too to prevent other forms of EMI.

So you say that i should place those caps on the connection of the mpu?

wvmarle:
It seems you have a few problems here.

Solderless breadboard: not the most reliable but fine for initial testing and getting the circuit right. If you want to keep it for longer, solder everything on perfboard or stripboard, and replace the Uno by a Nano or Pro Mini.

Motors: produce noise. A 100 nF cap between the two terminals, and between each terminal and the motor's casing is best to filter noise. If that's the same robot kit as I have, you're probably limited to the first (100 nF ceramic between the terminals). Not at the breadboard as then the noise can radiate from the wires: tackle it at the source.

Yes i would solder the components, when i make the robot do what i wanted.

What exactly are the terminals? (Sorry my native language is not english) You mean the dc motors themselves? So you say i should place the caps at the dc motor.

//I saved the quaternion functions in this header
#include "Quat.h"

//Yaw
int angle;

unsigned long initTime = 20000;

void setup() {

  Serial.begin(115200);

  if (!initIMU())
    Serial.println("Error initializing IMU");

  while (millis() < initTime)
  {
    updateMPUValues();
    angle = getYaw();
    Serial.println(angle);
  }




}

void loop() {


  updateMPUValues();
  angle = getYaw();


  Serial.println(angle);

}

jremington:
The MPU6050 does not have a magnetometer, so it is not susceptible to magnetic fields. It also cannot reliably measure changes in the Yaw angle, for exactly that reason. Yaw angles always drift when measured using the MPU6050, because there is no North reference.

Do you really have an MPU6050?

Post your code, using code tags.

Yes im sure that i have an mpu 6050.
You said it has no north reference. But when i test the mpu by hand (without being
connected to the robot) such a problem doesnt occur.

such a problem doesnt occur.

You did not test long enough to see it.

jremington:
You did not test long enough to see it.

Then my only solution will be using a mpu 9250 with magnetometer? Then i can use a lib that fuses gyro accel und mag together so i can get a reliable yaw value?

Honestly i tried that option. I already have a mpu 9250.. the problem was that the libs that fuses gyro accel and mag were too big for my arduino uno. I will need a bigger mc like the arduino mega. Pls correct me if im wrong.

If you are just steering ground vehicle, you don't need an IMU at all. Just use a magnetometer as a compass, which is not affected by motion. They are affected by magnetic fields, though and that is corrected by calibration in place.

All magnetometers must be calibrated to be useful as a compass. Here is an overview of the best approaches: Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

Depending on your vehicle, you might also be able to get away with the simple "min/max" calibration approach, discussed in this forum post: https://forum.pololu.com/t/correcting-the-balboa-magnetometer/14315.

eniddelemaj:
What exactly are the terminals? (Sorry my native language is not english) You mean the dc motors themselves? So you say i should place the caps at the dc motor.

The little bits sticking out from the motor where wires connect to. That's where the filtering caps go.
Don't place the electrolytics there, just the small ceramic ones.

10 to 100nF ceramic will knock out most of the RF interference - going much higher will impose extra load
on the H-bridge and may actually increase lower frequency interference due to current spikes needed
to charge/discharge the capacitor. Good supply decoupling for the motor supply at the motor driver is
important always, otherwise you radiate interference from the whole power supply wiring. This can be 1000uF
or more I reckon.

jremington:
If you are just steering ground vehicle, you don't need an IMU at all. Just use a magnetometer as a compass, which is not affected by motion. They are affected by magnetic fields, though and that is corrected by calibration in place.

All magnetometers must be calibrated to be useful as a compass. Here is an overview of the best approaches: Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

Depending on your vehicle, you might also be able to get away with the simple "min/max" calibration approach, discussed in this forum post: https://forum.pololu.com/t/correcting-the-balboa-magnetometer/14315.

To be honest i didnt make good experiences with magnetometer, because it seems very hard to
calibrate them properly. I only reached a deviation of 5 to 10 degrees. Thats too much for my Problem. Plus it is very slow in contrary to the combination of gyro and accel. I know the first tutorial. I worked with it.

MarkT:
10 to 100nF ceramic will knock out most of the RF interference - going much higher will impose extra load
on the H-bridge and may actually increase lower frequency interference due to current spikes needed
to charge/discharge the capacitor. Good supply decoupling for the motor supply at the motor driver is
important always, otherwise you radiate interference from the whole power supply wiring. This can be 1000uF
or more I reckon.

Yes i ordered them already. Have to wait for shipment.

I found out that the problem only appears, when the motors are blocked. For example
when i block the wheels by my hand the problem appears. But when i hold the robot
in the air the problem does not appear.
I think it has to do with the change of voltage.

i didnt make good experiences with magnetometer, because it seems very hard to
calibrate them properly. I only reached a deviation of 5 to 10 degrees. Thats too much for my Problem. Plus it is very slow in contrary to the combination of gyro and accel.

Then you must have been doing several things wrong. It does take some effort to properly calibrate a magnetometer, but the result will be faster and much, much more accurate than using a gyro and accelerometer. I'm sorry that you give up so easily.

Your comment:
"I found out that the problem only appears, when the motors are blocked. For example
when i block the wheels by my hand the problem appears. But when i hold the robot
in the air the problem does not appear.
I think it has to do with the change of voltage. ".

This makes ask about the type of motor. Do they have brushes? More than likely the problem is the change in current, not voltage.

Paul