We’ll learn about the MPU-6050 Accelerometer and Gyroscope module in this user guide, as well as how to utilise it with the ESP32 to measure accelerometer and gyroscope in the Arduino IDE. Acceleration, temperature, displacement, and angular velocity are all measured using the MPU6050 module. It comes with 6-axis motion tracking sensors that are built into the MPU6050 chip. The I2C protocol is used to transmit data.

Parts Required

For this example you need the following parts:

  • MPU-6050 Accelerometer Gyroscope -The MPU6050 module 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 parameters of a system or object.
  • ESP32 
  • Breadboard
  • Jumper wires

Introducing the MPU-6050 Gyroscope Accelerometer Sensor

The MPU6050 module is made up of a digital motion that handles all of the complicated processing and computations, as well as providing sensor data to other MCUs via I2C connection. It’s commonly used to assess the health and performance of various machinery. It’s also employed in robotics and motion sensor applications. Because this sensor is based on MEMS (Micro-Mechanical Systems) technology, it is exceedingly small and precise at specific frequencies. It’s a compact device with a low power consumption. It uses I2C communication to communicate with other microcontrollers and sensors.

Three-axis accelerometers, a three-axis gyroscope, and a temperature sensor are included in the module. All of this is contained on a single chip. Because each channel has a 16-bit analogue converter, the module takes into consideration the x, y, and z channels at the same time.

The 3-axis accelerometer measures the object’s acceleration or rate of change of velocity (m/s2). Both stationary and moving objects can be measured with this method. The rate of change of velocity for stationary objects is equal to the gravitational acceleration (9.8 m/s2), called gravity in short. A 3-axis gyroscope is also included in the module, which measures the rotational velocity of an object in rad/s. This attribute aids us in determining the object’s exact orientation.

Schematic Diagram – ESP32 with MPU-6050

Wire the ESP32 to the MPU-6050 sensor as shown in the following schematic diagram: connect the SCL pin to GPIO 22 and the SDA pin to GPIO 21.

MPU-6050 Accelerometer Gyroscope Wiring to ESP32 Schematic Diagram Circuit

Code – Getting MPU-6050 Sensor Readings: Accelerometer, Gyroscope and Temperature

The Adafruit library provides several examples for this sensor. In this section, we’ll take a look at the basic example that prints the sensor readings in the Serial Monitor.

Go to File > Examples > Adafruit MPU6050 > basic_readings. The following code should load.

It gets the angular velocity (gyroscope) on the x, y and z axis, the acceleration on the x, y and z axis and the temperature.

How the Code Works

Start by including the required libraries for the MPU-6050 sensor: Adafruit_MPU6050 and Adafruit_Sensor.

#include <Adafruit_MPU6050.h>

#include <Adafruit_Sensor.h>

Create an Adafruit_MPU6050 object called mpu to handle the sensor.

Adafruit_MPU6050 mpu;

setup()

In the setup(), initialize the serial monitor at a baud rate of 115200.

Serial.begin(115200);

Initialize the MPU-6050 sensor.

if (!mpu.begin()) {

  Serial.println(“Sensor init failed”);

  while (1)

    yield();

}

Set the accelerometer measurement range:

mpu.setAccelerometerRange(MPU6050_RANGE_8_G);

Set the gyroscope measurement range:

mpu.setGyroRange(MPU6050_RANGE_500_DEG);

Set the filter bandwidth:

mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);

loop()

In the loop() we’ll get sensor readings and display them in the Serial Monitor.

First, you need to get new sensor events with the current readings.

sensors_event_t a, g, temp;

mpu.getEvent(&a, &g, &temp);

Finally, print the readings. For the acceleration:

  • a.acceleration.x: gets acceleration on the x axis;
  • a.acceleration.y: gets acceleration on the y axis;
  • a.acceleration.z: gets acceleration on the z axis.

The acceleration is measured in meters per second square (m/s2)

Serial.print(“Acceleration X: “);

Serial.print(a.acceleration.x);

Serial.print(“, Y: “);

Serial.print(a.acceleration.y);

Serial.print(“, Z: “);

Serial.print(a.acceleration.z);

Serial.println(” m/s^2″);

To get gyroscope readings:

  • g.gyro.x: gets angular velocity on the x axis;
  • g.gyro.y: gets angular velocity on the y axis;
  • g.gyro.z: gets angular velocity on the z axis.

The angular velocity is measured in radians per seconds (rad/s).

