ESP32

Interface MPU6050 Accelerometer & Gyroscope Sensor with Esp32

Introduction

The MPU6050 is a popular MEMS (Microelectromechanical systems) sensor module that is used to measure acceleration, rotational motion, and orientation in 3D space. The module consists of a 3-axis accelerometer and a 3-axis gyroscope, which are combined on a single integrated circuit. The MPU6050 is widely used in various applications, including drones, robotics, and gaming devices.

The ESP32 is a powerful microcontroller that is widely used in the Internet of Things (IoT) applications. It features Wi-Fi and Bluetooth connectivity, making it an excellent choice for building IoT devices. In this article, we will discuss how to interface the MPU6050 with ESP32 and how to read its sensor values on 16×2 i2c LCD and SSD1306 Oled display module.




Amazon Links:

ESP32 development board

MPU-6050 sensor module

SSD1306 OLED display

16×2 i2c Lcd Display

Breadboard

 Jumper wires

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

What is MPU6050?

MPU6050 Accelerometer and Gyroscope Sensor pinout

The MPU6050 is a widely-used and adaptable motion tracking component that integrates a 3-axis accelerometer and a 3-axis gyroscope into a single chip, resulting in a 6-axis motion tracking device.It is designed and manufactured by InvenSense, which is now part of TDK Corporation. The MPU6050 has gained popularity in the hobbyist and DIY electronics community due to its low cost, ease of use, and wide range of applications.

The gyroscope and accelerometer in the MPU6050 work together to provide a comprehensive motion tracking solution. The gyroscope measures angular velocity around each of its three axes (x, y, and z), while the accelerometer measures acceleration along each of the same three axes. By combining the data from both sensors, the MPU6050 is able to provide accurate information about the orientation and movement of the device it is embedded in.

One of the key advantages of the MPU6050 is its high level of integration. Traditionally, motion tracking systems required separate gyroscopes and accelerometers, which had to be carefully calibrated and synchronized to work together. With the MPU6050, both sensors are combined on a single chip, and the necessary calibration and synchronization is performed automatically.

The MPU6050 communicates with an external microcontroller through a standard I2C bus interface. This allows developers to easily integrate the device into their projects, and to access the full range of features offered by the MPU6050.

Some of the most common applications for the MPU6050 include:

  • Robotics: The MPU6050 is frequently used in robotics applications to provide precise measurements of the orientation and movement of robotic limbs and other components. This information can be used to control the movement of the robot in real-time.
  • Gaming: The MPU6050 is used in gaming controllers and other input devices to provide motion sensing capabilities. This allows users to control their games using natural movements and gestures, rather than relying on traditional button-based controls.
  • Virtual Reality: The MPU6050 is also used in virtual reality (VR) applications, where it can be used to track the movement of a user’s head or body. This information can then be used to update the VR environment in real-time, creating a more immersive and realistic experience for the user.
  • Drones: The MPU6050 is often used in drones and other unmanned aerial vehicles (UAVs) to provide precise measurements of the vehicle’s orientation and movement. This information can be used to stabilize the vehicle in flight, and to control its movements in real-time.
  • Fitness Tracking: The MPU6050 can be used in wearable fitness devices to track the movement and activity of the user. This information can be used to monitor exercise performance, track progress over time, and provide feedback to the user.

In addition to its core motion tracking capabilities, the MPU6050 also includes a range of advanced features, such as a built-in temperature sensor, programmable digital filters, and an on-board FIFO buffer for storing data. These features make the MPU6050 a versatile and powerful tool for a wide range of applications.

Overall, the MPU6050 is a highly capable and versatile motion tracking device that has found wide use in a variety of industries and applications. Its low cost, ease of use, and high level of integration have made it a popular choice among hobbyists, DIY enthusiasts, and professional developers alike.



MPU6050 Accelerometer & Gyroscope Pinout:

MPU6050 Accelerometer and Gyroscope Sensor pinout

The MPU6050 pinout consists of a total of 8 pins arranged in a single row on the device. These pins are used for power supply, communication with external microcontrollers, and to provide access to the various sensors and features of the device.

