Friday, August 4, 2023

Accelerometer Data Visualization on Desktop and Android using Qt

The MPU6050 is a popular and widely used accelerometer and gyroscope sensor that can measure motion and orientation. In this tutorial, we will learn how to interface the MPU6050 with the ESP32, a powerful microcontroller board, to obtain motion data and then use this data to visualize a cube using Qt, and since we are using Qt which is a cross-platform framework, we can easily develop an application for the Desktop environment and also for Android. The following image illustrates the Accelerometer Visualization on Windows Applications and on Android Phones.

Working Demo
Note: ESP32 is sending data to Windows using Serial Communication, while ESP32 is sending data to Android Phones using Bluetooth connection.

Requirements:

  • ESP32 development board (If you don't have an ESP32 board then you can use any Arduino-based board, but just make sure to remove the Bluetooth prints from the code)
  • MPU6050 sensor module
  • Breadboard and jumper wires
  • Arduino IDE (or you can use ESP-IDF also but I am not covering that)

This post is sponsored by PCBWay, with more than a decade in the field of PCB prototype and fabrication, PCBWay is committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness and any other demanding requests. 
As one of the most experienced PCB manufacturers in the World, PCBWay prides itself to be your best business partner as well as a good friend in every aspect of your PCB needs.


Step 1: Wiring the MPU6050 to ESP32:

Start by connecting the MPU6050 sensor to the ESP32 development board. The MPU6050 communicates using the I2C protocol, so we will connect it as follows:

  • Connect VCC to 3.3V (or 5V if your MPU6050 module supports it).
  • Connect GND to the ground pin on the ESP32.
  • Connect SDA to GPIO 21 on the ESP32.
  • Connect SCL to GPIO 22 on the ESP32.
ESP32 MPU6050 Connection

Step 2: Installing the Required Libraries:

To communicate with the MPU6050 and access its data, we need to install the necessary libraries. As we are using the Arduino IDE, open the Library Manager and search for "MPU6050." Install the library named "MPU6050_Light". This is not a hard requirement, you can use any other library of your choice, the ultimate aim is to send the "Roll" and "Pitch" values to the Serial Port and over Bluetooth, which will be used by Qt to visualize a cube 3D object.

Install MPU6050_Light Library

Step 3: Setting up the Code:

Create a new sketch in the Arduino IDE. Begin by including the required libraries for the MPU6050 and I2C communication. Then, initialize the I2C interface and the MPU6050 sensor.

Step 4: Reading Data from MPU6050:

In the main loop of your code, read data from the MPU6050 sensor. You can obtain the accelerometer and gyroscope values in three axes (X, Y, Z) using appropriate functions from the MPU6050 library.

As shown in the below-mentioned code, I am just getting "Roll" and "Pitch" values and then transmitting these values to the PC using Serial Communication, and also sending the same value over Bluetooth communication, if your device doesn't support Bluetooth, you can skip this part.

To get the "Roll" the function "mpu.getAngleX()" is used and similarly to get the value of "Pitch" the function "mpu.getAngleY()" is used.

#include "Wire.h"
#include "BluetoothSerial.h"
#include <MPU6050_light.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Private Objects and Variables
MPU6050 mpu(Wire);
BluetoothSerial SerialBT;
unsigned long timer = 0;

void setup()
{
  Serial.begin(115200);
  // Bluetooth Device Name
  SerialBT.begin("ESP32-Blue");
  Wire.begin();
 
  byte status = mpu.begin();
 
  while(status!=0){ } // stop everything if could not connect to MPU6050
  delay(1000);
  // mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
  mpu.calcOffsets(); // gyro and accelero
}

void loop()
{
  mpu.update();
  if( (millis()-timer) > 100 )
  {
    timer = millis();
    /*
    Serial.print("X : ");
    Serial.print(mpu.getAngleX());
    Serial.print("\tY : ");
    Serial.print(mpu.getAngleY());
    Serial.print("\tZ : ");
    Serial.println(mpu.getAngleZ());
    */
    // Printing Data as per our Qt PC Application format
    Serial.print(mpu.getAngleX());
    Serial.print(",");
    Serial.println(mpu.getAngleY());
    // Sending Data as per our Qt Android format
    SerialBT.print(mpu.getAngleX());
    SerialBT.print(",");
    SerialBT.println(mpu.getAngleY());
  }
}

Step 5: Receiving this data with Qt and visualizing the MPU6050 sensor position with Qt (Windows)


This is a complex topic to explain here in a post, so that's why I have prepared a video to explain the whole steps, you can watch the above-mentioned video.

Step 6: Receiving this data with Qt and visualizing the MPU6050 sensor position with Qt (Android)

Again this is a complex topic to explain here, so that's why I have prepared a video to explain some basic steps, plus most of the steps are already in the above mentioned video.


Code Download

Some Other Project Ideas:

Now that you have successfully interfaced the MPU6050 with the ESP32, you can use the motion data in various exciting projects. Here are a few ideas to get you started:

  • Create a motion-controlled robot or vehicle.
  • Build a gesture-based controller for your computer or smart home devices.
  • Develop a stabilizing platform for a camera or a quadcopter.

Conclusion:

Interfacing the MPU6050 with the ESP32 opens up a world of possibilities for motion-sensing projects. You can now accurately measure acceleration and rotation, enabling you to create a wide range of applications, from robotics to virtual reality. Experiment with the data, explore new ideas, and have fun building your projects!


No comments:

Post a Comment