Bharat Pi and MPU6050 Accelerometer and Gyroscope Tutorial

by bharatpi in Circuits > Arduino

54 Views, 0 Favorites, 0 Comments

Bharat Pi and MPU6050 Accelerometer and Gyroscope Tutorial

WhatsApp Image 2024-04-10 at 16.52.49_eba0995c.jpg
  1. Accelerometer:
  • Measures acceleration along three axes: X, Y, and Z.
  • Provides data about the static acceleration due to gravity as well as dynamic acceleration in all three dimensions.
  • Useful for detecting orientation, tilt, and motion of an object.
  • Typically outputs data in units of g (acceleration due to gravity), with 1 g representing the standard acceleration due to gravity at the Earth's surface (approximately 9.8 m/s²).
  1. Gyroscope:
  • Measures angular rate of rotation or angular velocity along three axes: X, Y, and Z.
  • Provides data about the rate of change of orientation with respect to time.
  • Useful for tracking rotational motion, determining orientation changes, and stabilizing systems.
  • Outputs data in units of degrees per second (°/s) or radians per second (rad/s), indicating the rate of rotation around each axis.


Supplies

Bharat Pi boards

Jumper Wires

MPU6050

MPU6050

MPU6050.jpg

The MPU6050 is a popular integrated circuit (IC) that combines a 3-axis gyroscope and a 3-axis accelerometer into a single package. It's commonly used in various projects and products for motion sensing and tracking applications.

The MPU6050 integrates both accelerometer and gyroscope data and provides a convenient way to access this information through an I2C (Inter-Integrated Circuit) interface. By reading data from the MPU6050's registers via I2C communication, you can obtain real-time measurements of acceleration and rotation, which can be processed and utilized for various purposes in your projects.

Hardware Setup

Untitled Diagram.drawio (7).png

To connect the MPU6050 to an ESP32 microcontroller, you'll typically use the I2C (Inter-Integrated Circuit) communication protocol, as the MPU6050 supports I2C communication. Here's a step-by-step guide on how to connect the MPU6050 to an Bharat Pi:

1. Identify Pinout:

  • First, identify the pinout of the MPU6050 module. The MPU6050 typically has a total of 8 pins: VCC (or VDD): Power supply (usually 3.3V or 5V)
  • GND: Ground
  • SDA: Serial Data Line for I2C communication
  • SCL: Serial Clock Line for I2C communication
  • XDA: Auxiliary I2C bus Data
  • XCL: Auxiliary I2C bus Clock
  • AD0: I2C address LSB (can be used to change the device's I2C address)
  • INT: Interrupt output (optional)

2. Connect MPU6050 to Bharat Pi:

  • Connect the MPU6050 to the Bharat pi as follows:MPU6050 VCC to bharat pi 3.3V or 5V pin (choose based on your ESP32 board's voltage)
  • MPU6050 GND to Bharat pi GND pin
  • MPU6050 SDA to Bharat Pi GPIO pin (usually labeled as SDA or GPIO21 on ESP32)
  • MPU6050 SCL to Bharat pi GPIO pin (usually labeled as SCL or GPIO22 on ESP32)
  • If you want to change the I2C address of the MPU6050, you can connect the AD0 pin to either VCC or GND to set the LSB of the address accordingly. If left unconnected, the address is usually 0x68.

3. Power Supply:

  • Ensure that both the MPU6050 and ESP32 have a compatible power supply. Connect the power (VCC) and ground (GND) pins accordingly.



Code

Screenshot 2024-04-10 133308.png


#include <MPU6050_tockn.h>
#include <Wire.h>


MPU6050 mpu6050(Wire);


void setup() {
  Serial.begin(115200);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);
}


void loop() {
  mpu6050.update();
  Serial.print("angleX : ");
  Serial.print(mpu6050.getAngleX());
  Serial.print("\tangleY : ");
  Serial.print(mpu6050.getAngleY());
  Serial.print("\tangleZ : ");
  Serial.println(mpu6050.getAngleZ());
}

Make sure your wiring is correct, with the MPU6050 SDA and SCL pins connected to the appropriate GPIO pins on your Bharat Pi board. Also, ensure that you have a stable power supply for both the ESP32 and the MPU6050 sensor.


1 Get all data.



#include <MPU6050_tockn.h>
#include <Wire.h>


MPU6050 mpu6050(Wire);


long timer = 0;


void setup() {
  Serial.begin(115200);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);
}


void loop() {
  mpu6050.update();


  if(millis() - timer > 1000){
   
    Serial.println("=======================================================");
    Serial.print("temp : ");Serial.println(mpu6050.getTemp());
    Serial.print("accX : ");Serial.print(mpu6050.getAccX());
    Serial.print("\taccY : ");Serial.print(mpu6050.getAccY());
    Serial.print("\taccZ : ");Serial.println(mpu6050.getAccZ());
 
    Serial.print("gyroX : ");Serial.print(mpu6050.getGyroX());
    Serial.print("\tgyroY : ");Serial.print(mpu6050.getGyroY());
    Serial.print("\tgyroZ : ");Serial.println(mpu6050.getGyroZ());
 
    Serial.print("accAngleX : ");Serial.print(mpu6050.getAccAngleX());
    Serial.print("\taccAngleY : ");Serial.println(mpu6050.getAccAngleY());
 
    Serial.print("gyroAngleX : ");Serial.print(mpu6050.getGyroAngleX());
    Serial.print("\tgyroAngleY : ");Serial.print(mpu6050.getGyroAngleY());
    Serial.print("\tgyroAngleZ : ");Serial.println(mpu6050.getGyroAngleZ());
   
    Serial.print("angleX : ");Serial.print(mpu6050.getAngleX());
    Serial.print("\tangleY : ");Serial.print(mpu6050.getAngleY());
    Serial.print("\tangleZ : ");Serial.println(mpu6050.getAngleZ());
    Serial.println("=======================================================\n");
    timer = millis();
   
  }


}

  1. Include Libraries: The code includes the Wire library for I2C communication and the MPU6050_tockn library, which provides functions to interface with the MPU6050 sensor.
  2. MPU6050 Object Initialization: An instance of the MPU6050 class is created, passing the Wire object as an argument to the constructor. This sets up communication with the MPU6050 sensor over the I2C bus.
  3. Setup Function:
  • Serial communication is initialized at a baud rate of 115200.
  • The Wire communication is started.
  • The MPU6050 sensor is initialized using mpu6050.begin().
  • Gyroscope offsets are calculated using mpu6050.calcGyroOffsets(true).
  1. Loop Function:
  • Inside the loop, the mpu6050.update() function is called to update sensor data.
  • The condition if(millis() - timer > 1000) checks if one second has passed since the last data update. This ensures that sensor data is printed to the serial monitor once every second.
  • When the condition is true, sensor data is printed to the serial monitor. This includes temperature, accelerometer data (X, Y, Z), gyroscope data (X, Y, Z), accelerometer angles (X, Y), gyroscope angles (X, Y, Z), and filtered angles (X, Y, Z).
  • The timer variable is updated to store the current time, ensuring that the next data update occurs after one second.


Applications

  • Motion-controlled gaming devices
  • Gesture recognition systems
  • Inertial measurement units (IMUs) for robotics, drones, and other mobile devices
  • Motion tracking and analysis in sports science and healthcare
  • Virtual reality (VR) and augmented reality (AR) systems