Serial.print(“Rotation X: “);

Serial.print(g.gyro.x);

Serial.print(“, Y: “);

Serial.print(g.gyro.y);

Serial.print(“, Z: “);

Serial.print(g.gyro.z);

Serial.println(” rad/s”);

Finally, print the temperature – it is measured in Celsius degrees. To access the temperature reading use temp.temperature.

Serial.print(“Temperature: “);

Serial.print(temp.temperature);

Serial.println(” degC”);

New sensor readings are displayed every 500 milliseconds.

delay(500);

Demonstration

The code should be uploaded to your ESP32 board. Select the ESP32 board you’re using from Tools > Board. Select the port your board is linked to under Tools > Port. Then, on the Upload button, click.

Using a baud rate of 115200, open the Serial Monitor and push the on-board RST button. The sensor readings will be shown on the screen.

Sensor Calibration

Ideally, when the sensor is static, the gyroscope values should be zero on all axis, which doesn’t happen in our case. When the sensor is static, these are the gyroscope values we get:

  • x: 0.06 rad/s
  • y: -0.02 rad/s
  • z: 0.00 rad/s

On practical applications, you need to take the error into account and correct the values in the code to get more accurate readings.

The same happens for the acceleration values. The acceleration on the z axis should be closer to the gravitational force (9,8 m/s2) and it should be closer to zero on the x and y axis. In our case, these are the approximate values we get when the sensor is static:

  • x: 0.71 m/s2
  • y: 0.28 m/s2
  • z: 9.43 m/s2

Display MPU-6050 Readings on OLED Display

Parts Required

Here’s a list with the parts required to complete this example:

  • MPU-6050 Accelerometer Gyroscope
  • ESP32 (read Best ESP32 development boards)
  • 0.96 inch I2C OLED Display SSD1306
  • Breadboard
  • Jumper wires

Schematic Diagram – ESP32 with MPU-6050 and OLED Display

Wire all the parts as shown in the following schematic diagram. Because the OLED display and the MPU-6050 sensors use different I2C addresses, we can connect them to the same I2C bus (same pins on the ESP32).

Wire all the parts as shown in the following schematic diagram. Because the OLED display and the MPU-6050 sensors use different I2C addresses, we can connect them to the same I2C bus (same pins on the ESP32).

ESP32 with MPU-6050 and OLED Display Wiring Schematic Diagram Circuit

Code – Display MPU-6050 Sensor Readings on OLED Display

To use this example, make sure you have the Adafruit SSD1306 library installed. This library can be installed through the Arduino Library Manager.

Go to Sketch > Library > Manage Libraries and search for “SSD1306” and install the SSD1306 library from Adafruit.

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit MPU6050 test!");

  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }

  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }

  Serial.println("");
  delay(100);
}

void loop() {
  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  /* Print out the values */
  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");

  Serial.print("Rotation X: ");
  Serial.print(g.gyro.x);
  Serial.print(", Y: ");
  Serial.print(g.gyro.y);
  Serial.print(", Z: ");
  Serial.print(g.gyro.z);
  Serial.println(" rad/s");

  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");

  Serial.println("");
  delay(500);
}
#include <Adafruit_MPU6050.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>

Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);

void setup() {
  Serial.begin(115200);
  // while (!Serial);
  Serial.println("MPU6050 OLED demo");

  if (!mpu.begin()) {
    Serial.println("Sensor init failed");
    while (1)
      yield();
  }
  Serial.println("Found a MPU-6050 sensor");

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ; // Don't proceed, loop forever
  }
  display.display();
  delay(500); // Pause for 2 seconds
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setRotation(0);
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  display.clearDisplay();
  display.setCursor(0, 0);

  Serial.print("Accelerometer ");
  Serial.print("X: ");
  Serial.print(a.acceleration.x, 1);
  Serial.print(" m/s^2, ");
  Serial.print("Y: ");
  Serial.print(a.acceleration.y, 1);
  Serial.print(" m/s^2, ");
  Serial.print("Z: ");
  Serial.print(a.acceleration.z, 1);
  Serial.println(" m/s^2");

  display.println("Accelerometer - m/s^2");
  display.print(a.acceleration.x, 1);
  display.print(", ");
  display.print(a.acceleration.y, 1);
  display.print(", ");
  display.print(a.acceleration.z, 1);
  display.println("");

  Serial.print("Gyroscope ");
  Serial.print("X: ");
  Serial.print(g.gyro.x, 1);
  Serial.print(" rps, ");
  Serial.print("Y: ");
  Serial.print(g.gyro.y, 1);
  Serial.print(" rps, ");
  Serial.print("Z: ");
  Serial.print(g.gyro.z, 1);
  Serial.println(" rps");

  display.println("Gyroscope - rps");
  display.print(g.gyro.x, 1);
  display.print(", ");
  display.print(g.gyro.y, 1);
  display.print(", ");
  display.print(g.gyro.z, 1);
  display.println("");

  display.display();
  delay(100);
}

