closed loop

I want to ask about something. i have a project about how to balance abeam with two dc fans , i would like to ask if anyone could tell me how to read the angle from the mpu6050 and then take the readings and i will make a refrence for example to make it equal to zero and then when the reading of the angle exceeds the zero i would make it return to the postion zero as a feedback what do i have to do ?, thanks in advance.

Google "accelerometer tilt angle" for tutorials, methods and commercial applications.

can you please send me a link ?

Another thing after reading , the tilt angle is now away from zero by a value how do i make it return back to zero to be balanced ?

marwan96:
I want to ask about something. i have a project about how to balance abeam with two dc fans , i would like to ask if anyone could tell me how to read the angle from the mpu6050 and then take the readings and i will make a refrence for example to make it equal to zero and then when the reading of the angle exceeds the zero i would make it return to the postion zero as a feedback what do i have to do ?, thanks in advance.

I do something similar with my balancing bot Youtube Video https://youtu.be/uyHdyF0_BFo Here is the code to get the Yaw Pitch and roll angles I use from the MPU6050
use this library with my code: i2cdevlib/Arduino/MPU6050 at master · jrowberg/i2cdevlib · GitHub
The calibration sketch will give you offsets that will be used in Latest code sketch that will correct manufacturing differences for your MPU6050.

Enjoy :slight_smile:
Z

MPU6050_calibration.ino (7.64 KB)

MPU6050_Latest_code.ino (7.78 KB)

thank you very much :smiley:

i want to ask about something, after reading the tilt angle and comparing it with the first position which is equal to zero how to convert the tilt angle to a pwm(pulse width modulation) signal from 0v to 5v ?

marwan96:
i want to ask about something, after reading the tilt angle and comparing it with the first position which is equal to zero how to convert the tilt angle to a pwm(pulse width modulation) signal from 0v to 5v ?

Here is my complete balancing bot code.

I needed to use a different PID routine from the standard Arduino PID_v1 while it controls temperatures fine the accuracy needed for my balancing bot was greatly lacking.

Balancing_Bot_Works_.ino code was tuned for my small balancing bot.

My PID_v2 code focuses Ki and Kd multipliers around the 1 second interval. how this is done is that we detect how long it took to get the next reading (MPU6050 generates this about every 10 milliseconds) and then I Divide the KI and Kd by that so the influence on the output is only a portion of the 1 second interval. This way even if a sample is missed or skipped the influence isn’t affected. after adding multiple samples together over the 1 second and Ki and Kd influence is calculated appropriately. note that Kp is not mentioned because it has nothing to do with time. it is specifically proportional (in * Kp = out).

pwm(pulse width modulation) signal from 0v to 5v ?

Now PWM is not a variable voltage it is a variable duty cycle that works well with dc motors. if you listen to my balancing bot motors you hear a whining noise. this is caused by the power being pulsed on and off at a high rate of frequency.

in order to get my motor to go forward and reversed I use an H-Bridge the High side enable is triggered by 2 additional Arduino Uno pins per motor. If 1 side is high and the other is low the motor will turn one way switch this and the motor will drive the other.

with your fans you may only need a transistor/mosfet to power you fan motor if it only needs to spin in one direction. like a quad copter.

Z

Balanceing_Bot_Works_.ino (17.5 KB)

PID_v2.cpp (8.08 KB)

PID_v2.h (2.94 KB)