mpu6050 and dc motor and driver shield arduino

Here is, I am sharing the Arduino code for 2 diffrent DC motor control with MPU-6050 and Arduino UNO with adafruid motor driver shield.

I am using:

  • Two DC motors
  • L329 Motor Driver Shield
  • Arduino UNO
  • Gy-521 (MPU-6050)


Video Demo MPU 6050 with Motors

Arduino Code MPU 6050

#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>

#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ);

MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;

int valMotor1;
int valMotor2;
void setup()
Serial.println("Initialize MPU");
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");

motor1.setSpeed(100); // set the speed to 200/255

void loop()
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

ax = map(ax, -17000, 17000, -125, 125);

valMotor1 = 125+ax;
valMotor2 = 125-ax;

Serial.println(valMotor1, DEC);
Serial.println(valMotor2, DEC);

if(valMotor1 > 0 && valMotor1 < 255){
if(valMotor2 > 0 && valMotor2 < 255){

motor1.run(FORWARD); // turn it on going forward


  1. kisbitang 8 years ago

    Dear Circuit Magic,
    please write to me how(or where can i find) can i get these libraries to i can use this code? Links are enough to me!
    Your code is very helpful, cos i’m rookie. Thanks the answer!

    See ya!

    • Raimundo 7 years ago

      Tem como associar isso aos motores de drone como os motores brushless rctimer

  2. Tyrone Robinson 8 years ago

    Thanks for sharing you work. I am learning how to use the MPU 6050 myselfe and your codes are wonderful.
    My question is, Are you using the DMP to get both the combined values to controll the motors?
    And can you send me the “#include ” file via email? Have you tried making a balance robot?
    Thank you.

  3. Tyrone Robinson 8 years ago

    AFMotor.h file can you Email it to me please.

  4. Raimundo 7 years ago

    How did you put the GY-521 GYRO / ACCELEROMETER MODULE (MPU-6050) AND DRIVER L329D ARDUINO SHIELD you have the design scheme for your site appears only the code

  5. berkan 7 years ago

    can you combine that with 4 motors ?

  6. James Wakwaw 7 years ago

    hey dude, can you share the schematic for connection MPU6050? please update this thread.. thanks

  7. Arkar 7 years ago

    how to read the sensor? I can’t find the code to read pin A4 and A5 connected with MPU 6050. Pls, explain your code to me, fri. Give me the light for my stupid brain…. 🙁

  8. Alysson 6 years ago

    Hi, i´m make the self balanced robot for the college project and i´m using your article as guide line, can you send me the circuit diagram for two dc motors and the adafruit shield..i already test the another one and work fine.

  9. Siti Masitoh 5 years ago

    can you send schematic about this to me please

  10. Manish 5 years ago

    i need schematics
    pleas send in my email

  11. jfmechatronics 4 years ago

    Impressive project man.
    Excuse me, what is the Diagram Circuit?

  12. Fredy Cortés 4 years ago

    Amigo no podrias ayudarme con el codigo para lenguaje ada

  13. Aziz 3 years ago

    How does this “ax = map(ax, -17000, 17000, -125, 125);” map works?

  14. skg 2 years ago

    I do not understand why some people say something in the details and don’t do it. Here you have not shown the schematic drawing, for which everybody is asking for!

Leave a reply

Your email address will not be published. Required fields are marked *



Log in with your credentials


Forgot your details?


Create Account