L293d-mpu6050-arduino-circuit-diagram

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

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);
}

Circuit Diagram

L293d-mpu6050-arduino-circuit-diagram

 
 

24 Comments
  1. Robert 10 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 10 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 10 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 9 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 9 years ago

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

  6. manju 9 years ago

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

  7. manju 9 years ago

    how to switch to PID?

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

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

  10. Mark 9 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 9 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 8 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

    • Radhe banik 10 months ago

      Helo sir, are u solve your problem

  12. oseas verona 9 years ago

    librerias them where did you get it #include
    #include

  13. JIN 8 years ago

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

  14. Sojol 8 years ago

    can i do it without ic? and i want to rotate only in one direction. how can i do that?

  15. Javier Villares 8 years ago

    Hello , first of all , thanks for helping . From Argentina I ask for your help, I need to operate 3 motors of these characteristics taking into account the 3 axes of motion. Again, I appreciate your help. Thank you very much

  16. akshay gobin 8 years ago

    may i please know what program you use to design your circuits?

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

  18. Umer Farooq 7 years ago

    Any specifications for DC motor?

  19. seb 4 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
    😂😅😅😅

  20. Jp 4 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

  21. Jp 4 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

  22. Nitheesh Murikipudi 4 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