You are on page 1of 4

Control DC Motor CW/CCW with MPU-6050

Gyro/Accelerometer + Arduino
Home Arduino Control DC Motor CW/CCW with MPU-6050 Gyro/Accelerometer + Arduino
admin May 4, 2014 Arduino 12
, , ,

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
Arduino Code

01 #include <Wire.h>
#include<I2Cdev.h>
02
#include<MPU6050.h>
03
04 MPU6050 mpu;
05
06 int16_t ax, ay, az;
07 int16_t gx, gy, gz;
08
09 #define pin1 3
#define pin2 5
10
11 void setup(){
12 Serial.begin(9600);
13 Serial.println("Initialize MPU");
14 mpu.initialize();
15 //Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
pinMode(pin1,OUTPUT);
16 pinMode(pin2,OUTPUT);
17
18 }
19
20 void loop(){
21 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
22
ax = map(ax, -17000, 17000, -1500, 1500);
23
24 //Serial.println(ax);
25 if(ax > 0){
26 if(ax<255){
27 Serial.println(ax);
28 analogWrite(pin2,ax);
}
29 else{
30 Serial.println("+255");
31 analogWrite(pin2,255);
32 }
33
34 }
if(ax<0){
35 if(ax>-255){
36 Serial.println(ax);
37 analogWrite(pin1, ax-ax-ax);
38 }
else{
39 Serial.println("-255");
40 analogWrite(pin1, 255);
41 }
42 }
43
44
45
46 delay(1000);
47 }
48
49
50

You might also like