Hi! I am a total noob with Arduino, and am trying to control 4 small servo motors based off data from an MPU6050 accelerometer. My goal is this: based off the angle of the accelerometer, rotate the servo to that angle. It works very well running only one arduino, but running 4 the lag is incredible, upwards of 10 seconds in some cases. I'm not sure if this is an issue with my code, with my circuitry, or with my power supply. My circuitry setup for the MPU6050 is: VCC -> 5v, GND -> GND, SCL -> A5, SDA -> A4. Then, for the servos, I run a wire from Vin to one part of a breadboard, and a wire from Gnd to another part of the breadboard. The servo motor's grounds and power supply wires are connected in series with the breadboard. Then, the servo motor's input wire is connected to the respective pin (I am using pins 3, 5, 6, and 9). I am also using my computer as a power supply, maybe this is an issue?
Below is the code I am using, thank you for any help! Let me know if my description of the setup / code is unclear, or if a picture / video would help
/*MPU6050 sensor with servo control.
https://www.mpusensor.com
*/
// This next portion includes neccesary libraries, namely the Adafruit MPU6050 Accelerometer library, and servo libraries.
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Servo.h>
// Objects are created for the libraries
Servo servox1;
Servo servox2;
Servo servoy1;
Servo servoy2;
Adafruit_MPU6050 mpusensor;
// Setup Function
void setup(void) {
Serial.begin(115200); // Serial Monitor begins - the number here is the BAUD rate, which is essentially how fast the data is to be sent or recieved. If the serial monitor is displaying weird value, make sure the baud value matches what is listed here.
servox1.attach(3); // Sets the servo motor PIN number on ARDUINO - pin number should correspond to a DIGITAL pin, but does not have to be a dedicated hardware PWM pin
servox2.attach(5); // Sets the servo motor PIN number on ARDUINO - pin number should correspond to a DIGITAL pin, but does not have to be a dedicated hardware PWM pin
servoy1.attach(6); // Sets the servo motor PIN number on ARDUINO - pin number should correspond to a DIGITAL pin, but does not have to be a dedicated hardware PWM pin
servoy2.attach(9); // Sets the servo motor PIN number on ARDUINO - pin number should correspond to a DIGITAL pin, but does not have to be a dedicated hardware PWM pin
Wire.begin(); // Begin wire library
mpusensor.begin(); // Begin MPU6050 Sensor
servox1.write(0); // Turn the servo motor to the starting position
servox2.write(0); // Turn the servo motor to the starting position
servoy1.write(0); // Turn the servo motor to the starting position
servoy2.write(0); // Turn the servo motor to the starting position
// The next 3 lines of code set the accelerometer and gyroscope ranges
mpusensor.setAccelerometerRange(MPU6050_RANGE_8_G);//2_G,4_G,8_G,16_G
mpusensor.setGyroRange(MPU6050_RANGE_500_DEG);//250,500,1000,2000
mpusensor.setFilterBandwidth(MPU6050_BAND_21_HZ);
}
// Loop Function
void loop() {
// Get new sensor data from MPU6050
sensors_event_t a, g, temp;
mpusensor.getEvent(&a, &g, &temp);
int y_value = a.acceleration.y; // Accelerator Y-axis values are stored in variable "y_value"
int x_value = a.acceleration.x; // Accelerator X-axis values are stored in variable "x_value"
y_value = map(y_value, -10, 10, 180, 0); // y_value from accelerometer converted to degrees ranging from 0 to 180.
int opp_y_val = abs(180-y_value); // finds the opposite value of the corresponding y value
x_value = map(x_value, -10, 10, 180, 0); // x_value from accelerometer converted to degrees ranging from 0 to 180.
int opp_x_val = abs(180-x_value); // finds the opposite value of the corresponding x value
servoy1.write(opp_y_val); // servo motor is rotated to the opposite of the mapped y_value
servoy2.write(opp_y_val); // servo motor is rotated to the opposite of the mapped y_value
servox1.write(opp_x_val); // servo motor is rotated to the opposite of the mapped x_value
servox2.write(opp_x_val); // servo motor is rotated to the opposite of the mapped x_value
Serial.println(opp_y_val); // opp_y_val is displayed on the serial monitor
Serial.println(opp_x_val); // opp_x_val is displayed on the serial monitor
//delay(10); // optional delay
}