Test code
//www.ekostra.com
#include <Wire.h>
const int MPU6050_ADDR = 0x68; // MPU-6050 I2C address
int16_t accelerometer_x, accelerometer_y, accelerometer_z;
int16_t gyroscope_x, gyroscope_y, gyroscope_z;
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // Set to 0 to wake up the MPU-6050
Wire.endTransmission();
}
void loop() {
// Read accelerometer and gyroscope data
readMPU6050Data();
// Print the values
Serial.print("Accelerometer: ");
Serial.print("X = "); Serial.print(accelerometer_x);
Serial.print(", Y = "); Serial.println(accelerometer_y);
Serial.print(", Z = "); Serial.println(accelerometer_z);
//Serial.print("Gyroscope: ");
//Serial.print("X = "); Serial.print(gyroscope_x);
//Serial.print(", Y = "); Serial.print(gyroscope_y);
//Serial.print(", Z = "); Serial.println(gyroscope_z);
delay(20); // Delay for 1 second
}
void readMPU6050Data() {
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(0x3B); // Starting register for accelerometer data
Wire.endTransmission(false);
Wire.requestFrom(MPU6050_ADDR, 14, true); // Request 14 bytes from MPU-6050
accelerometer_x = Wire.read() << 8 | Wire.read();
accelerometer_y = Wire.read() << 8 | Wire.read();
accelerometer_z = Wire.read() << 8 | Wire.read();
delay(100);
gyroscope_x = Wire.read() << 8 | Wire.read();
gyroscope_y = Wire.read() << 8 | Wire.read();
gyroscope_z = Wire.read() << 8 | Wire.read();
}
Reviews
Clear filtersThere are no reviews yet.