I have been trying to create arduino code where my MPU6050 reads its angle and based on that angle, change the motor speed. For example, 45° is the max motor speed, 22° is roughly half speed, and so on.

I have a 5065 200kv brushless motor going to a ESC with a bec which I can attach to the Arduino. I have modified some self-balancing drone code from a tutorial that used a proportional-integral-derivative controller. What I want to do is take out the PID and do a comparison: if the angle is between 5° and 45° then servo write (angle × 44), because if I multiply the maximum angle by 44 I will get around 2000, which is the original top speed of the code. Although mine can go faster, and I will change it later after testing. I am fairly new with Arduino and I have not used the MPU6050 before.

#include <Wire.h>
#include <Servo.h>

Servo right_prop;

int16_t Acc_rawX, Acc_rawY, Acc_rawZ,Gyr_rawX, Gyr_rawY, Gyr_rawZ;

float Acceleration_angle[2];
float Gyro_angle[2];
float Total_angle[2];

float elapsedTime, time, timePrev;
int i;
float rad_to_deg = 180/3.141592654;

double throttle=0; //initial value of throttle to the motors

void setup() {
  Wire.begin(); //begin the wire comunication


  time = millis(); 
void loop() {

/////////////////////////////I M U/////////////////////////////////////
    timePrev = time;  
    time = millis();  
    elapsedTime = (time - timePrev) / 1000;



     Acceleration_angle[0] = atan((Acc_rawY/16384.0)/sqrt(pow((Acc_rawX/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;
     Acceleration_angle[1] = atan(-1*(Acc_rawX/16384.0)/sqrt(pow((Acc_rawY/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;

   Wire.write(0x43); //Gyro data first adress
   Wire.requestFrom(0x68,4,true); //Just 4 registers


   Gyro_angle[0] = Gyr_rawX/131.0; 
   Gyro_angle[1] = Gyr_rawY/131.0;

   /*---X axis angle---*/
   Total_angle[0] = 0.98 *(Total_angle[0] + Gyro_angle[0]*elapsedTime) + 0.02*Acceleration_angle[0];
   /*---Y axis angle---*/
   Total_angle[1] = 0.98 *(Total_angle[1] + Gyro_angle[1]*elapsedTime) + 0.02*Acceleration_angle[1];


if( Total_angle[1]> 5 && Total_angle[1]< 45)

//end of loop void
  • \$\begingroup\$ i have modified code using the code of a self balancing drone which used the mpu6050 and made a PID control system to balance the drone between two motors spinning at different speeds to counteract the other motor and make the drone level. My modified code is my attempt at removing the PID control and make it so the arduino reads what angle the sensor is at and tells my one motor to go a certain speed depending on the angle. If you would like i could post the original code or the video about it on youtube. \$\endgroup\$ – miles Feb 27 '19 at 22:55
  • \$\begingroup\$ Where did the original code come from? \$\endgroup\$ – 200_success Feb 27 '19 at 22:56
  • \$\begingroup\$ A youtube video youtu.be/AN3yxIBAxTA \$\endgroup\$ – miles Feb 27 '19 at 22:59
  • \$\begingroup\$ I have gone back and corrected all the errors from copying it. I appologize if i did this completely wrong. I believe i got rid of anything related to the PID and kept everything for reading the angle of the sensor to tell the motor what to do. Would it be simpler to make code that purely reads the angle of the motor and just add a “servo” to the code then do my if-then statement in the loop? \$\endgroup\$ – miles Feb 28 '19 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.