DC Motor Control using MPU6050 Gyro/Accelerometer Sensor & Arduino

DC Motor Control using MPU6050 Gyro/Accelerometer (Last Updated On: December 1, 2018)

Introduction:

In this project, we are going to learn about DC Motor Control using MPU6050 Gyro/Accelerometer Sensor & Arduino. MPU6050 is an Accelerometer & Gyro sensor combined in a single chip. By moving the sensor in upward or downward direction, the speed of the motors will increase or decrease. We will simply be interfacing MPU6050 or GY21 sensor with Arduino for controlling 2 motors & displaying the speed on LCD.



By moving the sensor in upward direction, the speed of first motor will increase and the speed of other motor will decrease. Similarly, by moving the sensor in downward direction, the speed of first motor will increase and the speed of other motor will decrease. We will also be displaying the speed of the motor on 16*2 LCD.


Hardware Requirements:

1. Arduino Uno
2. MPU6050 (Accelerometer and Gyro sensor) - (Buy Online from Amazon)
3. L293D Motor Driver - (Buy Online from Amazon)
4. 5V DC Motors - (Buy Online from Amazon)
5. 16*2 LCD
6. Connecting wires
7. Breadboard

MPU6050 Gyro/Accelerometer Sensor:

Introduction:

DC Motor Speed Control using MPU6050 Gyroscope Sensor & Arduino

The InvenSense MPU-6050 sensor contains a MEMS accelerometer and a MEMS gyro in a single chip. It is very accurate, as it contains 16-bits analog to digital conversion hardware for each channel. Therefor it captures the x, y, and z channel at the same time. The sensor uses the I2C-bus to interface with the Arduino.

The MPU-6050 is not expensive, especially given the fact that it combines both an accelerometer and a gyro.

MPU6050 Pinout:

The MPU-6050 module has 8 pins,
INT: Interrupt digital output pin.
AD0: I2C Slave Address LSB pin. This is 0th bit in 7-bit slave address of device. If connected to VCC then it is read as logic one and slave address changes.
XCL: Auxiliary Serial Clock pin. This pin is used to connect other I2C interface enabled sensors SCL pin to MPU-6050.
XDA: Auxiliary Serial Data pin. This pin is used to connect other I2C interface enabled sensors SDA pin to MPU-6050.
SCL: Serial Clock pin. Connect this pin to microcontrollers SCL pin.
SDA: Serial Data pin. Connect this pin to microcontrollers SDA pin.
GND: Ground pin. Connect this pin to ground connection.
VCC: Power supply pin. Connect this pin to +5V DC supply.

3-Axis Gyroscope:

The MPU6050 consist of 3-axis Gyroscope with Micro Electro Mechanical System(MEMS) technology. It is used to detect rotational velocity along the X, Y, Z axes as shown in below figure.
DC Motor Control using MPU6050 Gyro/Accelerometer

3-Axis Accelerometer:

The MPU6050 consist 3-axis Accelerometer with Micro Electro Mechanical (MEMs) technology. It used to detect angle of tilt or inclination along the X, Y and Z axes as shown in below figure.
DC Motor Control using MPU6050 Gyro/Accelerometer


DC Motor Control using MPU6050 Gyro/Accelerometer Sensor & Arduino:

Circuit Diagram:

DC Motor Control using MPU6050 Gyro/Accelerometer

Connections:

1. Connection Between Arduino, Motor & L293D
– Pin No. 1, 8, 9, 16 of L293D IC is connected to 5V
– Pin No’s 4, 5, 12 and 13 are the ground pins; connect these to GND (Arduino GND pin).
– Pin No. 2 of L293D IC is connected to Pin No. D2 of Arduino.
– Pin No. 7 of L293D IC is connected to Pin No. D3 of Arduino.
– Pin No. 10 of L293D IC is connected to Pin No. D4 of Arduino.
– Pin No. 15 of L293D IC is connected to Pin No. D5 of Arduino.
– Pin No. 3, 5 of L293D IC is connected to 2 terminals of motor M1.
– Pin No. 11, 14 of L293D IC is connected to 2 terminals of motor M2.

