Skip to content

ESP32 MPU6050: Wire and read motion data | ShillehTek

October 23, 2023

Project Overview

ESP32 + MPU6050: In this project, you connect an ESP32 to an MPU6050 accelerometer/gyroscope so you can start reading motion data for IoT and embedded system projects using the Arduino IDE.

This guide focuses on the physical wiring basics and the code setup you need to integrate the MPU6050 with an ESP32.

  • Time: 20 to 40 minutes
  • Skill level: Beginner
  • What you will build: A working ESP32 + MPU6050 setup that compiles and runs in the Arduino IDE and outputs usable accelerometer and gyroscope readings

Parts List

From ShillehTek

  • Breadboard - for quick solderless prototyping
  • Jumper wires - for making stable I2C and power connections

External

  • ESP32 development board
  • MPU6050 accelerometer/gyroscope module (GY-521 or equivalent)
  • USB cable for programming the ESP32
  • Arduino IDE (installed on your computer)

Note: MPU6050 modules are typically used over I2C. Always confirm your specific module pin labels and voltage requirements before powering it from the ESP32.

Step-by-Step Guide

Step 1 - Prepare your ESP32 and Arduino IDE

Goal: Get the Arduino IDE ready to compile and upload code to your ESP32.

What to do: Install and open the Arduino IDE, connect your ESP32 to your computer over USB, and make sure you can select an ESP32 board and the correct serial port.

Expected result: The Arduino IDE recognizes your ESP32 and is ready to upload sketches.

Step 2 - Wire the MPU6050 to the ESP32

Goal: Make the physical connections required for power and I2C communication between the ESP32 and the MPU6050.

What to do: Connect the MPU6050 module to the ESP32 using the module pin labels (power, ground, and I2C lines). Use your module documentation to match the MPU6050 SDA and SCL pins to the ESP32 I2C pins you plan to use.

Expected result: The MPU6050 is powered correctly and connected to the ESP32 over I2C (SDA/SCL).

Step 3 - Add MPU6050 support in the Arduino IDE

Goal: Ensure your Arduino environment has the library support needed to talk to the MPU6050.

What to do: In the Arduino IDE, install an MPU6050-compatible library (or open an example sketch from your chosen library) so you can initialize the sensor and read accelerometer and gyroscope values.

Expected result: Your sketch compiles with the required includes and MPU6050 initialization code available.

Step 4 - Upload a test sketch and read sensor data

Goal: Verify the ESP32 can communicate with the MPU6050 and produce usable readings.

What to do: Upload a simple MPU6050 test sketch from the Arduino IDE (for example, a library example). Open the Serial Monitor to view output and confirm that accelerometer and gyroscope data updates as you move the sensor.

Expected result: The Serial Monitor shows live motion readings from the MPU6050 when connected to the ESP32.

Conclusion

You now have an ESP32 communicating with an MPU6050 accelerometer/gyroscope so you can start using motion data in Arduino IDE based projects. This setup is a solid foundation for motion sensing, stabilization, and other embedded applications.

Want the exact parts used in this kind of build? Grab what you need from ShillehTek.com. If you want help customizing this project or building something similar for your product, check out our consulting: https://shillehtek.com/pages/iot-consulting.

The ESP32 is a popular and versatile microcontroller and system-on-chip (SoC) that's widely used in various embedded systems and Internet of Things (IoT) projects. It was developed by Espressif Systems, a company based in China. The ESP32 is a successor to the ESP8266 and offers many improvements and additional features. In this tutorial, we show how to use the MPU6050 with this amazing microcontroller to start getting gyro and accelerometer values in real time, very easily with the Adafruit MPU6050 library in the Arduino IDE.

The MPU6050 is a highly capable 6-axis motion tracking device, combining a 3-axis gyroscope and a 3-axis accelerometer on a single chip. It provides real-time motion data, which is essential for various applications in robotics, motion sensing, and interactive technology. Its versatility and functionality make it an invaluable component for hobbyists and professionals alike working on motion-related projects.

Table of Contents

  • Parts List
  • Step 1: Wiring
  • Step 2: Boards Manager
  • Step 3: Library
  • Step 4: Code
  • Conclusion + Next Ideas

Parts List

From ShillehTek

  • Breadboard
  • Jumper Wires
  • Pre-Soldered ShillehTek MPU6050 

External

  • ESP32 Dev Board (WROOM-based)
  • USB Cord

Related Shillehtek Products

Step 1: Wiring

You can see the physical connection is super simple as the module uses I2C communication so we only need 4 jumper wires to get started here!

Step 2: Add the Board and Connect

Go to the boards manager in Arduino and search esp32. Download the boards by Espressif. Then you can select the board in Arduino IDE followed by the port (after you plug into your computer). You should be good to go there.

Step 3: Add Adafruit MPU6050 Library

Adafruit has a convenient library that you can download straight from the Arduino IDE

Go to Library Manager, search MPU6050, and select the Adafruit MPU6050 library.

Step 4: Run Example Script

They also have example scripts in their library now that you have it downloaded.

Go to File > Examples

In the section, you will see examples from external libraries, select the basic_readings example

You can upload this to your board right away if you have your connections ready and you should start seeing readings in the serial monitor. Make sure you open the serial monitor with the correct Baud Rate

The full code is here:

// Basic demo for accelerometer readings from Adafruit MPU6050

#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_21_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);
}

Conclusion + Next Ideas

That is it for this example, with the basic example you can customize it and add logic as you see fit.

If you enjoyed this quick tutorial and it made your life easier, be sure to subscribe to the YouTube channel by going to the video at the beginning of this tutorial and hitting subscribe. Let us know if you have any questions about this.

Want help productionizing a project like this? ShillehTek consulting covers hardware, firmware, and deployment.