The following is a brief description of each pin on the MPU6050:

  • VCC: This pin is used to supply power to the MPU6050. It requires a voltage input between 2.3V and 3.4V.
  • GND: This pin is used to connect the MPU6050 to ground.
  • SDA: This pin is used for bi-directional data transfer between the MPU6050 and an external microcontroller. It is connected to the microcontroller’s data line in an I2C communication setup.
  • SCL: This pin is used to clock data transfer between the MPU6050 and the external microcontroller in an I2C communication setup.
  • AD0: This pin is used to change the device address of the MPU6050. When it is connected to VCC, the device address is set to 0x69. When it is connected to GND, the device address is set to 0x68.
  • INT: This pin is used to indicate when the MPU6050 has new data available for processing. It can be configured to generate an interrupt signal to the external microcontroller, allowing for real-time processing of data.
  • XDA: This pin provides access to the raw data from the X-axis accelerometer.
  • XCL: This pin provides access to the raw data from the X-axis gyroscope.

In addition to these pins, the MPU6050 also has a built-in temperature sensor that can be accessed through the I2C interface. This allows developers to monitor the temperature of the device and take appropriate actions to prevent overheating.

It is important to note that the MPU6050 is a surface mount device (SMD), which means that it does not have traditional through-hole pins. Instead, it is designed to be mounted directly onto a printed circuit board (PCB) using a soldering process. This requires specialized equipment and techniques, making it a bit more challenging to work with than some other types of electronic components.

To simplify the process of working with the MPU6050, there are a number of breakout boards and modules available that include the device pre-soldered onto a small PCB with pins or headers that can be easily connected to an external microcontroller. These modules often include additional features such as voltage regulators, level shifters, and filtering circuits to ensure reliable operation of the MPU6050 in a wide range of applications.

In summary, the MPU6050 pinout consists of 8 pins that are used for power supply, communication with external microcontrollers, and to provide access to the various sensors and features of the device. Understanding how to work with these pins is essential for integrating the MPU6050 into your projects and applications.



Required Libraries installation:

To interface an MPU6050 with an ESP32 microcontroller, you will need to install the following libraries:

Adafruit_Sensor.h installation:

The Adafruit_Sensor.h library is a sensor library for Arduino and compatible microcontrollers that provides a common interface for reading sensor data. Here are the steps to install the Adafruit_Sensor.h library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.”

MPU6050 Accelerometer and Gyroscope Sensor library manager

In the Library Manager, search for “Adafruit Sensor” in the search bar. The library should appear in the search results.

adafruit sensor library installation for MPU6050 Accelerometer and Gyroscope Sensor

Click on the Adafruit Sensor library and then click the “Install” button to install the library.

After installation is complete, close the Library Manager and return to the Arduino IDE.

Adafruit_MPU6050.h installation

the Adafruit_MPU6050.h library is a sensor library for Arduino and compatible microcontrollers that provides an interface for reading data from the MPU6050 accelerometer and gyroscope sensor. Here are the steps to install the Adafruit_MPU6050.h library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.”

MPU6050 Accelerometer and Gyroscope Sensor library manager

In the Library Manager, search for “Adafruit MPU6050” in the search bar. The library should appear in the search results.

adafruit MPU6050 Accelerometer and Gyroscope Sensor

Click on the Adafruit MPU6050 library and then click the “Install” button to install the library.

After installation is complete, close the Library Manager and return to the Arduino IDE.



Adafruit_SSD1306.h installation

The Adafruit_SSD1306.h library is a display library for Arduino and compatible microcontrollers that provides an interface for driving OLED displays based on the SSD1306 controller chip. Here are the steps to install the Adafruit_SSD1306.h library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.”

MPU6050 Accelerometer and Gyroscope Sensor library manager

In the Library Manager, search for “Adafruit SSD1306” in the search bar. The library should appear in the search results.

ssd1306 oled display library installation for MPU6050 Accelerometer and Gyroscope Sensor

Click on the Adafruit SSD1306 library and then click the “Install” button to install the library in my case I installed it already that why it display the update button.

After installation is complete, close the Library Manager and return to the Arduino IDE.

To use the library in your code, you will need to include the Adafruit_SSD1306.h library at the top of your sketch.

Adafruit_GFX.h installation

The Adafruit_GFX.h library is a graphics library for Arduino and compatible microcontrollers that provides a set of common graphics operations, such as drawing lines, rectangles, and circles. Here are the steps to install the Adafruit_GFX.h library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.”

MPU6050 Accelerometer and Gyroscope Sensor library manager

In the Library Manager, search for “Adafruit GFX” in the search bar. The library should appear in the search results.

adafruit gfx library installation for MPU6050 Accelerometer and Gyroscope Sensor

Click on the Adafruit GFX by adafruit library and then click the “Install” button to install the library but in my case I installed that why it display the update button.

