ESP32 with MPU-6050 Accelerometer, Gyroscope and Temperature Sensor (Arduino)

Содержание

In this guide you’ll learn how to use the MPU-6050 accelerometer and gyroscope module with the ESP32. The MPU-6050 IMU (Inertial Measurement Unit) is a 3-axis accelerometer and 3-axis gyroscope sensor. The accelerometer measures the gravitational acceleration and the gyroscope measures the rotational velocity. Additionally, this module also measures temperature. This sensor is ideal to determine the orientation of a moving object.

ESP32 MPU-6050 Module Accelerometer Gyroscope Temperature Sensor Arduino

In this guide we’ll cover two examples:

Introducing the MPU-6050 Gyroscope Accelerometer Sensor

The MPU-6050 is a module with a 3-axis accelerometer and a 3-axis gyroscope.

MPU-6050 Module Accelerometer Gyroscope Temperature Sensor

The gyroscope measures rotational velocity (rad/s), this is the change of the angular position over time along the X, Y and Z axis (roll, pitch and yaw). This allows us to determine the orientation of an object.

Roll Pitch Yaw Angles

The accelerometer measures acceleration (rate of change of the object’s velocity). It senses static forces like gravity (9.8m/s 2 ) or dynamic forces like vibrations or movement. The MPU-6050 measures acceleration over the X, Y an Z axis. Ideally, in a static object the acceleration over the Z axis is equal to the gravitational force, and it should be zero on the X and Y axis.

Using the values from the accelerometer, it is possible to calculate the roll and pitch angles using trigonometry. However, it is not possible to calculate the yaw.

We can combine the information from both sensors to get more accurate information about the sensor orientation.

MPU-6050 Pinout

Here’s the pinout for the MPU-6050 sensor module.

VCC Power the sensor (3.3V or 5V)
GND Common GND
SCL SCL pin for I2C communication (GPIO 22)
SDA SDA pin for I2C communication (GPIO 21)
XDA Used to interface other I2C sensors with the MPU-6050
XCL Used to interface other I2C sensors with the MPU-6050
AD0 Use this pin to change the I2C address
INT Interrupt pin – can be used to indicate that new measurement data is available

Preparing Arduino IDE

We’ll program the ESP32 board using Arduino IDE. So, make sure you have the ESP32 add-on installed. Follow the next tutorial:

If you prefer using VS Code + PlatformIO IDE, follow the next guide:

Installing Libraries

There are different ways to get readings from the sensor. In this tutorial, we’ll use the Adafruit MPU6050 library. To use this library you also need to install the Adafruit Unified Sensor library and the Adafruit Bus IO Library.

Open your Arduino IDE and go to Sketch Include Library Manage Libraries. The Library Manager should open.

Type “adafruit mpu6050” on the search box and install the library.

Install Adafruit MPU6050 Gyroscope Accelerometer Sensor Library Arduino IDE

Then, search for “Adafruit Unified Sensor”. Scroll all the way down to find the library and install it.

Install Adafruit Unified Library Arduino IDE

Finally, search for “Adafruit Bus IO” and install it.

Install Adafruit Bus IO Library Arduino IDE

After installing the libraries, restart your Arduino IDE.

If you’re using VS Code with PaltformIO, copy the following lines to the platformio.ini file.

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

In this section you’ll learn how to get sensor readings from the MPU-6050 sensor: acceleration (x, y, z), angular velocity (x, y, z) and temperature.

Parts Required

For this example you need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

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 .

Create an Adafruit_MPU6050 object called mpu to handle the sensor.

setup()

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

Initialize the MPU-6050 sensor.

Set the accelerometer measurement range:

Set the gyroscope measurement range:

Set the filter bandwidth:

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.

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/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).

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

New sensor readings are displayed every 500 milliseconds.

Demonstration

Upload the code to your ESP32 board. Go to Tools Board and select the ESP32 board you’re using. Go to Tools Port and select the port your board is connected to. Then, click the Upload button.

Open the Serial Monitor at a baud rate of 115200, press the on-board RST button. The sensor measurements will be displayed.

Move the sensor orientation and see the values changing accordingly.

MPU-6050 Sensor Readings Serial Monitor Arduino IDE

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/s 2 ) 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/s 2
  • y: 0.28 m/s 2
  • z: 9.43 m/s 2

Display MPU-6050 Readings on OLED Display

ESP32 MPU-6050 Module Accelerometer Gyroscope Temperature Sensor Arduino Demonstration

The Adafruit MPU6050 library provides an example that dipslays the MPU-6050 gyroscope and accelerometer readings on an OLED display.

Parts Required

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

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

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

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

Learn more about using the OLED display with the ESP32: ESP32 OLED Display with Arduino IDE

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.

Install Library I2C OLED Display SSD1306 Arduino IDE

For this example, copy the following code or go to File Examples Adafruit MPU6050 MPU6050_oled.

How the Code Works

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

Create an Adafruit_MPU6050 object called mpu to handle the sensor.

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

setup()

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

Initialize the MPU-6050 sensor.

Initialize the OLED display.

Set the font size and color for the display.

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.

Get new sensor readings.

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

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

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

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

Display the gyroscope readings on the Serial Monitor.

Finally, print the gyroscope readings on the display.

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

New readings are displayed every 100 milliseconds.

Demonstration

Upload the code to your ESP32 board. Go to Tools Board and select the ESP32 board you’re using. Go to Tools Port and select the port your board is connected to. Then, click the upload button.

Open the Serial Monitor at a baud rate of 115200, press the on-board RST button. The sensor measurements will be displayed both on the Serial Monitor and on the OLED display.

MPU-6050 Module Accelerometer Gyroscope Temperature Sensor OLED Demonstration ESP32 ESP8266

Move the sensor and see the values changing.

MPU-6050 Sensor Readings OLED Example Serial Monitor

You can watch the video demonstration:

Wrapping Up

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 you’ve learned how to wire the sensor and get sensor readings. We hope you’ve found this getting started guide useful. We have guides for other popular sensors:


Источник: randomnerdtutorials.com