How the Code Works

Start by including the required libraries for the MPU-6050 sensor and for the OLED display.

#include <Adafruit_MPU6050.h>

#include <Adafruit_SSD1306.h>

#include <Adafruit_Sensor.h>

Create an Adafruit_MPU6050 object called mpu to handle the sensor.

Adafruit_MPU6050 mpu;

Create an Adafruit_SSD1306 object called display to handle the OLED display. This is for a display with 128×64 pixels.

Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);

setup()

In the setup(), initialize the serial monitor at a baud rate of 115200.

Serial.begin(115200);

Initialize the MPU-6050 sensor.

if (!mpu.begin()) {

  Serial.println(“Sensor init failed”);

  while (1)

    yield();

}

Initialize the OLED display.

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally

if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128×64

  Serial.println(F(“SSD1306 allocation failed”));

  for (;;)

    ; // Don’t proceed, loop forever

}

display.display();

Set the font size and color for the display.

display.setTextSize(1);

display.setTextColor(WHITE);

display.setRotation(0);

loop()

In the loop() is where we’ll get the sensor readings and display them on the OLED.

Start by creating events for each measurement, accelerometer, gyroscope and temperature.

sensors_event_t a, g, temp;

Get new sensor readings.

mpu.getEvent(&a, &g, &temp);

Clear the display in each loop() to write new readings.

display.clearDisplay();

Set the display cursor to (0,0) – the upper left corner. It will start writing text from that location.

display.setCursor(0, 0);

The following lines print the accelerometer readings in the Serial Monitor.

Serial.print(“Accelerometer “);

Serial.print(“X: “);

Serial.print(a.acceleration.x, 1);

Serial.print(” m/s^2, “);

Serial.print(“Y: “);

Serial.print(a.acceleration.y, 1);

Serial.print(” m/s^2, “);

Serial.print(“Z: “);

Serial.print(a.acceleration.z, 1);

Serial.println(” m/s^2″);

The following lines display the acceleration x, y an z values on the OLED display.

display.println(“Accelerometer – m/s^2”);

display.print(a.acceleration.x, 1);

display.print(“, “);

display.print(a.acceleration.y, 1);

display.print(“, “);

display.print(a.acceleration.z, 1);

display.println(“”);

Display the gyroscope readings on the Serial Monitor.

Serial.print(“Gyroscope “);

Serial.print(“X: “);

Serial.print(g.gyro.x, 1);

Serial.print(” rps, “);

Serial.print(“Y: “);

Serial.print(g.gyro.y, 1);

Serial.print(” rps, “);

Serial.print(“Z: “);

Serial.print(g.gyro.z, 1);

Serial.println(” rps”);

Finally, print the gyroscope readings on the display.

display.println(“Gyroscope – rps”);

display.print(g.gyro.x, 1);

display.print(“, “);

display.print(g.gyro.y, 1);

display.print(“, “);

display.print(g.gyro.z, 1);

display.println(“”);

Lastly, call display.display() to actually show the readings on the OLED.

display.display();

New readings are displayed every 100 milliseconds.

delay(100);

DEMONSTRATION

The code should be uploaded to your ESP32 board. Select the ESP32 board you’re using from Tools > Board. Select the port your board is linked to under Tools > Port. After that, press the upload button.

Using a baud rate of 115200, open the Serial Monitor and push the on-board RST button. On both the Serial Monitor and the OLED display, the sensor measurements will be presented.

CONCLUSION

The MPU-6050 is an accelerometer and gyroscope. It measures acceleration on the x, y and z axis as well as angular velocity. This module also measures temperature.

This sensor modules communicates via I2C communication protocol. So, the wiring is very simple. Just connect the sensor to the ESP32 default I2C pins.

In this tutorial we’ve learned how to wire the sensor and get sensor readings

Author

Write A Comment