esp32-p4-hydra-mpu6050-i2c.ino
Demonstration of the I2C interface with a common motion sensor, using the Adafruit library.
Install the 'Adafruit MPU6050' and 'Adafruit Unified Sensor' libraries via the Library Manager. Connect the sensor to the STEMMA QT connector or I2C pins (SDA/SCL). Upload the code and open the Serial Monitor to view real-time data.
esp32-p4-hydra-mpu6050-i2c.ino
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
void setup() {
Serial.begin(115200);
if (!mpu.begin()) {
Serial.println("Capteur MPU6050 non trouvé !");
while (1);
}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("MPU6050 prêt");
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.print("Accel X: "); Serial.print(a.acceleration.x, 2);
Serial.print(" | Gyro Z: "); Serial.println(g.gyro.z, 2);
delay(100);
}