top of page
Writer's pictureRamesh G

Arduino Based Gyroscope Accelerometer Controlled Servo Motor


In this Project, we will measure an angle using MPU6050 Accelerometer and Gyroscope Sensor with an Arduino and control Servo Motors. The Accelerometer sends X, Y, and Z acceleration forces. We need to convert the forces into X, Y, Z 3D angle to determine the 3D Orientation of the sensor.The gyroscope measures rotational velocity or rate of change of the angular position over time, along the X, Y, and Z-axis. It uses MEMS technology and the Coriolis Effect for measuring. The outputs of the gyroscope are in degrees per second, so in order to get the angular position, we just need to integrate the angular velocity.

The accelerometer can measure gravitational acceleration along the 3 axes and using some trigonometry math we can calculate the angle at which the sensor is positioned. So, if we use, or combine the MPU6050 accelerometer and gyroscope data we can get very accurate information about the sensor orientation.

Circuit Diagram


Components Required

Arduino Uno - 1 no

MPU6050 GY-521 - 1no

LCD20x4 I²C - 1no

Servo 180°- 3 no

MPU6050 Accelerometer and Gyroscope Sensor

The MPU6050 is a Micro Electro-Mechanical Systems (MEMS) which consists of a 3-axis Accelerometer and 3-axis Gyroscope inside it. This helps us to measure acceleration, velocity, orientation, displacement and many other motion related parameter of a system or object. This module also has a (DMP) Digital Motion Processor inside it which is powerful enough to perform complex calculation and thus free up the work for Microcontroller.

The module also have two auxiliary pins which can be used to interface external IIC modules like an magnetometer, however it is optional. Since the IIC address of the module is configurable more than one MPU6050 sensor can be interfaced to a Microcontroller using the AD0 pin. This module also has well documented and revised libraries available hence it’s very easy to use with famous platforms like Arduino. So if you are looking for a sensor to control motion for your RC Car, Drone, Self balancing Robot, Humanoid, Biped or something like that then this sensor might be the right choice for you.


The hardware of the module is very simple, it actually comprises of the MPU6050 as the main components as shown above. Since the module works on 3.3V, a voltage regulator is also used. The IIC lines are pulled high using a 4.7k resistor and the interrupt pin is pulled down using another 4.7k resistor.

The MPU6050 module allows us to read data from it through the IIC bus. Any change in motion will be reflected on the mechanical system which will in turn vary the voltage. Then the IC has a 16-bit ADC which it uses to accurately read these changes in voltage and stores it in the FIFO buffer and makes the INT (interrupt) pin to go high. This means that the data is ready to be read, so we use a MCU to read the data from this FIFO buffer through IIC communication.

Refer Project for More detail about MPU6050 sensor.

Applications

  • Used for IMU measurement

  • Drones / Quad copters

  • Self balancing robots

  • Robotic arm controls

  • Humanoid robots

  • Tilt sensor

  • Orientation / Rotation Detector

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 the ground connection. VCC: Power supply pin. Connect this pin to +5V DC supply.


Installing the Arduino Library

LiquidCrystal_I2C.h : you need to Download and install the LiquidCrystal_I2C library.

Follow the next steps to install those libraries.


In your Arduino IDE, to install the libraries go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.


After installing the required libraries, copy the following code to your Arduino IDE.


Subscribe and Download code.

Arduino code: Servo Sweep Test

#include <Servo.h>


Servo myservo; // create servo object to control a servo

Servo myservo1; // create servo object to control a servo

Servo myservo2; // create servo object to control a servo

// twelve servo objects can be created on most boards


int pos = 0; // variable to store the servo position


void setup() {

myservo.attach(11); // attaches the servo on pin 9 to the servo object

myservo1.attach(10); // attaches the servo on pin 9 to the servo object

myservo2.attach(9); // attaches the servo on pin 9 to the servo object

}


void loop() {

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees

// in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees

// in steps of 1 degree

myservo1.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

myservo1.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees

// in steps of 1 degree

myservo2.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

myservo2.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

}

After a successful upload, Press the “EN/RST” button on the Arduino Uno board.


Arduino code: Main Code

#include<Wire.h>

#include <Servo.h>

Servo servo0;

Servo servo1;

Servo servo2;

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);

const int MPU_addr=0x68;

int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

int minVal=265;

int maxVal=402;

double x;

double y;

double z;

void setup(){

Wire.begin();

lcd.init(); // initialize the lcd

// Print a message to the LCD.

lcd.backlight();

Wire.beginTransmission(MPU_addr);

Wire.write(0x6B);

Wire.write(0);

Wire.endTransmission(true);

Serial.begin(9600);

servo0.attach(10);

servo1.attach(9);

servo2.attach(11);

}

void loop(){

Wire.beginTransmission(MPU_addr);

Wire.write(0x3B);

Wire.endTransmission(false);

Wire.requestFrom(MPU_addr,14,true);

AcX=Wire.read()<<8|Wire.read();

AcY=Wire.read()<<8|Wire.read();

AcZ=Wire.read()<<8|Wire.read();

int xAng = map(AcX,minVal,maxVal,-90,90);

int yAng = map(AcY,minVal,maxVal,-90,90);

int zAng = map(AcZ,minVal,maxVal,-90,90);

x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);

y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);

z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

Serial.print("AngleX= ");

Serial.println(x);

Serial.print("AngleY= ");

Serial.println(y);

Serial.print("AngleZ= ");

Serial.println(z);


int servo0Value = map(x, 0, 360, 0, 180);

int servo1Value = map(y, 0, 360, 0, 180);

int servo2Value = map(z, 0, 360, 0, 180);

delay(400);

// Control the servos according to the MPU6050 orientation

servo0.write(servo0Value);

delay(100);

servo1.write(servo1Value);

delay(100);

servo2.write(servo2Value);

delay(100);

Serial.println(servo0Value);

Serial.println(servo1Value);

Serial.println(servo2Value);

Serial.println("-----------------------------------------");

lcd.setCursor(0,0);

lcd.print("3Servo Angle Control");

lcd.setCursor(0,1);

lcd.print("X angle : ");

lcd.print(servo0Value);

lcd.print(" ");

lcd.setCursor(0,2);

lcd.print("Y angle : ");

lcd.print(servo1Value);

lcd.print(" ");

lcd.setCursor(0,3);

lcd.print("Z angle : ");

lcd.print(servo2Value);

lcd.print(" ");

delay(500);


}

After a successful upload, open the Serial Monitor at a baud rate of 9600. Press the “EN/RST” button on the Arduino Uno board and Open Serial Monitor and see the result in Serial Monitor.



Recent Posts

See All

Comments


bottom of page