ESP32 & Gyroscope Arduino Serial Plotter


In this project, we explore how to interface with the MPU6500 motion sensor using an Arduino to capture real-time gyroscope and accelerometer data. The MPU6500 is a versatile 6-axis sensor capable of measuring angular velocity, linear acceleration, and temperature, making it ideal for applications like robotics, drones, and motion tracking. By leveraging the MPU6500_WE library, we simplify sensor configuration, calibration, and data retrieval, enabling precise motion analysis with minimal effort. This guide walks through the code setup and implementation, making it easy to integrate the MPU6500 into your next Arduino project.





Code:

----------------------------------------------------------------------------------------------------------------------------

#include<MPU6500_WE.h>

#include<Wire.h>

#define MPU6500_ADDR 0x68

MPU6500_WE myMPU6500 = MPU6500_WE(MPU6500_ADDR);

 

void setup() {

  // put your setup code here, to run once:

  Serial.begin(115200);

  Wire.begin();

  if(!myMPU6500.init()){

    Serial.println("MPU6500 does not respond");

  }

  else

  {

    Serial.println("MPU6500 is connected");

  }

  Serial.println("Position you MPU6500 flat and don't move it - Calibrationg...");

  delay(1000);

  myMPU6500.autoOffsets();

  Serial.println("Done");

  myMPU6500.enableGyrDLPF();

  myMPU6500.setGyrDLPF(MPU6500_DLPF_6);

  myMPU6500.setSampleRateDivider(5);

  myMPU6500.setGyrRange(MPU6500_GYRO_RANGE_250);

  myMPU6500.setAccRange(MPU6500_ACC_RANGE_2G);

  myMPU6500.enableAccDLPF(true);

  myMPU6500.setAccDLPF(MPU6500_DLPF_6);

  delay(200);

  

}

 

void loop() {

  // put your main code here, to run repeatedly:

  xyzFloat gValue = myMPU6500.getGValues();

  xyzFloat gyr = myMPU6500.getGyrValues();

  float temp = myMPU6500.getTemperature();

  float resultantG = myMPU6500.getResultantG(gValue);

/*

  Serial.print(gValue.x);

  Serial.print(",");

  Serial.print(gValue.y);

  Serial.print(",");

  Serial.print(gValue.z);

 

  Serial.print(",");

*/

  Serial.print(gyr.x);

  Serial.print(",");

  Serial.print(gyr.y);

  Serial.print(",");

  Serial.print(gyr.z);

 

  Serial.println();

 

  delay(10);

}

-----------------------------------------------------------------------------------

Brief Explanation of the MPU6500 Sensor Code

This code demonstrates how to interface with an MPU6500 sensor using the Arduino platform to read gyroscope and accelerometer data. The MPU6500 is a motion-tracking device that measures acceleration, angular velocity, and temperature. Here's a breakdown of the code:

Libraries and Setup

  1. Libraries Included:
    • `MPU6500_WE.h`: A library for easy interfacing with the MPU6500.
    • `Wire.h`: Required for I2C communication.
  2. I2C Address and Object Creation:
    • The I2C address of the MPU6500 is defined as `0x68`.
    • An object `myMPU6500` is created for managing the MPU6500.
  3. Setup Function:
    • Initializes the I2C communication using `Wire.begin()` and starts the serial monitor.
    • Checks the connection to the MPU6500. If the sensor does not respond, an error message is printed; otherwise, it confirms the connection.
    • Calibrates the sensor to calculate offsets for accurate readings using `autoOffsets()`.
    • Configures the gyroscope and accelerometer settings, including:
      • Digital Low Pass Filters (DLPF).
      • Sampling rate and ranges for gyroscope and accelerometer.
      • Enables additional filtering for stable data.

Main Loop

The loop continuously retrieves:

  • Gyroscope Data: Angular velocities (`gyr.x`, `gyr.y`, `gyr.z`) in degrees per second.
  • Accelerometer Data: Gravity vector components (`gValue.x`, `gValue.y`, `gValue.z`) in g-force (commented out in the provided code).
  • Temperature: The onboard temperature sensor value (not displayed in the output).
  • Resultant Acceleration: The magnitude of the acceleration vector (not displayed in the output).

The gyroscope data is printed to the serial monitor for real-time monitoring.

Key Features

  • Calibration: The sensor is calibrated by placing it flat and still, allowing accurate readings.
  • Filtering: Digital filters reduce noise in gyroscope and accelerometer readings, enhancing data reliability.
  • Sampling Control: The sampling rate is adjusted using a divider for efficient data collection.

Potential Applications

This code can be used in robotics, drones, gaming controllers, or any motion-tracking applications to measure angular velocity and acceleration.

This project is a great starting point for experimenting with the MPU6500 sensor and building motion-sensitive systems!

 

… Thank you …


Post a Comment (0)
Previous Post Next Post