2. Connection Between Arduino & MPU6050
– Connect VCC pin of MPU6050 to the 5V pin of Arduino
– Connect GND pin of MPU6050 to the GND of Arduino
– Connect SCL pin of MPU6050 to the A5 of Arduino
– Connect SDA pin of MPU6050 to the A4 of Arduino

2. Connection Between Arduino & LCD
– Connect 1, 3, 5, 16 pin of LCD to the GND pin of Arduino
– Connect 2, 15 pin of LCD to the 5V pin of Arduino
– Connect Pin No. 4 of LCD to the D13 of Arduino
– Connect Pin No. 6 of LCD to the D12 of Arduino
– Connect Pin No. 11 of LCD to the D11 of Arduino
– Connect Pin No. 12 of LCD to the D10 of Arduino
– Connect Pin No. 13 of LCD to the D9 of Arduino
– Connect Pin No. 14 of LCD to the D8 of Arduino


Program & Source Code:

First download the header files library from below and add it to the Arduino Library:
1. MPU6050 Library
2. I2Cdev Library

#include <Wire.h>
#include <MPU6050.h>
#include<LiquidCrystal.h> // lcd Header
LiquidCrystal lcd(13,12,11,10,9,8); // pins for LCD Connection

#define motor1_pin1 2
#define motor1_pin2 3
#define motor2_pin1 4
#define motor2_pin2 5

MPU6050 gy_521;

int16_t ax, ay, az;
int16_t gx, gy, gz;

int motor1_speed;
int motor2_speed;

void setup ( )
{
Wire.begin( );

lcd.begin(16,2); //initializing lcd
Serial.begin(9600); // initializing serial
lcd.print("Initializing.....");
delay(2000);
lcd.clear();

Serial.println ("Initializing MPU and testing connections");
gy_521.initialize ( );
Serial.println(gy_521.testConnection( ) ? "Successfully Connected" : "Connection failed");
lcd.print(gy_521.testConnection( ) ? "Connected.............." : "No Connection....");
delay(2000);
lcd.clear();
Serial.print("Reading Values");
lcd.print("Reading Values...");
delay(2000);
lcd.clear();
}

void loop ( )
{
gy_521.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
ax = map(ax, -17000, 17000, -125, 125);

motor1_speed = 125+ax; //To move first motor
motor2_speed = 125-ax; //To move second motor

Serial.print ("Motor1 Speed = ");
lcd.setCursor(0,0);
lcd.print ("MOT1 Speed = ");
Serial.print (motor1_speed, DEC);
lcd.print (motor1_speed, DEC);

Serial.print ("Motor2 Speed = ");
lcd.setCursor(0,1);
lcd.print ("MOT2 Speed = ");
Serial.println (motor2_speed, DEC);
lcd.print (motor2_speed, DEC);
delay(300);
analogWrite (motor1_pin2, motor1_speed);
analogWrite (motor2_pin2, motor2_speed);
delay (300);
}

Working Explanation:

In this Project DC Motor Control using MPU6050 Gyro/Accelerometer Sensor & Arduino first we need to understand the operation of MPU6050 & then the working of DC Motor Control using MPU6050 Gyro/Accelerometer

Operation of MPU6050
The structure of the accelerometer and gyro sensor has a mass attached to a spring which has fixed outer plates and moves along one direction. If an acceleration is applied in any of the direction, the capacitance between the plates and the mass will change. The accelerometer sensor will measure this change in capacitance which corresponds to an acceleration value.

DC Motor Control using MPU6050 Gyro/Accelerometer

Working of DC Motor Control using MPU6050 Gyro/Accelerometer
Whenever we move the sensor in the upward or downward direction, the sensor gives the output from -17000 to 17000. The motors only require 0 to 255 pwm value to rotate. So, we will map these values from -125 to 125. Now, when we move the MPU6050 towards upward direction, the output value which we will get is 125. Then will add 125 to this output value and this will be the speed of the first motor.

When we move the MPU 6050 towards downward direction, the output value which we will get is -125. We will subtract this from 125 and this will be speed for the second motor.




Video Preview: Working & Explanation

Leave a Reply

Your email address will not be published. Required fields are marked *