After installation is complete, close the Library Manager and return to the Arduino IDE.




Adafruit busIO installation

The Adafruit BusIO library is a communication library for Arduino and compatible microcontrollers that provides a set of common communication protocols, such as I2C, SPI, and UART. Here are the steps to install the Adafruit BusIO library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.”

MPU6050 Accelerometer and Gyroscope Sensor library manager

In the Library Manager, search for “Adafruit BusIO” in the search bar. The library should appear in the search results.

adafruit busio library installation for MPU6050 Accelerometer and Gyroscope Sensor

Click on the Adafruit Bus library and then click the “Install” button to install the library but in my case I installed it already that why it display the update button.

After installation is complete, close the Library Manager and return to the Arduino IDE.

16×2 i2c library installation

To use a 16×2 I2C LCD display with your ESP32, you’ll need to install a library that supports the display. Here are the steps to install the LiquidCrystal_I2C library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.”

MPU6050 Accelerometer and Gyroscope Sensor library manager

In the Library Manager, search for “LiquidCrystal_I2C” in the search bar. The library should appear in the search results.

16x2 i2c lcd library installation for MPU6050 Accelerometer and Gyroscope Sensor

Click on the LiquidCrystal_I2C by frank de brabander library and then click the “Install” button to install the library.

After installation is complete, close the Library Manager and return to the Arduino IDE.

To use the library in your code, you will need to include the LiquidCrystal_I2C library at the top of your sketch. You can do this by adding the following line to the top of your sketch:

#include <LiquidCrystal_I2C.h>



Display MPU6050 DATA on esp32 Serial Monitor:

In this section, we will explore how to display MPU6050 data on the ESP32 serial monitor using the Arduino IDE.

Mpu6050 with ESP32 Circuit diagram:

MPU6050 Accelerometer and Gyroscope Sensor connection with esp32

The MPU6050 requires a 3.3V power supply, while the ESP32 can be powered by either 5V or 3.3V. Connect the VCC pin of the MPU6050 to a 3.3V pin on the ESP32, and connect the GND pin of the MPU6050 to a GND pin on the ESP32.

The MPU6050 communicates using the I2C protocol, which requires just two wires: SDA (data) and SCL (clock). The ESP32 has two dedicated I2C pins: GPIO21 for SDA and GPIO22 for SCL. Connect the SDA pin of the MPU6050 to GPIO21 on the ESP32, and connect the SCL pin of the MPU6050 to GPIO22 on the ESP32.

In order to use the I2C bus to communicate with the MPU6050, you need to configure the ESP32’s I2C bus. You can do this using the Wire library, which provides an easy-to-use interface for I2C communication.



MPU6050 with ESP32 Code:

Code explanation:

This code is written in C++ and is designed to read data from an MPU6050 sensor using an esp32 board. The MPU6050 is a 6-degree-of-freedom (6-DOF) motion tracking sensor, which includes a 3-axis accelerometer and a 3-axis gyroscope.

Let’s go through the code line by line:

These are the libraries being used in the code. The Wire library is used for I2C communication, while the Adafruit_Sensor and Adafruit_MPU6050 libraries are used to interface with the MPU6050 sensor.

This line creates an instance of the Adafruit_MPU6050 class called “mpu”. This will be used to communicate with the sensor.

The setup function runs once when the program starts. The first line initializes serial communication with a baud rate of 9600. The “while (!Serial);” line waits until a connection is established before continuing. This is useful when the Arduino board is connected to a computer for debugging.

The next line, “Wire.begin()”, initializes the I2C bus. Then, the “if (!mpu.begin())” statement checks if the MPU6050 sensor is detected on the I2C bus. If it is not detected, the program prints an error message and enters an infinite loop.

The next three lines set the accelerometer and gyroscope ranges to 2g and 250 degrees per second (dps), respectively. The filter bandwidth is also set to 5 Hz.

The loop function runs repeatedly after the setup function has completed. In this function, three variables of type “sensors_event_t” are created to hold the accelerometer, gyroscope, and temperature data from the MPU6050 sensor.

The line “mpu.getEvent(&a, &g, &temp)” reads the sensor data into the “a”, “g”, and “temp” variables. The “&” symbol is used to pass the memory addresses of these variables to the “getEvent” function.

The next several lines print the sensor data to the serial monitor using the “Serial.print” function. The “println” function is used to print a newline character at the end of each line. The delay function is used to pause for 500 milliseconds before

Output:

