Controlling Servo with MPU6050

So I’m doing a project where the gyroscope on the MPU6050 module will move a servo. But every example I found online ether didn’t work or used the accelerometer which is not what I want because this is going to go on a foam glider which is already accelerating. I tried a bunch of different ways but some how I couldn’t get this code to use the gyroscope instead of the accelerometer (this code can move the servo using the accelerometer). I don’t know what to do!

#include <GY6050.h>

//hammadiqbal12@gmail.com
//https://www.youtube.com/watch?v=Cvtr3LKdqvk


#include <GY6050.h>           //library for GYRO 
#include <Wire.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo


int X = 0;
int Y = 0;
int Z = 0;
GY6050 gyro(0x68);              //to save GYRO data


void setup() {

  Wire.begin();            //initializing GYRO
  gyro.initialisation();
  delay(100);
  myservo.attach(9);

}

void loop() {

  X = map(gyro.refresh('A', 'X'), -90, 90, 0, 180);                //mapping the gyro data according to angle limitation of servo motor
  Y = map(gyro.refresh('A', 'Y'), -90, 90, 0, 180);
  Z = map(gyro.refresh('A', 'Z'), -90, 90, 0, 180);
  myservo.write(Y);                                          //movement of Y axis will control servo
  delay(15);

}

I would start by writing a simple program with some Serial prints in just to show what values you get from the gyro mode. I.e. changing the 'A' in the gyro.refresh statements to 'G' (for gyro). Then you'll have something to work with. Or you could just run the MPU6050_test_Serial example program that comes with the library which does that and more.

Then you can do a more detailed specification because it's really not clear what you expect the servo do in response to what gyro values.

Steve

This is great advise, thanks. But I already tried to change the A to G but then it didn't work at all!

OH and also when I tried the example program you suggested the Gyro X and Y on the serial monitor only change to 1 for like a second then go back to zero even though I'm still tilting the module.

I am doing this project where a servo moves depending on the gyroscope on the MPU6050 module. I got this example code but the Gyro X and Y values only change for a second then go back to zero. How can I move the servo depending on these raw Gyro values?

#include <Wire.h>
#include <GY6050.h>


GY6050 gyro(0x68);                            // Create Gyro object with "0x68" as I2C addres (most likely the address of your MPU6050)


void setup()                                  // Main setup
{
  gyro.initialisation();
  Serial.begin(9600);
  Serial.println("MPU6050 Test");
  delay(2500);
}
void loop()                                  // Main loop
{
 // Serial.print("AcX = ");
  //erial.print(gyro.refresh('A', 'X'));           // Ask for the X axis of the Accelerometer and print it
  //Serial.print(" | AcY = ");
  //Serial.print(gyro.refresh('A', 'Y'));           // Ask for the Y axis of the Accelerometer and print it
  //Serial.print(" | AcZ = ");
  //Serial.print(gyro.refresh('A', 'Z'));           // Ask for the Z axis of the Accelerometer and print it
  //Serial.print(" | Tmp = ");
  //Serial.print(gyro.refresh('T', 'C'));           // Ask for the Temperature in Celsius and print it
  //Serial.print(" C | ");
  //Serial.print(gyro.refresh('T', 'F'));           // Ask for the Temperature in Farenheit and print it
  //Serial.print(" F");
  Serial.println(" | GyX = ");
  Serial.print(gyro.refresh('G', 'X'));           // Ask for the X axis of the Gyroscope and print it
  Serial.print(" | GyY = ");
  Serial.print(gyro.refresh('G', 'Y'));           // Ask for the Y axis of the Gyroscope and print it
  //Serial.print(" | GyZ = ");
  //Serial.println(gyro.refresh('G', 'Z'));         // Ask for the Z axis of the Gyroscope and print it, then carriage return
  delay(50);
}

How do you think a sensor works that gives you acceleration values, then continues to give that same value when there is no acceleration?

Paul

