How to Use the MPU6500 Sensor with Arduino

 

ESP32 Accelerometer & Gyroscope

In the world of electronics and robotics, sensors play a crucial role in bridging the physical and digital realms. The MPU6500 is a high-performance motion sensor that combines:

·        A gyroscope

·        An accelerometer

·        A temperature sensor

This versatile sensor is widely used in applications like motion tracking, gesture recognition, and orientation detection.

The project focuses on establishing communication between the Arduino and MPU6500 sensor using the I2C protocol. Through this setup:

·        The sensor is calibrated

·        Its data is processed to provide meaningful insights

·        Collected data is displayed in a user-friendly format via the Arduino Serial Monitor



The MPU6500 is a versatile sensor capable of measuring acceleration, gyroscopic values, and temperature. We walk through an Arduino program that reads data from the MPU6500 and outputs it to the Serial Monitor.


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.println();

  Serial.print("Acceleration in g (x,y,z): ");

  Serial.print("X:");

  Serial.print(gValue.x);

  Serial.print(",");

  Serial.print("Y:");

  Serial.print(gValue.y);

  Serial.print(",");

  Serial.print("Z:");

  Serial.println(gValue.z);

 

  Serial.print("Gyroscope data in degrees: ");

  Serial.print("X:");

  Serial.print(gyr.x);

  Serial.print(",");

  Serial.print("Y:");

  Serial.print(gyr.y);

  Serial.print(",");

  Serial.print("Z:");

  Serial.print(gyr.z);

  Serial.println();

  Serial.print("Temperature in °C: ");

  Serial.print(temp);

  Serial.println();

  Serial.print("********************************************");

  delay(20);

}

Code Explanation for MPU6500 Sensor

Overview: This code approach to initializing, calibrating, configuring, and utilizing the MPU6500 sensor for motion and environmental data.

1. Library Inclusion:

The necessary libraries for MPU6500 communication and I2C are included. These libraries simplify sensor setup and data retrieval.

2. Sensor Initialization:

The sensor's I2C address is defined, and an object is created to manage interactions with the MPU6500.

3. Setup Functionality:

  • Serial Communication: The microcontroller establishes communication with the Serial Monitor to display sensor data.
  • I2C Protocol: I2C communication between the microcontroller and the MPU6500 sensor is initiated.
  • Connection Check: The sensor's connection is tested, and a status message is printed to indicate whether the MPU6500 is responding.

4. Calibration:

The sensor is calibrated by automatically adjusting offset values. This step ensures accurate accelerometer and gyroscope measurements by accounting for any inherent biases.

5. Sensor Configuration:

  • Gyroscope: The digital low-pass filter (DLPF) is enabled, and the sensitivity is set to ±250°/sec.
  • Accelerometer: The range is configured to ±2g, and its DLPF is also enabled.
  • Sample Rate: The sample rate for sensor data collection is defined.

6. Data Reading in the Loop:

  • Acceleration Values: The accelerometer provides data on the forces acting along the x, y, and z axes.
  • Gyroscope Values: The gyroscope measures angular velocity for the x, y, and z axes.
  • Temperature: The sensor's internal temperature is read.
  • Resultant Acceleration: The total acceleration considering all three axes is calculated.

7. Data Display:

The data from the accelerometer, gyroscope, and temperature sensor is formatted and printed to the Serial Monitor for real-time visualization.

8. Loop Delay:

A short delay is added between each iteration of the loop to allow time for data processing and to stabilize readings.

 

... Thank you ...

Post a Comment (0)
Previous Post Next Post