Accelerometer and Gyro Data Logging

Introduction

This experiment demonstrates how to log data from an accelerometer and gyro sensor using Arduino.

Accelerometer and Gyro Sensor

Components Needed

Circuit Setup

  1. Connect the VCC and GND pins of the MPU6050 to the 5V and GND on Arduino, respectively.
  2. Connect the SDA and SCL pins of the MPU6050 to the SDA (A4) and SCL (A5) pins of the Arduino.

The sensor will output accelerometer and gyroscope data, which can be logged and analyzed.

Accelerometer and Gyro Circuit Setup

Code for Accelerometer and Gyro Data Logging

Upload the following code to your Arduino to log accelerometer and gyro data:


#include 
#include 

MPU6050 sensor;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  sensor.initialize();
}

void loop() {
  int ax, ay, az;
  int gx, gy, gz;

  sensor.getAcceleration(&ax, &ay, &az);
  sensor.getRotation(&gx, &gy, &gz);

  Serial.print("Accel: ");
  Serial.print("X: ");
  Serial.print(ax);
  Serial.print(", Y: ");
  Serial.print(ay);
  Serial.print(", Z: ");
  Serial.print(az);
  Serial.print(" | ");

  Serial.print("Gyro: ");
  Serial.print("X: ");
  Serial.print(gx);
  Serial.print(", Y: ");
  Serial.print(gy);
  Serial.print(", Z: ");
  Serial.println(gz);

  delay(500);
}
            

Explanation

The MPU6050 sensor detects acceleration and rotational motion, sending data to the Arduino for logging and monitoring via the Serial Monitor.

Troubleshooting