MPU6050 Accelerometer and Gyroscope Sensor ouput on serial monitor



Display MPU-6050 Readings on 16X2 I2C LCD using esp32:

The MPU-6050 is a commonly used 6-axis accelerometer and gyroscope sensor module. It is used to measure the motion, orientation, and vibration of objects in real-time. The ESP32 is a powerful microcontroller that can be used to control the MPU-6050 and display its readings on a 16×2 I2C LCD. In this section, we will walk through the steps required to display MPU-6050 readings on a 16×2 I2C LCD using an ESP32.

Hardware Requirements

To follow along with this tutorial, you’ll need the following hardware:

  • ESP32 development board
  • MPU-6050 sensor module
  • 16×2 I2C Lcd
  • Breadboard and jumper wires

About 16×2 i2c lcd:

The most common LCD configuration with I2C interface is 16×2, which means 16 characters per line and 2 lines of text. However, there are other sizes of LCD screens available with I2C interface, such as 20×4 or 8×2, which have more or less characters per line and lines of text.

LCD displays with I2C interface are popular among hobbyists and professionals for their ease of use and convenience. Instead of using multiple pins to control the display, you only need two wires for I2C communication, which makes it easy to integrate into projects with limited space or pin resources.

The 16×2 I2C LCD module typically has an onboard IC that converts the I2C signals into parallel signals that can be displayed on the LCD screen. The IC typically used for this purpose is the PCF8574, which is an 8-bit I/O expander that communicates with the microcontroller via I2C protocol. The PCF8574 IC converts the I2C signals into parallel signals that can control the LCD module.

The 16×2 I2C LCD module has 16 pins, which are usually arranged in two rows of eight pins. The pins are used for power supply, I2C communication, and controlling the LCD module. The pins can be divided into four groups:

  • Power supply pins: These pins are used to provide power to the LCD module. The most common power supply pins are VCC and GND, which are used to provide the positive and negative power supply to the module, respectively.
  • I2C communication pins: These pins are used for I2C communication between the microcontroller and the LCD module. The most common I2C communication pins are SDA and SCL, which are used for data and clock signals, respectively.
  • Contrast control pin: This pin is used to adjust the contrast of the LCD screen. The contrast control pin is usually labeled as VO, and a variable resistor is often used to adjust the contrast.
  • Control pins: These pins are used to control the LCD module. The most common control pins are RS, RW, and EN. The RS pin is used to select the register, the RW pin is used to set the read or write mode, and the EN pin is used to enable the LCD module.

The 16×2 I2C LCD module can display up to 16 characters per line and up to two lines of text. The characters can be alphanumeric, symbols, or custom characters. The display can be used to show various information such as sensor readings, messages, and status updates.

In conclusion, the 16×2 I2C LCD is a popular type of LCD display that uses I2C communication interface to communicate with the microcontroller. It is widely used in various projects, including robotics, home automation, and data loggers, to display important information or debug messages. The 16×2 I2C LCD module has 16 pins, which are used for power supply, I2C communication, and controlling the LCD module. With its ease of use and convenience, the 16×2 I2C LCD is a versatile and valuable component in any microcontroller-based project.




16×2 I2C LCD display and MPU6050 with ESP32 Circuit:

16x2 i2c lcd and MPU6050 Accelerometer and Gyroscope Sensor connection with esp32

The 16×2 I2C LCD display module and the MPU6050 both require a 3.3V power supply. Connect the VCC pin of the 16×2 I2C LCD display module and the MPU6050 to a 3.3V pin on the ESP32, and connect the GND pins of both modules to a GND pin on the ESP32.

Both the 16×2 I2C LCD display module and the MPU6050 communicate using the I2C protocol, which requires just two wires: SDA (data) and SCL (clock). The ESP32 has two dedicated I2C pins: GPIO21 for SDA and GPIO22 for SCL. Connect the SDA pin of the 16×2 I2C LCD display module to GPIO21 on the ESP32, and connect the SCL pin of the 16×2 I2C LCD display module to GPIO22 on the ESP32. Connect the SDA pin of the MPU6050 to GPIO21 on the ESP32, and connect the SCL pin of the MPU6050 to GPIO22 on the ESP32.



16×2 I2C LCD display and MPU6050 with ESP32 Code:

Code explanation:

This code includes four libraries that are required for the ESP32 to interact with the MPU6050 Accelerometer and Gyroscope Sensor and display data on an LCD.

Wire.h: This library allows the ESP32 to communicate using the I2C protocol, which is used to communicate with the MPU6050 sensor and the LCD.

