Hello,
I'm attempting to add angle limits to my MPU6050/ servo combination. Since the servo arm will be attached to a control rod I can't have it do a 360's randomly as it does currently.
I have no idea how to implement this and can't seem to find anything online referring to how to do this.
Schematic
Existing code
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Servo.h>
Adafruit_MPU6050 mpu;
int x = 0;
int y = 0;
int z = 0;
Servo servo1;
int value = 0;
void setup(void) {
Serial.begin(115200);
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
mpu.enableSleep(0);
// set accelerometer range to +-8G
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
// set gyro range to +- 500 deg/s
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
// set filter bandwidth to 21 Hz
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
delay(100);
servo1.attach(9);
servo1.write(0);
}
void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
x = a.acceleration.x;
y = a.acceleration.y;
z = a.acceleration.z;
Serial.println(y);
value=map(y,-9,9,0,180);
servo1.write(value);
value=0;
}
Any help is appreciated Thank you
Please use code tags when posting code.
If you want to measure tilt angle using the accelerometer, follow this tutorial. What you are currently doing does not make sense.
https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
Use a 4xAA, 6V battery pack to power the servo.
Is that a 9v servo?
How are you powering the Arduino?
Hello,
Maybe my initial post was a little confusing. I do not wish to measure the tilt angle, I want the servo to adhere to a strict angle range and not move the servo arm beyond those limits. Thanks again for the help.
Your program commands the servo to certain angles. So control those commands.
What does the accelerometer do?
Currently the Arduino is being powered by my computer via a USB cable.
The Servo's max voltage is 8.4V. It's a high torque servo
elliottwilliams10:
It's a high torque servo
A 9V PP3 battery certainly won't work.
Please explain what you are really trying to do.
The accelerometer was included in the circuit to measure the angle or deviance from the device's neutral position
Good luck with your project!
The 9V battery does work with the servo, I tested it. Or do you think the high voltage is a cause of the servo's occasional sporadic movement?
I am attempting to use this system to keep a somewhat large RC submarine level in the water.
b707
August 16, 2022, 3:42pm
11
yes, it will work, but not for long
For that, it works very well to to measure the tilt angle from vertical, using an accelerometer.
I see. with this said I have already committed to the MPU6050. Is there a way to proceed with this. The article you referenced doesn't appear to apply to my desired outcome.
Best,
The MPU6050 will work fine.
Yes, the article does apply to your situation. "Horizontal" is defined with respect to the vertical axis.
The tilt from horizontal is equal to the tilt from vertical.