1/***************************************************************
2 * @file I2C_Magneto.ino
3 * @brief Minimal I2C bring-up for 7Semi ICM-20948 +
4 * AK09916 magnetometer readout (updated API).
7 * - I2C init on default/custom pins
8 * - IMU beginI2C() initialization
9 * - Manual sensor enable
10 * - Magnetometer initialization (initMag)
11 * - Read magnetometer (uT) at ~2 Hz
13 * Wiring (Arduino UNO, I2C)
16 * - VCC -> 3V3 (or 5V if board supports it)
19 * Wiring (ESP32, I2C default)
26 * - WHO_AM_I must be 0xEA
27 * - initMag() must be called before readMag()
28 * - Magnetometer runs via internal I2C master
32 ***************************************************************/
34#include <DevLab_ICM20948.h>
36/* ====================== User Config ======================= */
37/** @brief I2C SDA pin used by the example board. */
39/** @brief I2C SCL pin used by the example board. */
41/** @brief I2C bus speed in hertz. */
42#define I2C_FREQ 400000UL
43/** @brief ICM-20948 I2C address selected by the AD0 pin. */
46/** @brief Global ICM-20948 driver instance. */
50 * @brief Configure I2C, verify the IMU, and initialize the magnetometer.
52 * The magnetometer is accessed through the ICM-20948 internal I2C master, so
53 * initMag() must succeed before loop() can read magnetic field data.
58 Serial.println(F("ICM-20948 (I2C) — Magnetometer Example"));
59 Wire.begin(SDA_PIN, SCL_PIN);
61 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
62 Serial.println(F("ERROR: beginI2C() failed."));
66 Serial.println(F("ICM-20948 ready (I2C)."));
70 if (!imu.readWhoAmI(who) || who != 0xEA) {
71 Serial.println(F("ERROR: WHO_AM_I mismatch"));
75 Serial.print(F("WHO_AM_I = 0x"));
76 Serial.println(who, HEX);
78 /* Enable required sensors (mag uses internal I2C master)
79 * - accel/gyro not strictly required but safe to keep ON
81 if (!imu.setSensors(true, true, false)) {
82 Serial.println(F("setSensors failed."));
85 /* Initialize magnetometer. */
87 Serial.println(F("ERROR: initMag() failed"));
91 Serial.println(F("Magnetometer ready"));
95 * @brief Read and print magnetometer data in microtesla.
100 /* Read magnetometer data
101 * - Output in microtesla (uT)
102 * - Returns true on success
104 if (imu.readMag(mx, my, mz)) {
105 Serial.print(F("MAG [uT]: "));
106 Serial.print(mx, 2); Serial.print(F(", "));
107 Serial.print(my, 2); Serial.print(F(", "));
108 Serial.println(mz, 2);
110 Serial.println(F("Mag read failed"));
113 Serial.println(F("-----------------------------"));