Ok so I have been doing electrical engineering for a few years now but only programming in the past year. I am not a pro programmer. I just want to know how I would convert the raw gyro values to be able to move the servo (I also don't want the accelerometer).

buavts:
Ok so I have been doing electrical engineering for a few years now but only programming in the past year. I am not a pro programmer. I just want to know how I would convert the raw gyro values to be able to move the servo (I also don't want the accelerometer).

Well, the device specs show: "Three-axis MEMS rate gyroscope sensor with 16-bit ADCs and signal conditioning".
The gyro data is the RATE OF CHANGE in each of the three axes. So, I guess you would have to integrate these values over the time period you ware concerned with. Otherwise, I don't know what information you are after.
Paul

The information I am after is the actual lines of code I would have to add because as I mentioned earlier, I am NOT the best programmer.

buavts:
The information I am after is the actual lines of code I would have to add because as I mentioned earlier, I am NOT the best programmer.

Well, you are already getting the X and Y values, as in your initial post. Save the values and do what you want with them. You haven't said what you actually want to do with the values. Being a good programmer has nothing to do with that.
Paul

Like I said in my first post I want to move a servo, but I can't because when I tilt the module I can see in the serial monitor that the values change from 0 to 1 for like a second then go back to 0. And how would I save the values?

Lets start over.

Can you program to move the servo right now? If not concentrate on that.

Paul

Yes I can program to move servos. But not with this code.

#include <Wire.h>
#include <GY6050.h>


GY6050 gyro(0x68);                            // Create Gyro object with "0x68" as I2C addres (most likely the address of your MPU6050)


void setup()                                  // Main setup
{
  gyro.initialisation();
  Serial.begin(9600);
  Serial.println("MPU6050 Test");
  delay(2500);
}
void loop()                                  // Main loop
{
 // Serial.print("AcX = ");
  //erial.print(gyro.refresh('A', 'X'));           // Ask for the X axis of the Accelerometer and print it
  //Serial.print(" | AcY = ");
  //Serial.print(gyro.refresh('A', 'Y'));           // Ask for the Y axis of the Accelerometer and print it
  //Serial.print(" | AcZ = ");
  //Serial.print(gyro.refresh('A', 'Z'));           // Ask for the Z axis of the Accelerometer and print it
  //Serial.print(" | Tmp = ");
  //Serial.print(gyro.refresh('T', 'C'));           // Ask for the Temperature in Celsius and print it
  //Serial.print(" C | ");
  //Serial.print(gyro.refresh('T', 'F'));           // Ask for the Temperature in Farenheit and print it
  //Serial.print(" F");
  Serial.println(" | GyX = ");
  Serial.print(gyro.refresh('G', 'X'));           // Ask for the X axis of the Gyroscope and print it
  Serial.print(" | GyY = ");
  Serial.print(gyro.refresh('G', 'Y'));           // Ask for the Y axis of the Gyroscope and print it
  //Serial.print(" | GyZ = ");
  //Serial.println(gyro.refresh('G', 'Z'));         // Ask for the Z axis of the Gyroscope and print it, then carriage return
  delay(50);
}

What have you tried and what are the results?

Paul

I have tried every other example sketch online but none of them work except this one. I actually thought I was done with this project but then I realized I need the gyro not the accelerometer. I tried to fix it but know I screwed it up and it’s not working. So I moved on to another sketch and I am trying to use the gyro of this one.

This is the sketch I already tried (kind of succeeded).

#include <GY6050.h>

//hammadiqbal12@gmail.com
//https://www.youtube.com/watch?v=Cvtr3LKdqvk


#include <GY6050.h>           //library for GYRO 
#include <Wire.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo


int X = 0;
int Y = 0;
int Z = 0;
GY6050 gyro(0x68);              //to save GYRO data


void setup() {

  Wire.begin();            //initializing GYRO
  gyro.initialisation();
  delay(100);
  myservo.attach(9);

}

void loop() {

  X = map(gyro.refresh('A', 'X'), -90, 90, 0, 180);                //mapping the gyro data according to angle limitation of servo motor
  Y = map(gyro.refresh('A', 'Y'), -90, 90, 0, 180);
  Z = map(gyro.refresh('A', 'Z'), -90, 90, 0, 180);
  myservo.write(Y);                                          //movement of Y axis will control servo
  delay(15);

}

The gyro is called a rate gyro and only gives you the rate of precession of the gyro. So no good for the move servo thing.

BUT

There are formulas, do your own internet search thingy, on how to get an angle of tilt from the accelerometers!

BUT

After you get those values, you’ll find that they are full of noise and so you’ll ask about filtering the noise so you’ll be told about filters like Kalman, I use the simple Kaman filter GitHub - denyssene/SimpleKalmanFilter: A basic implementation of Kalman Filter for single variable models. but you should be using something a bit beefer then a Uno for those kinds of calculating thingies, cause then you’ll head over to the Mahony/Madgwick thngy, lots of FPU math there.

Want to know more, GitHub - jremington/MPU-9250-AHRS: Arduino Mahony AHRS, includes magnetometer and accelerometer calibration code <<< clicky, clicky.

BUT

There is the low pass Complementary filter which works great to keep a platform level, I used one for a while. Oh You can use the words “arduino low pass Complementary filter” to get the skinny on that filter.


Servo can torque in degree or mS. You’ll want to change to torqueing your servos to milliSeconds. Use metal geared servos, not plastic. Now some numbers to keep in mind. 1 degree of servo torque is 11.11mS. With that number you can torque partial degrees. I find the LeWonSoul servos to torque down to .7ish of a degree; with an Uno/Mega/Due.

If you use an ESP32 and the LED API you can get even more refined servo torque values but the hobby servos are not sensitive enough to respond to such low torque values; a 1/3 of a mS torque value.

Anyways, you get the MPU angle, convert it to a counter torque value and send it to the servo.

I have a hexapod, that uses a LIDAR on a X/Y stable platform, to scan my residence, as it roams around the house. The MPU and the servos keep the LIDAR platform level on X and Y.


Anyways hope you got that there are a number of keywords that you can use to do a bit of research in my post. Oh, Spelling may be off on some things.

Thanks IdahoWalker but I am not planning on buying new servos, LOTS of research (because for the past 2 weeks I've already been doing research), and more complex code. I simply can't spend my time on that because it would take forever. But is there a more simple alternative?

I take it you did not look into any of the hints I gave in my OP.

Try this, search on the words "arduino mpu angle from accelerometer". Go on try it.

first of all I read your entire answer.

Ok I searched it up now what.

But I have to say something. Since this is going on a foam glider the accelerometer measurements will be messed up because the glider is already accelerating. It would be great if I knew how to move a servo with the raw values from the gyro.

@buavts, please do not cross-post. Threads merged.