Servo Motor controlled by Gyro(MPU 6050) using Arduino - Wired Connection


Circuit Diagram:


Required Components:


Arduino uno/nano - 1 Nos
Battery - 1 Nos
Servo Motor - 1 Nos
Gyro (MPU 6050) - 1 Nos
 

Program Code:

#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(115200);
Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
servo1.attach(5); 
servo2.attach(6);
}

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(5);
}

Video:

Comments

  1. hello, excellent tutorials, i need some help i keep getting this error and i dont know what to do, sorry i'm new on this

    thanks.



    I2Cdev.h: No such file or directory




    ReplyDelete

Post a Comment

Popular posts from this blog

Servo motor controlled by Joystick using Arduino & Transceiver - Wireles...

DC motor controlled by Joystick using Arduino & L298N Motor driver - Wireless Connection

DC motor controlled by Gyro MPU 6050 using Arduino & L298N Motor driver - Wired Connection