electroSome

Controlling of DC Motors using MPU-6050

Contents

In this project, we are going to control the speed of 2 DC motors using the MPU-6050. MPU-6050 is an accelerometer and Gyro sensor. By moving the sensor in upward or downward direction, the speed of the motors will increase or decrease.

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.

Components Required

The components required for this project are as follows

Hardware

Circuit Diagram

 

Controlling of DC Motors using MPU-6050 Circuit Diagram

Explanation

1) Firstly, we have to make the connections for L293D motor driver with Arduino. The connections of the L293D motor driver with the Arduino are as follows.

2) Secondly, make the connections for MPU-6050 with the Arduino

MPU-6050 Accelerometer + Gyro Sensor

The MPU-6050 is a 6 DOF (Degrees of Freedom) or a six-axis IMU sensor, which means that it gives six values as output. Three values from the accelerometer and three from the gyroscope. An accelerometer works on the principle of the piezoelectric effect and Gyroscopes work on the principle of Coriolis acceleration.

The MPU-6050 is a sensor based on MEMS (Micro Electro Mechanical Systems) technology. Both the accelerometer and the gyroscope are embedded inside a single chip. This chip uses I2C (Inter-Integrated Circuit) protocol for communication.

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.

Operation of MPU-6050

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.

Software

Arduino Uno Program

#include <Wire.h>
#include <MPU6050.h>
 
#define first_motor_pin1 5
#define first_motor_pin2 6
#define second_motor_pin1 9
#define second_motor_pin2 10

MPU6050 sensor;
int16_t ax, ay, az;
int16_t gx, gy, gz;
 
int first_motor_speed;
int second_motor_speed;

void setup ( )
{
  Wire.begin( );
  Serial.begin (9600);
  Serial.println ("Initializing the sensor");
  sensor.initialize ( );
  Serial.println(sensor.testConnection( ) ? "Successfully Connected" : "Connection failed");
  delay(1000);
  Serial.println("Taking Values from the sensor");
  delay(1000);
}

void loop ( )
{
  sensor.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  ax = map(ax, -17000, 17000, -125, 125);
  first_motor_speed = 125+ax;
  second_motor_speed = 125-ax;
  
  Serial.print ("Motor1 Speed = ");
  Serial.print (first_motor_speed, DEC);
  Serial.print (" && ");
  Serial.print ("Motor2 Speed = ");
  Serial.println (second_motor_speed, DEC);
   
  analogWrite (first_motor_pin2, first_motor_speed);
  analogWrite (second_motor_pin2, second_motor_speed);
  delay (200);
}

Program Explanation

First of all, we included the library for the MPU-6050 sensor so that we can use MPU-6050 functions from the library. The MPU-6050 sensor works with Arduino through the SPI communication. Therefore, we have included the wire library which will enable the SPI communication.

#include <Wire.h>
#include <MPU6050.h>

Then we defined the pins where we have connected the motor wires. We connected the first motor to the pin no’s 5, 6 of Arduino and the second motor to the pin no’s 9, 10 of Arduino.

#define first_motor_pin1 5
#define first_motor_pin2 6
#define second_motor_pin1 9
#define second_motor_pin2 10

After that, we defined the variables which will help us in calculating the output.

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

In the setup function, we checked that whether the sensor is working properly or not. If the sensor works properly, then it will print “Successfully Connected” otherwise it will print “Connection failed”.

Serial.println(sensor.testConnection( ) ? "Successfully Connected" : "Connection failed");
delay(1000);
Serial.println("Taking Values from the sensor");

Then we took the output from MPU-6050 sensor and mapped these from -125 to 125. Then we added and subtracted mapped value with 125.

sensor.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
ax = map(ax, -17000, 17000, -125, 125);
first_motor_speed = 125+ax;    
second_motor_speed = 125-ax;  
analogWrite (first_motor_pin2, first_motor_speed);
analogWrite (second_motor_pin2, second_motor_speed);

Practical Implementation

Controlling of DC Motors using MPU-6050 Practical Implementation

Working

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 MPU-6050 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

Exit mobile version