MPU 6050 / 9250 - Issue of trying to get z/yaw axis to activate servo motor

Hi new to this forum so I’m hoping I post this question in the right area. Im a beginner but trying to learn.

Currently working on a few projects for a animatronic cosplay. Main issue I’m having is for a shoulder mounted turret tracker.

I’m using a mpu 6050 module, arduino/elegoo uno and x2 cheap servor motors (sg90) So far I succesfully got the turret moving as the mpu moves. But the problem is that the z axis of the module has no effect on movement of the turret.

Bascialy I wasnt the servo motor to move when I move the module in the yaw/z axis as well as the pitch/y axis

I can move front/back (pitch) and angle sideways (roll) but not the yaw.

I was told to get a 9250 as they perform better, so I got one. I was able to use the same script and still have the turret moving but not for the yaw.

Here is the script im using

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Servo.h"

MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
Servo servo1;
Servo servo2;

int val1;
int val2;
int prevVal1;
int prevVal2;

void setup()
{
Wire.begin();
Serial.begin(38400);
Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
servo1.attach(9);
servo2.attach(10);
}

void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
val1 = map(ax, -17000, 17000, 0, 179);
if (val1 != prevVal1)
{
servo1.write(val1);
prevVal1 = val1;
}
val2 = map(ay, -17000, 17000, 0, 179);
if (val2 != prevVal2)
{
servo2.write(val2);
prevVal2 = val2;
}
delay(50);
}

The attachement shows my set up.
+AOD of mpu 6050 is connected to ground on the arduino uno to have it on I2C address 0x69.

Ive seen videos where few people succesfuly had the yaw move whilst using mpu 6050 and can’t seem to recreate it.

Im not sure if I need a completely different script or if I need to change some values of the current script

Let me know if you need more info in what I’m trying to do. A lot of this is new to me but Im trying to learn. I would really appreciate for any help. Thank You


Please edit your post to add code tags, as described in the "How to use this forum" post at the head of every forum topic.

The X axis acceleration "ax" has nothing to do with the yaw angle.

You need a library to estimate the yaw angle (relative to the starting yaw value), using the gyro. Most people use the Rowberg library.

Ok code tag added.

Tahnk you for replying. Right will need to look more into these axis then it seems I confused myself. Ill look into the library than.

Make sure to power the servos separately from the Arduino, and connect the grounds. 4XAA battery packs work fine for one or two small servos.

Although lots of bad tutorials suggest to use the 5V output to power motors or servos, that can actually destroy the Arduino. At the very least it will malfunction.

Ok did not know that so will make those adjustments thank you.

I managed to get some data for the yaw, pitch and roll using 3 different scripts that I tested and using the library you recommended. It seems there is data when the yaw axis moves. So assuming what I did is correct how do I than go about adding the servos into the coding and how do I make the servos move based on pitch and yaw.

Let me know if you want me to place the codes I used.