Controlling servo motor with accelerometer | MPU6050 with servo

Hello, welcome back. In this tutorial, we’ll learn how to use the MPU6050 sensor with a servo motor. You’ll see how the sensor’s values change as it moves along one axis. We’ll use the servo motor to test this. You can also try this on other axes if you like. This information is really useful for making drones, prototype airplanes, and self-balancing robots.
The process of this tutorial
When you turn on the MPU6050 accelerometer sensor with the Arduino board, it sends data from all 6 axes to the Arduino using I2C communication. We’ll use just the Y-axis value from this data to control how many degrees the servo motor rotates. This way, you can see how the servo motor moves as the sensor changes position.
OK, let’s do this project step by step. The required components are given below.
- Arduino UNO board x 1 — Our Store / Amazon
- MPU6050 sensor x 1 — Our Store / Amazon
- Servo motor x 1 — Our Store / Amazon
- Jumper wires — Our Store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.




Step 2
Secondly, connect these components. To do this, use the circuit diagram below.

Step 3
Thirdly, let’s create the program for this project. It is as follows.
/*MPU6050 sensor with servo control.
Home Page
*/
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Servo.h>
Servo servo;
Adafruit_MPU6050 srituhobby;
void setup(void) {
Serial.begin(115200);
servo.attach(3);
Wire.begin();
srituhobby.begin();
servo.write(0);
srituhobby.setAccelerometerRange(MPU6050_RANGE_8_G);//2_G,4_G,8_G,16_G
srituhobby.setGyroRange(MPU6050_RANGE_500_DEG);//250,500,1000,2000
srituhobby.setFilterBandwidth(MPU6050_BAND_21_HZ);
delay(100);
}
void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
srituhobby.getEvent(&a, &g, &temp);
int value = a.acceleration.y;
value = map(value, -10, 10, 180, 0);
servo.write(value);
Serial.println(value);
//delay(10);
}
Code Explanation
Firstly, the MPU6050 and servo libraries are included.
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Servo.h>
Secondly, objects are created for these libraries.
Servo servo;
Adafruit_MPU6050 srituhobby;
In the setup function,
void setup(void) {
//The serial monitor is begin
Serial.begin(115200);
// This code sets the servo motor PIN
servo.attach(3);
//The wire library is begin
Wire.begin();
//The MPU6050 sensor is begin
srituhobby.begin();
// Servo motor turns to starting position
servo.write(0);
//These code lines set the accelerometer and gyroscope ranges
srituhobby.setAccelerometerRange(MPU6050_RANGE_8_G);//2_G,4_G,8_G,16_G
srituhobby.setGyroRange(MPU6050_RANGE_500_DEG);//250,500,1000,2000
srituhobby.setFilterBandwidth(MPU6050_BAND_21_HZ);
delay(100);
}
In the loop function,
void loop() {
// Get new sensor events with the readings
sensors_event_t a, g, temp;
srituhobby.getEvent(&a, &g, &temp);
// Accelerator Y-axis values are put into the variable
int value = a.acceleration.y;
// This value is converted from 0 to 180
value = map(value, -10, 10, 180, 0);
//This code line rotate the servo motor using that value
servo.write(value);
// That value is printed on the serial monitor
Serial.println(value);
//delay(10);
}
Step 4
Lastly, select the board and port. Afterward, upload this code to the Arduino board.
OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.
Controlling servo motor with accelerometer | MPU6050 with servo
Thanks – this was fun! Is there an easy way to make it possible to adapt this to only read the acceleration and based on the speed, move the servo a set number of degrees?
Sure, you can try it.
Neat project. I put it together using the same exact device you used and using your code. It works great . . . but my Uno freeze after 5 or 10 minutes. A few serial print statements indicates that it freezes during the sensor polling process:
sensors_event_t a, g, temp;
PitchSensor.getEvent(&a, &g, &temp);
Any suggestions?
This is a fun project. I am planning to use it as part of an automotive instrument, but am having some trouble. I am using the exact same hardware you used but the sketch freeze after about 10 minutes. I checked to make sure that I am using the most recent version of the libraries involved. Any thoughts you have as to what might be the problem would be most appreciated.
I think it’s a error on the Arduino board. I haven’t any idea for that.
Hey dude, Awesome work! helped me out big time. If i wanted to add a second Servo to read on a different Axis is this possible with this code? i believe it would be by just defining the 2 different servos then getting them to read the different inputs. by all means i am not a coding and i am learning so figured it was best to ask the guy who created the code?.
Thanks in advance
Lee
You have to change this code for that. First, understand this code and try it. You can do it.
I’m using the same pinouts and hardware (and also the code) but the servo can’t move the same as in the video. It only keeps rotating in the same direction. How do I supposed to do this? Thanks in advance