Stabilizer spoon using MPU6050, UNO, two servos

I am making a stabilizer spoon project in which I want to control two servos one for X axis motion, one for Y axis motion, by using MPU 6050 sensor.
I got a code (below ) from website. I included necessary libraries and upload, no error came. No the Motor with X axis control is operating smoothly but the one with Y axis control is showing tremors and shaky movements even when no change in sensor it shakes by its own.
I replaced the motor .
I swiped the pins also but problem is still there.
what should I do?

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

Servo servoX; // Servo motor for X-axis stabilization
Servo servoY; // Servo motor for Y-axis stabilization

int MPU6050_address = 0x68; // I2C address of MPU6050 sensor
int16_t accelerometerX, accelerometerY, accelerometerZ;
int16_t gyroscopeX, gyroscopeY, gyroscopeZ;
float roll = 0, pitch = 0;
float rollOffset = 0, pitchOffset = 0; // Calibration offsets for roll and pitch
int servoXCenter = 90; // Center position for X-axis servo
int servoYCenter = 90; // Center position for Y-axis servo

void setup() {
  Wire.begin();
  Wire.beginTransmission(MPU6050_address);
  Wire.write(0x6B); // Power register
  Wire.write(0); // Wake up MPU6050
  Wire.endTransmission(true);
  servoX.attach(5); // Attach X-axis servo to pin 9
  servoY.attach(6); // Attach Y-axis servo to pin 10
}

void loop() {
  readMPU6050(); // Read sensor data
  stabilizeSpoon(); // Stabilize spoon
}

void readMPU6050() {
  Wire.beginTransmission(MPU6050_address);
  Wire.write(0x3B); // Starting register for accelerometer data
  Wire.endTransmission(false);
  Wire.requestFrom(MPU6050_address, 14, true); // Read 14 bytes of data
  accelerometerX = Wire.read() << 8 | Wire.read();
  accelerometerY = Wire.read() << 8 | Wire.read();
  accelerometerZ = Wire.read() << 8 | Wire.read();
  gyroscopeX = Wire.read() << 8 | Wire.read();
  gyroscopeY = Wire.read() << 8 | Wire.read();
  gyroscopeZ = Wire.read() << 8 | Wire.read();
  roll = atan2(accelerometerY, accelerometerZ) * 180 / PI; // Calculate roll angle
  pitch = atan2(-accelerometerX, sqrt(accelerometerY * accelerometerY + accelerometerZ * accelerometerZ)) * 180 / PI; // Calculate pitch angle
}

void stabilizeSpoon() {
  int servoXPosition = servoXCenter + (roll - rollOffset); // Calculate servo position for X-axis
  int servoYPosition = servoYCenter + (pitch - pitchOffset); // Calculate servo position for Y-axis
  servoX.write(constrain(servoXPosition, 0, 180)); // Write servo position for X-axis with bounds
  servoY.write(constrain(servoYPosition, 0, 90)); // Write servo position for Y-axis with bounds
}

Schematics please. A powering issue is suspected.
Link to the datasheet of the servos, power supply, please. Please avoid sales sights only telling the price.

1 Like

With servos, that is almost always a sign of an inadequate power supply.

The Arduino 5V output cannot be used for motors or servos, so use a separate power supply capable of 1 Ampere per servo. Be sure to connect the grounds.

1 Like
#include "Wire.h"
#include <MPU6050_light.h>
#include <Servo.h>

MPU6050 mpu(Wire);
unsigned long timer = 0;
float roll = 0, pitch = 0;
int servoXCenter = 90; // Center position for X-axis servo
int servoYCenter = 90; // Center position for Y-axis servo

Servo servoX; // Servo motor for X-axis stabilization
Servo servoY; // Servo motor for Y-axis stabilization

void setup() {
  Serial.begin(9600);
  Wire.begin();
  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while (status != 0) { } // stop everything if could not connect to MPU6050
  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  mpu.calcOffsets(); // gyro and accelero
  Serial.println("Done!\n");
  
  servoX.attach(5); // Attach X-axis servo to pin 9
  servoY.attach(6); // Attach Y-axis servo to pin 10
}
void loop() {
  mpu.update();
  if ((millis() - timer) > 10) { // print data every 10ms    
    roll = mpu.getAngleX();
    pitch = mpu.getAngleY();
    Serial.print("X : ");
    Serial.print(roll);
    Serial.print("\tY : ");
    Serial.print(pitch);
    Serial.print("\tZ : ");
    Serial.println(mpu.getAngleZ());
    timer = millis();
    stabilizeSpoon();
  }
}

void stabilizeSpoon() {
  int servoXPosition = servoXCenter + roll; // Calculate servo position for X-axis
  int servoYPosition = servoYCenter + pitch; // Calculate servo position for Y-axis
  Serial.print(servoXPosition);
  Serial.print(" , ");
  Serial.println(servoYPosition);
  servoX.write(constrain(servoXPosition, 0, 180)); // Write servo position for X-axis with bounds
  servoY.write(constrain(servoYPosition, 0, 90)); // Write servo position for Y-axis with bounds
}

Use this library

MPU6050 library

Also when i faced the same issue, i changed both the motor to a metallic servo and also changed to this code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.