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:
- Arduino UNO
- GY-521 (MPU-6050)
- L29dD Driver IC
- DC Motor
- Breadboard
- 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);
}
Good article – helps me to understand better how MPU6050 is working!
It will be also possible to switch from map to PID.
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?
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
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
Thank you.
Teşekkür ettim dostum, kısa, öz anlaşılır olmuş.
very useful and simple thanks a lot for map function.
please guide for self balancing robot too
how to switch to PID?
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
i prejudice. my problem, does not see the file i2cdev. how do I install?
thanks
Alme
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.
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
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
Helo sir, are u solve your problem
librerias them where did you get it #include
#include
how about ax change to gx, does it changing code?
can i do it without ic? and i want to rotate only in one direction. how can i do that?
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
may i please know what program you use to design your circuits?
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”?
Any specifications for DC motor?
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
😂😅😅😅
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
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
Error i2cdev.h