Adafruit_Sensor.h: This library provides common sensor types for Adafruit sensor libraries. It is a dependency for the Adafruit_MPU6050 library.

Adafruit_MPU6050.h: This is a library for the MPU6050 sensor, which is a 6-axis accelerometer and gyroscope. This library allows the ESP32 to read data from the MPU6050 sensor.

LiquidCrystal_I2C.h: This is a library for the LCD display that allows the ESP32 to display data on a 16×2 LCD using I2C communication protocol.

These libraries need to be included at the beginning of the code for the program to be able to use their functions and features.

These lines of code declare two objects of type Adafruit_MPU6050 and LiquidCrystal_I2C respectively.

Adafruit_MPU6050 mpu;: This creates an object called mpu of the Adafruit_MPU6050 class, which is used to communicate with the MPU6050 sensor.



LiquidCrystal_I2C lcd(0x27, 16, 2);: This creates an object called lcd of the LiquidCrystal_I2C class, which is used to communicate with the LCD display. The parameters passed to the constructor are:

0x27: The I2C address of the LCD display. This may vary depending on the specific LCD module used.

16: The number of columns in the LCD display.

2: The number of rows in the LCD display.

Serial.begin(9600);: This line initializes the serial communication with the computer at a baud rate of 9600 bits per second. The ESP32 will use the Serial interface to communicate with the computer and print messages to the Serial Monitor.

lcd.init();: This line initializes the LiquidCrystal_I2C library for the LCD display. This function sets up the communication protocol between the ESP32 and the LCD display.

lcd.backlight();: This line turns on the backlight of the LCD display. This function ensures that the LCD display is visible by providing backlighting.

lcd.clear();: This line clears the contents of the LCD display. This ensures that the display is empty before printing any data.

while (!Serial);: This line waits until a serial connection is established between the ESP32 and the computer. The program will not continue until a connection is made.

Wire.begin();: This line initializes the I2C communication protocol. The I2C protocol is used to communicate with the MPU6050 sensor.

if (!mpu.begin()): This line checks if the MPU6050 sensor is properly connected and initialized. If the sensor is not found, the program will print “MPU6050 not found” to the Serial Monitor and the program will stop executing.

mpu.setAccelerometerRange(MPU6050_RANGE_2_G);: This line sets the accelerometer range to +/- 2g. The accelerometer is used to measure the acceleration of the sensor in the x, y, and z directions.

mpu.setGyroRange(MPU6050_RANGE_250_DEG);: This line sets the gyroscope range to +/- 250 degrees per second. The gyroscope is used to measure the angular velocity of the sensor in the x, y, and z directions.

mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);: This line sets the filter bandwidth to 5Hz. This function sets the filter cutoff frequency for the low pass filter, which is used to remove high-frequency noise from the data.

The loop() function is called repeatedly after the setup() function has been executed. Here’s a step-by-step explanation of the loop() function:

Three sensor event variables a, g, and temp are defined.

The mpu.getEvent() function is called with the addresses of a, g, and temp as arguments. This function retrieves the latest sensor data from the MPU6050 sensor and updates the values of a, g, and temp with the latest acceleration, gyro, and temperature values respectively.

The cursor of the LCD is set to the first row and second column using lcd.setCursor(0, 0).

The X, Y, and Z axis acceleration values are printed on the first row of the LCD using lcd.print(a.acceleration.x, 1), lcd.print(a.acceleration.y, 1), and lcd.print(a.acceleration.z, 1) respectively. The 1 argument specifies that only one decimal point should be printed.

The cursor of the LCD is then set to the second row and first column using lcd.setCursor(0, 1).

The X, Y, and Z axis gyro values are printed on the second row of the LCD using lcd.print(g.gyro.x, 1), lcd.print(g.gyro.y, 1), and lcd.print(g.gyro.z, 1) respectively. The 1 argument specifies that only one decimal point should be printed.

The program waits for 1000 milliseconds (1 second) using the delay(1000) function before repeating the process. This results in the sensor data being updated and displayed on the LCD once every second.

output:

MPU6050 Accelerometer and Gyroscope Sensor output on 16x2 i2c lcd



Display MPU-6050 Readings on SSD1306 Oled using esp32:

In this section, we’ll explore how to interface the MPU-6050 Accelerometer and Gyroscope sensor with an ESP32 microcontroller and display the sensor readings on an SSD1306 OLED display.

Hardware Requirements

