MPU6050, incorrect reading with battery

Hi! I am making a twp wheel balancing car, using Arduino Nano, Mpu6050, A4988 x2, stepper motor x2 and 18650 battery x3. I have two problems now.

  1. In my code, 90 degree represents vertical position. When batteries are not connected in circuit, the angle calculated by Nano is good.
    But when batteries are connected into circuit, the calculated angle is between 82 and 83 while the actual angle is very close to 88 or 89. And when I rotate the car by hand, value of the calculated angle doesn't change in a from about 83 degree to 95 degree, until its position reaches the boundary values.

  2. The second question is actually about PID. The car now oscillates fast around vertical position (equilibrium point or target position, I guess). I tried many values, but none of them really useful.

Can you give me any advice for the blank angle range or PID setting?
Attachments below are arduino sketch and circuit.

Thank you very much!

Nano_main_one.ino (11.4 KB)

Hi,
Please post your code using tags.
OPs Circuit.


Thanks.. Tom... :slight_smile:

Hi,
Have you a DMM?
Measure the battery voltage when the problem occurs.
Measure the LM7805 output when the problem occurs.

You need to place two bypass capacitors close to the LM7805 regulator input and output leads.
Look up and LM7805 datasheet and it will show you what you need. They help with regulation and noise immunity.

Make sure your motor supply wires go directly to the battery and the Arduino power supply wires do separately so that motor noise does not get onto the Arduino Supply.

Tom... :slight_smile:

advice from a fellow newbie who also plans to use a Nano and MPU-6050;

** **START SLOWER** **

i see your code has jumped right in with all the "standard" formulas for PID control, the alpha in the filters, etc.

you need to (if you want to understand it from first principles) to bring it all the way down. (well, i found out i did have to, personally.)

start with only Proportional control - that's what i have only just "conquered" - and that was using a simple "line follower" mockup with nothing but a micro-servo and an LDR (+illumination)

that servo/LDR setup is perfect for learning to find the right PTerm without any oscillation.

as you start moving the line faster, the oscillation begins to appear/creep in, and then you start having to play with the Derivative part - the DTerm.

my 2 cents, if you dive right in with ALL THREE IN ONE GO - you could be just playing hit & miss with fiddling all the Ks until you happen to find a suitable setting - learn the basics first and you can then confidently set the PID - having understood each term and it's individual effect on the error correction.

BabyGeezer:
advice from a fellow newbie who also plans to use a Nano and MPU-6050;

** **START SLOWER** **

my 2 cents, if you dive right in with ALL THREE IN ONE GO - you could be just playing hit & miss with fiddling all the Ks until you happen to find a suitable setting - learn the basics first and you can then confidently set the PID - having understood each term and it's individual effect on the error correction.

Excellent Advice.
Tom... :slight_smile:

Hi! guys, first thank you for your advises about noise and PID!

I measured the output voltage, it always stays stably at 5.04 V (I am not sure if my $20 DMM could detect noise at higher frequencies)

I made some changes. I added one more 7805 and two 3.7V Lipo batteries, using these to power Nano and mpu6050. Using 18650 batteries to power motors and A4988 drivers.
I added capacitors to input of 7805; I think the datasheet says 0.33 uF, but the closest ones I have are 10 uF and ceramic 0.1 uF. So I used 0.1 uF, but I don't know if this is suitable.
The result is slightly improved, it now can show angle around 85 degree somehow.

And something else about the car. In the tests I have done, it can drive itself towards middle (vertical position) with large enough kp, but usually its body tends to tilt to one direction, I don't know if this is caused by improper kp kd, mass center offset from middle, or the acquired angle ?