DC Motor Control with GY-521 Gyro/Accelerometer Module (MPU-6050) and L329D Arduino Driver Shield

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)

Demo Video

Subscribe to our channel to get cool projects!



Arduino Code:

<pre>#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 5 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 4 years ago

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

  2. Tyrone Robinson 5 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 5 years ago

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

  4. Raimundo 4 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 4 years ago

    can you combine that with 4 motors ?

  6. James Wakwaw 4 years ago

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

  7. Arkar 4 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. Siti Masitoh 2 years ago

    can you send schematic about this to me please

  9. Manish 1 year ago

    i need schematics
    pleas send in my email
    [email protected],com

  10. jfmechatronics 1 year ago

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

  11. Fredy Cortés 11 months ago

    Amigo no podrias ayudarme con el codigo para lenguaje ada

  12. Aziz 5 months ago

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

Leave a reply

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



Log in with your credentials


Forgot your details?


Create Account