To proceed with this tutorial, you will require the following hardware:

  • ESP32 development board
  • MPU-6050 sensor module
  • SSD1306 OLED display
  • Breadboard and jumper wires

About SSD1306 Oled Display Module:

The SSD1306 is a popular OLED (Organic Light-Emitting Diode) display controller that is commonly used in small electronic devices such as smartwatches, digital cameras, and portable media players. It is capable of displaying monochrome images and text with high contrast and low power consumption, making it an excellent choice for battery-powered devices.




OLED Display Technology

OLED technology is a relatively new display technology that differs from traditional LCD (Liquid Crystal Display) technology. Unlike LCDs, OLEDs do not require a backlight to illuminate the display, as each pixel generates its own light. This makes OLED displays thinner, lighter, and more power-efficient than LCD displays.

SSD1306 Controller

The SSD1306 controller is a single-chip OLED driver that can control up to 128×64 pixels (or 128×32 pixels) of monochrome OLED displays. It uses a simple I2C (Inter-Integrated Circuit) interface for communication with the host microcontroller.

The SSD1306 controller has several features that make it a popular choice for small display applications. For example, it supports a wide range of voltages (from 1.65V to 3.3V) and has a low power consumption of only a few microamps in standby mode. It also supports several display modes such as normal, inverse, and gray-scale.

SSD1306 Display Modules

There are many different SSD1306 display modules available on the market, each with its own size, resolution, and pinout. However, most of these modules share the same basic design, consisting of a small OLED display panel, an SSD1306 controller chip, and a few supporting components.

One of the most common SSD1306 Oled display modules is the 0.96-inch OLED display module, which has a resolution of 128×64 pixels and is available in both I2C and SPI (Serial Peripheral Interface) versions. This module is commonly used in small electronic devices and is supported by many popular microcontrollers such as the Arduino, nodemcu, raspberry pi, raspberry pi pico and the ESP32.



SSD1306 Oled display and MPU6050 with ESP32 Circuit:

To connect MPU6050 and SSD1306 Oled display with the ESP32 microcontroller, we need to use the I2C pins of the ESP32 which are GPIO 21 (SDA) and GPIO 22 (SCL). The SDA pin of MPU6050 is connected to GPIO 21 (SDA) of the ESP32, and the SCL pin of MPU6050 is connected to GPIO 22 (SCL) of the ESP32.

Similarly, the SDA pin of SSD1306 is connected to GPIO 21 (SDA) of the ESP32, and the SCL pin of SSD1306 is connected to GPIO 22 (SCL) of the ESP32.

ssd1306 oled display and MPU6050 Accelerometer and Gyroscope Sensor connection with esp32

Once the devices are connected, the ESP32 can read data from the MPU6050 Accelerometer and Gyroscope Sensor and send it to the SSD1306 OLED display for visualization.

SSD1306 Oled display and MPU6050 with ESP32 Code:



Code Explanation:

This code is designed to read data from an MPU6050 accelerometer and gyroscope sensor and display the data on an OLED display using the Adafruit_SSD1306 library. The code includes several library files that provide additional functionality and support for the various sensors and displays.

Here is a breakdown of the code:

These are the library files used in the code. Adafruit_Sensor, Wire, Adafruit_GFX, Adafruit_SSD1306, and Adafruit_MPU6050 are libraries for interfacing with the various sensors and display.

These lines define the screen size and initialize the OLED display. The ORelay_RESET variable is set to -1, which means the OLED display will not be reset during initialization.

This line initializes the MPU6050 sensor using the Adafruit_MPU6050 library.

This code block is executed once when the device is powered on. It initializes the serial communication, Wire library, MPU6050 sensor, and OLED display. It also clears the display to ensure that the screen is empty.

This code block is executed repeatedly in a loop. It reads data from the mpu6050 accelerometer and gyroscope sensors using the getEvent() function, clears the OLED display, and prints the sensor data to the display using various display functions such as setTextSize(), setTextColor(), println(), and print(). Finally, it displays the data on the OLED display and waits for 500 milliseconds before repeating the process.

MPU6050 Accelerometer and Gyroscope Sensor output on ssd1306 oled display

Watch Video Tutorial:

Related Articles:

Automatic Speed Controller for Electric Bike using Arduino and Gyroscope mpu6050

Swimming Pool Monitoring System using Arduino and LoRa, Save a child from drowning

Reduce Distracted Driving Accidents, Driver Drowsiness Detection System using Arduino

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button