Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save andrewflash/b48db731d487025d1bf1fa270b6ca678 to your computer and use it in GitHub Desktop.

Select an option

Save andrewflash/b48db731d487025d1bf1fa270b6ca678 to your computer and use it in GitHub Desktop.
Exilir AIoT Bootcamp Batch 1 - Chapter 3 - IMU - Dengan Library - Blocking
/*
* MPU6500 data acquisition on ESP32-C5 using Adafruit Library
* -------------------------------------------------------------------
* Target board: DFRobot FireBeetle 2 ESP32-C5.
*
* Wiring (I2C):
* MPU6500 VCC -> 3V3
* MPU6500 GND -> GND
* MPU6500 SDA -> IO2
* MPU6500 SCL -> IO3
* MPU6500 AD0 -> GND (I2C address 0x68) atau 3V3 (I2C address 0x69)
* MPU6500 INT -> PIN_INT (optional; -1 disable dan pakai pooling)
*/
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
// Definisikan pin I2C untuk FireBeetle 2 ESP32-C5
#define I2C_SDA 2
#define I2C_SCL 3
#define MPU_ADDR 0x68
Adafruit_MPU6050 mpu;
void setup() {
// Baud rate 115200 sangat disarankan untuk Edge Impulse agar transfer data cepat
Serial.begin(115200);
while (!Serial) delay(10);
// Mulai I2C dengan pin kustom
Wire.begin(I2C_SDA, I2C_SCL);
if (!mpu.begin(MPU_ADDR, &Wire)) {
Serial.println("Sensor MPU6050 tidak ditemukan!");
Serial.println("Cek wiring SDA/SCL atau pin AD0.");
while (1) {
delay(10);
}
}
// Konfigurasi rentang dan filter (sesuaikan dengan kebutuhan Edge Impulse Anda)
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// Mencetak header satu kali untuk label di Serial Plotter (Edge Impulse akan mengabaikan teks awal ini)
Serial.println("accelX,accelY,accelZ,gyroX,gyroY,gyroZ");
delay(100);
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Format data CSV: ax, ay, az, gx, gy, gz
Serial.print(a.acceleration.x); Serial.print(",");
Serial.print(a.acceleration.y); Serial.print(",");
Serial.print(a.acceleration.z); Serial.print(",");
Serial.print(g.gyro.x); Serial.print(",");
Serial.print(g.gyro.y); Serial.print(",");
Serial.println(g.gyro.z); // Gunakan println di data terakhir agar pindah baris
// Jeda 10ms untuk mendapatkan frekuensi sampling sekitar 100Hz (Standar ML IMU)
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment