Introduction
This experiment demonstrates how to log data from an accelerometer and gyro sensor using Arduino.
Components Needed
- Accelerometer and Gyro Sensor (e.g., MPU6050)
- Arduino
- Jumper Wires
Circuit Setup
- Connect the VCC and GND pins of the MPU6050 to the 5V and GND on Arduino, respectively.
- 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.
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
- Ensure that the SDA and SCL pins are correctly connected to the Arduino.
- If the readings seem incorrect, double-check the sensor wiring and the sensor’s calibration.