MPU6050 Controlled Servo Arm

12,235

31

11

Introduction: MPU6050 Controlled Servo Arm

So, in this instructable i present the process to make a 2-axis pan and tilt servo arm controlled using the MPU6050
accelerometer.

First of all we need to Upload the files from i2cdev.zip and MPU6050.zip into the Arduino IDE library.
This can be done by extracting the files within the .zip folders and copying into the library folder of the arduino IDE.

The parts needed for this project are:
1) Arduino UNO
2) Breadboard
3) MPU6050
4) 2 x Servo motors ( i used microservo motors in this case)
5) Male-Male Jumper cables
6) Computer with Arduino IDE

The connections should be made as shown in the Fritzing diagram.

For MPU6050:
SCL -> A5
SDA -> A4
INT -> 2

Servo1 -> 9
Servo2 ->10

After all the connections are made, upload the code onto your Arduino:

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

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





Important note: There might be issues connecting the MPU6050, as the wires keeping running lose, so when you initially upload it, check in the serial moniter if the accelerometer was successfully connected. Else reconnect and try again.




Tips: You can add more servos by introducing new variables and mapping accordingly. Adding more powerful servos and a better structure can open new possibilities like a fpv camera mount etc

.

Always ready for feedback.
Thank you.

Be the First to Share

    Recommendations

    • Toys & Games Contest

      Toys & Games Contest
    • Big vs Small Challenge

      Big vs Small Challenge
    • Fix It Challenge

      Fix It Challenge

    11 Comments

    0
    detlev_barthel
    detlev_barthel

    Question 2 years ago on Introduction

    Hi, I can move the servos with the sensor, but the serial monitor produces nonsense. I get the following error messages:

    Found programmer: Id = "à’b’㌃"; type = ã
    Software Version = ã.“; Hardware Version = b.’
    avrdude: error: buffered memory access not supported. Maybe it isn't
    a butterfly/AVR109 but a AVR910 device?
    avrdude: initialization failed, rc=-1
    Double check connections and try again, or use -F to override
    this check.

    avrdude: error: programmer did not respond to command: leave prog mode
    avrdude: error: programmer did not respond to command: exit bootloader
    avrdude: error: programmer did not respond to command: exit bootloader

    Do you have an idea, what is going wrong. Any help is appreciatede. Thanks a lot, Detlev

    0
    detlev_barthel
    detlev_barthel

    Question 2 years ago on Introduction

    Hi, many thanks for the very good tutorial. I am definitely no pro and I get the following error message:

    Found programmer: Id = "à“c“⌂"; type = â
    Software Version = â.“; Hardware Version = c.“
    avrdude: error: buffered memory access not supported. Maybe it isn't
    a butterfly/AVR109 but a AVR910 device?
    avrdude: initialization failed, rc=-1
    Double check connections and try again, or use -F to override
    this check.

    Do you have an idea, what is going wrong? I appreciate your help.

    The servo is reacting very quickly to the tilting of the sensor, but the text output is screwed up. Thanks in advance and best regards, Detlev

    0
    IsaacE15
    IsaacE15

    Question 3 years ago

    Hey. Nice program. If I were to add another servo, making it 3 servo one mpu6050, how big of a difference does it effect the program?

    0
    alexpou
    alexpou

    3 years ago

    Hello, where did you find the values -17000, 17000, 0, 179 ? Are they your offsets?

    0
    Idrispo
    Idrispo

    4 years ago

    It is not so smooth. How can we make it smoother?

    0
    Michael Chan
    Michael Chan

    4 years ago

    Thanks for sharing!

    From the line "map(ax, -17000, 17000, 0, 179);", how come the acceleration values are assumed to be within -17000 and 17000? Aren't they 16-bit values? If so, shouldn't the range be wider than -17000, 17000?

    0
    DswW
    DswW

    4 years ago

    Hello, Can i see how is your servo motor connected with each other in detail?

    And can i know the exact model of the servo motor that you are using pls?

    tq for sharing

    0
    Soul_seeker
    Soul_seeker

    Reply 4 years ago

    Unfortunately i disassembled the setup, but its a Towerpro 90g motor found here:

    http://www.xcluma.com/servo-SG90

    As for servo connection its basically arm of one servo is glued ontho the body of other servo, in order to use two axis.

    0
    gada888
    gada888

    4 years ago

    Thanks for sharing.

    0
    Soul_seeker
    Soul_seeker

    Reply 4 years ago

    Anyday brother ! :D