Complete Guide: How to Connect MPU6050 to ESP32 - Physical Wiring and Code Setup

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. All you will need is an ESP32 and a soldered MPU6050 which you can purchase here:

- Use Discount Code SHILLEHTEK for 30% Off!

Buy MPU6050 on ShillehTek

- Or buy it on Amazon

Buy MPU6050 on Amazon

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.

Recognizing the technical challenges that can come with micro-soldering delicate components, our MPU6050 modules at ShillehTek come pre-soldered. This not only saves you time and effort but also ensures that you get reliable and consistent performance right out of the box. Whether you're building your first project or a seasoned creator, our pre-soldered MPU6050 modules provide a convenient and hassle-free solution, letting you focus on the creative aspects of your projects.

Also, be sure to subscribe and support the channel if you have not!

Subscribe:

Youtube

Support:

https://www.buymeacoffee.com/mmshilleh

1-) Physical Connection

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:

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. Thanks for your time.

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.