Introduction:
In this project, we are going to learn about DC Motor Control using MPU6050 Gyro/Accelerometer Sensor, L293D Motor Driver IC & Arduino. MPU6050 is an Accelerometer & Gyro sensor combined in a single chip. By moving the sensor in an 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 an upward direction, the speed of the first motor will increase and the speed of another motor will decrease. Similarly, by moving the sensor in a downward direction, the speed of the first motor will increase and the speed of another motor will decrease. We will also be displaying the speed of the motor on the 16*2 LCD.
Before moving ahead, learn about Arduino MPU6050 Interfacing. You may also take a look at another post where Tilt angle can also be measured by combining MPU6050 with Arduino.
Hardware Requirements:
- Arduino Uno
- MPU6050 (Accelerometer and Gyro sensor)
- L293D Motor Driver
- 5V DC Motors
- 16*2 LCD
- Connecting wires
- Breadboard
MPU6050 Gyro/Accelerometer Sensor:
Introduction:
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 the 0th bit in the 7-bit slave address of the 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 the microcontrollers SCL pin.
SDA: Serial Data pin. Connect this pin to the microcontrollers SDA pin.
GND: Ground pin. Connect this pin to a ground connection.
VCC: Power supply pin. Connect this pin to +5V DC supply.
3-Axis Gyroscope:
The MPU6050 consists of a 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 the below figure.
3-Axis Accelerometer:
The MPU6050 consists of a 3-axis Accelerometer with Micro Electro Mechanical (MEMs) technology. It used to detect the angle of tilt or inclination along the X, Y, and Z axes as shown in the below figure.
DC Motor Control using MPU6050 Gyro/Accelerometer Sensor & Arduino:
Circuit Diagram:
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 the L293D IC is connected to 2 terminals of motor M1.
– Pin No. 11, 14 of the 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 pins 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#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 that has fixed outer plates and moves along one direction. If acceleration is applied in any of the directions, 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.
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 values to rotate. So, we will map these values from -125 to 125. Now, when we move the MPU6050 towards the upward direction, the output value which we will get is 125. Then we will add 125 to this output value and this will be the speed of the first motor.
When we move the MPU 6050 towards the 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.
2 Comments
i can’t add MPU6050 Library,I2Cdev Library to arduino ide
Error in 129:21