Introduction
This experiment demonstrates how to use the MPU6050 sensor, which combines a gyroscope and accelerometer, to measure motion and orientation. It is commonly used in motion tracking and robotics applications.
Components Needed
- MPU6050 Sensor
- Arduino (e.g., Uno, Nano)
- Jumper wires
Circuit Setup
- Connect the VCC pin of the MPU6050 to the 5V pin on the Arduino.
- Connect the GND pin to the GND pin on the Arduino.
- Connect the SDA and SCL pins to the corresponding pins on the Arduino (A4 and A5 on Arduino Uno).
Ensure the sensor is properly connected to the Arduino.
Code for Gyroscope and Accelerometer
Upload the following code to your Arduino to read data from the MPU6050 sensor:
#include
#include
MPU6050 mpu;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
}
void loop() {
int16_t ax, ay, az;
int16_t gx, gy, gz;
mpu.getAcceleration(&ax, &ay, &az);
mpu.getRotation(&gx, &gy, &gz);
Serial.print("Accel X: "); Serial.print(ax);
Serial.print(" Y: "); Serial.print(ay);
Serial.print(" Z: "); Serial.println(az);
Serial.print("Gyro X: "); Serial.print(gx);
Serial.print(" Y: "); Serial.print(gy);
Serial.print(" Z: "); Serial.println(gz);
delay(500);
}
Explanation
The code initializes the MPU6050 sensor and reads both the acceleration and gyroscope data. The sensor detects movement along three axes (X, Y, Z), and the data is output to the serial monitor.
Troubleshooting
- If the sensor data appears as 0, check the wiring and ensure the sensor is powered correctly.
- If the readings are unstable, try recalibrating the sensor and verifying the I2C connection.