3 * @brief Magnetometer operation-mode sweep for the 7Semi ICM-20948.
4 * @details Initializes the AK09916 magnetometer through the ICM-20948 internal
5 * I2C master, iterates through continuous measurement modes, and prints CSV-like
6 * magnetic field samples to the Serial monitor.
10#include <DevLab_ICM20948.h>
11// -----------------------------------------------------------
12// PINES I2C — ESP32C6 NANO Unit Electronics
13// -----------------------------------------------------------
14/** @brief I2C SDA pin used by the example board. */
16/** @brief I2C SCL pin used by the example board. */
18/** @brief I2C bus speed in hertz. */
19#define I2C_FREQ 400000UL
20/** @brief ICM-20948 I2C address selected by the AD0 pin. */
23/** @brief Global ICM-20948 driver instance. */
26/** @brief Print a separator line to Serial. */
27void printLinea() { Serial.println(F("------------------------------------------------------------")); }
28/** @brief Print a double separator line to Serial. */
29void printDobleLinea() { Serial.println(F("============================================================")); }
31/** @brief Magnetometer operating modes tested by the sweep. */
32const ICM20948_Op_Mode opMode[] = {
33 ICM20948_Op_Mode::MODE_POWER_DOWN,
34 ICM20948_Op_Mode::MODE_SINGLE_MEASUREMENT,
35 ICM20948_Op_Mode::MODE_CONTINUOUS_1,
36 ICM20948_Op_Mode::MODE_CONTINUOUS_2,
37 ICM20948_Op_Mode::MODE_CONTINUOUS_3,
38 ICM20948_Op_Mode::MODE_CONTINUOUS_4,
39 ICM20948_Op_Mode::MODE_SELF_TEST
42/** @brief Text labels matching the operating modes printed in the sweep. */
43const char* etiquetasOpModeCfg[] = {
44 "PowerDown","SingleMeasure","Continuo1","Continuo2","Continuo3","Continuo4"
46/** @brief Number of printable magnetometer operation modes. */
47const uint8_t N_OPM = 6;
50 * @brief Initialize the magnetometer and collect samples for each mode.
53 /* Initialize magnetometer. */
55 Serial.println(F("ERROR: initMag() failed"));
58 for(int i = 2; i < N_OPM; i++){
59 imu.setMagOpMode(opMode[i]);
61 Serial.println(F("Magnetometer ready"));
64 /* Read magnetometer data
65 * - Output in microtesla (uT)
66 * - Returns true on success
68 for(int r = 0; r < 500 ; r++){
69 if (imu.readMag(mx, my, mz)) {
70 Serial.printf("[%d]",r);
72 Serial.print(etiquetasOpModeCfg[i]);
74 Serial.print(mx, 2); Serial.print(F("; "));
75 Serial.print(my, 2); Serial.print(F("; "));
76 Serial.println(mz, 2);
78 Serial.println(F("Mag read failed"));
81 Serial.println(F("-----------------------------"));
88 * @brief Initialize I2C, verify the IMU, enable sensors, and start the sweep.
91 // put your setup code here, to run once:
94 Serial.println(F("ICM-20948 (I2C) — Magnetometer Example"));
96 Wire.begin(SDA_PIN,SCL_PIN);
98 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
99 Serial.println(F("ERROR: beginI2C() failed."));
100 while (1) delay(200);
103 Serial.println(F("ICM-20948 ready (I2C)."));
107 if (!imu.readWhoAmI(who) || who != 0xEA) {
108 Serial.println(F("ERROR: WHO_AM_I mismatch"));
109 while (1) delay(200);
112 Serial.print(F("WHO_AM_I = 0x"));
113 Serial.println(who, HEX);
115 /* Enable required sensors (mag uses internal I2C master)
116 * - accel/gyro not strictly required but safe to keep ON
118 if (!imu.setSensors(true, true, false)) {
119 Serial.println(F("setSensors failed."));
122 Serial.println(F(" Sensor listo.\n"));
123 Serial.println(F(" Sensor PLANO y QUIETO sobre la mesa."));
124 Serial.println(F(" Iniciando en 5 segundos..."));
125 for (int i = 5; i > 0; i--) {
126 Serial.printf(" %d...\n", i);
135 * @brief Empty loop; this sketch runs the magnetometer sweep once in setup().
138 // put your main code here, to run repeatedly: