The MPU6050 is a microelectromechanical system (MEMS) that combines a 3-axis accelerometer and a 3-axis gyroscope into one unit. This sensor is widely used in various applications such as motion tracking, gesture recognition, and robotics. Its compatibility with Wokwi, an online simulator for electronic projects, allows users to simulate and test their projects before actual hardware implementation.
Pin Name | Description |
---|---|
VCC | Power supply (2.3V to 3.4V) |
GND | Ground |
SCL | Serial Clock Line for I2C communication |
SDA | Serial Data Line for I2C communication |
XDA | Auxiliary Data I2C Serial Data Line |
XCL | Auxiliary Data I2C Serial Clock Line |
AD0 | I2C Address Select |
INT | Interrupt Output |
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Wire.begin();
Serial.begin(9600);
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed");
while (1);
}
Serial.println("MPU6050 connection successful");
}
void loop() {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Print acceleration values in m/s^2
Serial.print("aX = "); Serial.print(ax/16384.0); Serial.print(" | ");
Serial.print("aY = "); Serial.print(ay/16384.0); Serial.print(" | ");
Serial.print("aZ = "); Serial.println(az/16384.0);
// Print gyroscope values in degrees/s
Serial.print("gX = "); Serial.print(gx/131.0); Serial.print(" | ");
Serial.print("gY = "); Serial.print(gy/131.0); Serial.print(" | ");
Serial.print("gZ = "); Serial.println(gz/131.0);
delay(1000);
}
I2C scanner
sketch to confirm the device's address and connectivity.Q: Can the MPU6050 be used with a 5V microcontroller? A: Yes, but ensure that the I2C lines are level-shifted to protect the MPU6050.
Q: How can I change the sensitivity of the accelerometer or gyroscope? A: Use the library functions to set the desired full-scale range.
Q: What is the purpose of the AD0 pin? A: The AD0 pin is used to set the LSB of the I2C address, allowing for two MPU6050s on the same bus.