DevLab_ICM20948 1.0.0 ICM-20948 9-Axis Motion Sensor Driver

Loading...
Searching...
No Matches
mag_test.ino
Go to the documentation of this file.
1/**
2 * @file mag_test.ino
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.
7 */
8
9#include <Wire.h>
10#include <DevLab_ICM20948.h>
11// -----------------------------------------------------------
12// PINES I2C — ESP32C6 NANO Unit Electronics
13// -----------------------------------------------------------
14/** @brief I2C SDA pin used by the example board. */
15#define SDA_PIN 6
16/** @brief I2C SCL pin used by the example board. */
17#define SCL_PIN 7
18/** @brief I2C bus speed in hertz. */
19#define I2C_FREQ 400000UL
20/** @brief ICM-20948 I2C address selected by the AD0 pin. */
21#define ICM_ADDR 0x69
22
23/** @brief Global ICM-20948 driver instance. */
24DevLab_ICM20948 imu;
25
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("============================================================")); }
30
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
40};
41
42/** @brief Text labels matching the operating modes printed in the sweep. */
43const char* etiquetasOpModeCfg[] = {
44 "PowerDown","SingleMeasure","Continuo1","Continuo2","Continuo3","Continuo4"
45};
46/** @brief Number of printable magnetometer operation modes. */
47const uint8_t N_OPM = 6;
48
49/**
50 * @brief Initialize the magnetometer and collect samples for each mode.
51 */
52void applySettings(){
53 /* Initialize magnetometer. */
54 if (!imu.initMag()) {
55 Serial.println(F("ERROR: initMag() failed"));
56 while (1) delay(200);
57 }
58 for(int i = 2; i < N_OPM; i++){
59 imu.setMagOpMode(opMode[i]);
60 delay(100);
61 Serial.println(F("Magnetometer ready"));
62 float mx, my, mz;
63
64 /* Read magnetometer data
65 * - Output in microtesla (uT)
66 * - Returns true on success
67 */
68 for(int r = 0; r < 500 ; r++){
69 if (imu.readMag(mx, my, mz)) {
70 Serial.printf("[%d]",r);
71 Serial.print(";");
72 Serial.print(etiquetasOpModeCfg[i]);
73 Serial.print(";");
74 Serial.print(mx, 2); Serial.print(F("; "));
75 Serial.print(my, 2); Serial.print(F("; "));
76 Serial.println(mz, 2);
77 } else {
78 Serial.println(F("Mag read failed"));
79 }
80 }
81 Serial.println(F("-----------------------------"));
82 delay(500);
83 }
84
85}
86
87/**
88 * @brief Initialize I2C, verify the IMU, enable sensors, and start the sweep.
89 */
90void setup() {
91 // put your setup code here, to run once:
92 Serial.begin(115200);
93 delay(200);
94 Serial.println(F("ICM-20948 (I2C) — Magnetometer Example"));
95
96 Wire.begin(SDA_PIN,SCL_PIN);
97 /* Initialize IMU. */
98 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
99 Serial.println(F("ERROR: beginI2C() failed."));
100 while (1) delay(200);
101 }
102
103 Serial.println(F("ICM-20948 ready (I2C)."));
104
105 /* Verify device. */
106 uint8_t who;
107 if (!imu.readWhoAmI(who) || who != 0xEA) {
108 Serial.println(F("ERROR: WHO_AM_I mismatch"));
109 while (1) delay(200);
110 }
111
112 Serial.print(F("WHO_AM_I = 0x"));
113 Serial.println(who, HEX);
114
115 /* Enable required sensors (mag uses internal I2C master)
116 * - accel/gyro not strictly required but safe to keep ON
117 */
118 if (!imu.setSensors(true, true, false)) {
119 Serial.println(F("setSensors failed."));
120 }
121
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);
127 delay(1000);
128 }
129
130 applySettings();
131
132}
133
134/**
135 * @brief Empty loop; this sketch runs the magnetometer sweep once in setup().
136 */
137void loop() {
138 // put your main code here, to run repeatedly:
139
140}