#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
#include <Wire.h>
#include <Servo.h>
Adafruit_MPU6050 mpu;//Data types
Servo X_Axis; Servo Y_Axis;
void setup()
{
Serial.begin(115200);//bit/s
mpu.begin();
Wire.begin();
X_Axis.attach(6);//connected motor1 (X-Axis) to pin 6.
Y_Axis.attach(5);//connected motor2 (Y-Axis) to pin 5.
X_Axis.write(0);
Y_Axis.write(0);
}
/*
COMPONENTS:
1-Arduino uno, 1- mpu6050, 2-Servos (X&Y)
CONNECTIONS:
mpu6050 -> Arduino Uno
SDA=I2C data pin -> A4
SCL=I2C clock pin -> A5
GND -> GND.2
VCC -> 5V (3-5)
Servo -> Arduino Uno
GND -> GND.3
PWM -> 5,6
v+(5V) -> 5V (3-5)
*/
void loop()
{
sensors_event_t temp, acc, gyro;
//user-defined data-type
mpu.getEvent(&acc, &gyro, &temp);
//gets value of events periodically
int X_Axis_pos, Y_Axis_pos;
//To convert Degree into rad.
X_Axis_pos = gyro.gyro.x * (180/3.14);
Y_Axis_pos = gyro.gyro.y * (180/3.14);
//Updating servo values
X_Axis.write(X_Axis_pos);
Y_Axis.write(Y_Axis_pos);
//Delay between each reading.
delay(50);
}