Control DC Motor CW/CCW with MPU-6050 Gyro/Accelerometer + Arduino

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

Related Post

12 Comments
  1. Robert 3 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 3 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 3 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 3 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 2 years ago

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

  6. manju 2 years ago

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

  7. manju 2 years ago

    how to switch to PID?

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

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

  10. Mark 2 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 2 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 2 years ago

    librerias them where did you get it #include
    #include

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