In this article you will get the code and circuit diagram to control the DC Motor CW/CCW using GY-521 gyroscope and accelerometer module (MPU-6050).

To make this prototype I am using:

  1. Arduino UNO
  2. GY-521 (MPU-6050)
  3. L29dD Driver IC
  4. DC Motor
  5. Breadboard
  6. Jumper wire


Here is the demo video:

Subscribe to our channel to get cool projects!

 

Circuit Diagram

L293d-mpu6050-arduino-circuit-diagram


Arduino Code

[code lang=”js”]#include <Wire.h>
#include<I2Cdev.h>
#include<MPU6050.h>

MPU6050 mpu;

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

#define pin1 3
#define pin2 5

void setup(){
Serial.begin(9600);
Serial.println("Initialize MPU");
mpu.initialize();
//Serial.println(mpu.testConnection() ? "Connected" : "Connection failed"); pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);

}

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

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

//Serial.println(ax);
if(ax > 0){
if(ax<255){
Serial.println(ax);
analogWrite(pin2,ax);
}
else{
Serial.println("+255");
analogWrite(pin2,255);
}

}
if(ax<0){
if(ax>-255){
Serial.println(ax);
analogWrite(pin1, ax-ax-ax);
}
else{
Serial.println("-255");
analogWrite(pin1, 255);
}
}
delay(1000);
}[/code]

18 Comments
  1. Robert 9 years ago

    Good article – helps me to understand better how MPU6050 is working!
    It will be also possible to switch from map to PID.

  2. reynaldo 9 years ago

    hello, i have a problem whith a library map430_i2c.c
    ->
    msp430_i2c.c:14:17: error: i2c.h: No such file or directory
    msp430_i2c.c: In function ‘I2C_masterInit’:

    How do you gave a solution?

  3. Bennett 9 years ago

    Here is the error that I am getting : ‘MPU6050’ does not name a type. I do not know where to find the “.h” file.

    thanks

  4. epy 8 years ago

    hello,
    i have some problem with this code…
    why i can’t compile/verify this code…
    he said MPU6050 mpu; does not name a type

  5. Emre TAKIL 8 years ago

    Thank you.
    Teşekkür ettim dostum, kısa, öz anlaşılır olmuş.

  6. manju 8 years ago

    very useful and simple thanks a lot for map function.
    please guide for self balancing robot too

  7. manju 8 years ago

    how to switch to PID?

  8. Gerard O Brien 8 years ago

    hello awesome tutorial! I am currently working on an activity tracker with the MPU-650 sensor. Im interested in the map function used in the above code. Could you please email me on the code for your build.
    Thanks
    Gerard

  9. Alme 8 years ago

    i prejudice. my problem, does not see the file i2cdev. how do I install?
    thanks
    Alme

  10. Mark 8 years ago

    I am also having trouble finding the referenced / included .h files. I believe they are needed for the code to work.
    Has anyone found them?
    If so, please let me know where I can get them.
    Thanks all.

  11. Akash Mahla 8 years ago

    hmm….ohk so it was easy, But can we control a high power (24 volts 4.4 am) bldc 3 phase motor with hall sensor with a Gyroscope (MPU6050)?? if so how?? can u please make a stepwise demonstration on that

  12. oseas verona 8 years ago

    librerias them where did you get it #include
    #include

  13. rushikesh badgujar 6 years ago

    Hey Buddy, thanks for tutorial. I have few questions regarding MPU6050.
    Well in this piece of code
    if(ax-255){
    Serial.println(ax);
    analogWrite(pin1, ax-ax-ax);
    }
    can you explain about ax-ax-ax or ax+ax+ax in further code. I want to understand exactly how is this part interacting with the motor.
    At the beginning i thought it would have been “analogWrite(pin2, ax-ax-ax)

    can you please explain how and when are these used? “ax-ax-ax”/”ax+ax+ax”?

  14. Umer Farooq 6 years ago

    Any specifications for DC motor?

  15. seb 3 years ago

    Hi how can I adapt this code because I want to use this but with a motor shield 293d. sorry but I’m a beginner
    😂😅😅😅

  16. Jp 3 years ago

    can a accelerometer possibly identify if it is moving towards right or towards left? if possible, can I use the accelerometer to variate the speed of a dc motor using it? thank you very much <3

  17. Jp 3 years ago

    can a accelerometer possibly identify if it is moving towards right or towards left? if possible, can I use the accelerometer to variate the speed of a dc motor using it? thank you very much <3

  18. Nitheesh Murikipudi 3 years ago

    Error i2cdev.h

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