Controlling servo motor with accelerometer | MPU6050 with servo

Controlling servo motor with accelerometer

Hello, welcome back. In this tutorial, we will learn how to work the MPU6050 sensor with a servo motor. Also, From this, we can also understand how the values change as the MPU6050 accelerometer sensor moves along one axis. In this tutorial, a servo motor is used to identify it. If you want, you can test this function on other axes as well. Also, the knowledge contained in this tutorial is most important for making drones, prototype aircraft, and self-balancing robots.

The process of this tutorial

When activating this MPU6050 accelerometer sensor via the Arduino board. Through this, the values moving along 6 axes are received by the Arduino board through I2C communication. Next, using only the value of the accelerometer Y-axis from these 6 axes, the same value is converted to the number of degrees that a servo motor rotates. Then, We can see how the servo motor rotates as the sensor moves.

OK, let’s do this project step by step. The required components are given below.

Step 1

Firstly, identify these components.

Step 2

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

Controlling servo motor with accelerometer

Step 3

Thirdly, let’s creates the program for this project. It is as follows.

/*MPU6050 sensor with servo control.
   http://srituhobby.com
*/

#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 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

Similar Posts

7 Comments

  1. 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?

  2. 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?

  3. 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.

  4. 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

Leave a Reply

Your email address will not be published.