> [!TLDR] Using MPU6050 with ESP32 > This guide explains how to connect and use an MPU6050 accelerometer and gyroscope with an ESP32 microcontroller.. The provided code utilizes the `Adafruit_MPU6050` library to initialize the sensor, set the accelerometer and gyroscope ranges, configure the filter bandwidth, and read acceleration, rotation, and temperature data. This data is then printed to the serial monitor. # Introduction Using the [[Acelerômetro e Giroscópio MPU 6050|MPU 6050]] with an [[ESP32]] involves controlling GPIO pins (General Purpose Input/Output). Here's a breakdown of how to do it! ## Requisites - Hardware - [[ESP32]] - [ESP32](app://obsidian.md/ESP32) is a series of low cost and low power consumption microcontrollers. It is also a system-in-one-chip with integrated microcontroller, Wi-Fi and Bluetooth - [[Acelerômetro e Giroscópio MPU 6050|MPU 6050]] - Accelerometer and gyroscope - [[Resistores]] - Used to limit the current thought the data pin of the [[Sensor DHT22]]. - Software - [[Arduino IDE]] or [[Wokwi]] for programming/simulating the code to run on the [[ESP32]]. - Make sure to [[Instalando o driver da ESP32 no Windows|install the ESP32 driver]] to be able to upload the code to the [[ESP32]]. # Hardware Connections * **[[Acelerômetro e Giroscópio MPU 6050|MPU 6050]]:** * **Pin VCC:** Connect to the VCC on the [[ESP32]] * **Pin GND:** Connect to GND of the [[ESP32]] * **Pin SCL:** Connect to a GPIO for the I2C communication in the [[ESP32]] * **Pin SDA:** Connect to a GPIO for the I2C communication in the [[ESP32]] ![[MPU6050 with ESP32 diagram.png]] # Example Code 1 - Using the `Adafruit MPU6050` ```cpp // 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); } ``` ## Explanation This code demonstrates how to read accelerometer, gyroscope, and temperature data from an Adafruit MPU6050 sensor using the Adafruit_MPU6050 library. Let's break down the code section by section: * `Adafruit_MPU6050.h`: This header file contains the necessary definitions and functions for interacting with the MPU6050 sensor. * `Adafruit_Sensor.h`: This header provides a common interface for working with various Adafruit sensors. * `Wire.h`: This header enables I2C communication, which is used to communicate with the MPU6050. - `Adafruit_MPU6050 mpu;` - Create the MPU object using the MPU library. * `Serial.begin(115200)`: Initializes serial communication at a baud rate of 115200. This is used for printing sensor data to the serial monitor. * `mpu.begin()`: Initializes the MPU6050 sensor. If initialization fails, it prints an error message and halts the program. * `mpu.setAccelerometerRange(…)`: Sets the measurement range of the accelerometer. The example uses `MPU6050_RANGE_8_G`, meaning it can measure accelerations up to ±8g. Other options are ±2g, ±4g, and ±16g. The code then prints the selected range. * `mpu.setGyroRange(…)`: Sets the measurement range of the gyroscope. The example uses `MPU6050_RANGE_500_DEG`, meaning it can measure angular velocities up to ±500°/s. Other options are ±250°/s, ±1000°/s, and ±2000°/s. The code then prints the selected range. * `mpu.setFilterBandwidth(…)`: Sets the filter bandwidth for the sensor readings. A lower bandwidth reduces noise but also slows down the response time. The example uses `MPU6050_BAND_21_HZ`. The code then prints the selected bandwidth. * `sensors_event_t a, g, temp;`: Creates three variables of type `sensors_event_t` to store the accelerometer, gyroscope, and temperature data, respectively. * `mpu.getEvent(&a, &g, &temp);`: Reads the sensor data and stores it in the `a`, `g`, and `temp` variables. * `Serial.print(…)`: Prints the sensor readings to the serial monitor. The acceleration is printed in m/s², rotation in rad/s, and temperature in °C. * `delay(500)`: Pauses the program for 500 milliseconds (0.5 seconds) before taking the next reading.