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

 

 

Circuit Diagram

L293d-mpu6050-arduino-circuit-diagram

Video Demo

Arduino code

#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);
}
21 Comments
  1. Robert 6 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 6 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 6 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 6 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 6 years ago

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

  6. manju 6 years ago

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

  7. manju 6 years ago

    how to switch to PID?

  8. Gerard O Brien 6 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 6 years ago

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

  10. Mark 5 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 5 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

    • ryan 5 years ago

      I am currently trying to do that with an 18v drill and power source, look at the MD10C as you won’t get away with using an l293D

  12. oseas verona 5 years ago

    librerias them where did you get it #include
    #include

  13. JIN 5 years ago

    how about ax change to gx, does it changing code?

  14. ankit neupane 4 years ago

    what is programming for 4 axis

  15. rushikesh badgujar 3 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”?

  16. Umer Farooq 3 years ago

    Any specifications for DC motor?

  17. seb 11 months 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
    😂😅😅😅

  18. Jp 8 months 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

  19. Jp 8 months 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

  20. Nitheesh Murikipudi 8 months 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