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
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()
{
Wire.begin();
Serial.begin(9600);
Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");

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

}
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){
motor1.setSpeed(valMotor1);
}
if(valMotor2 > 0 && valMotor2 < 255){
motor2.setSpeed(valMotor2);
}

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

delay(200);

}

Related Post

9 Comments
  1. kisbitang 3 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!
    I2Cdev.h
    MPU6050.h
    AFMotor.h
    Your code is very helpful, cos i’m rookie. Thanks the answer!

    See ya!

    • Raimundo 3 years ago

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

  2. Tyrone Robinson 3 years ago

    Hello,
    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 3 years ago

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

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

    hello,
    can you combine that with 4 motors ?

  6. James Wakwaw 3 years ago

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

  7. Arkar 2 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 8 months ago

    can you send schematic about this to me please

Leave a reply

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

*

or

Log in with your credentials

or    

Forgot your details?

or

Create Account