DevLab_ICM20948 1.0.2
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 DevLab ICM-20948.
4 * @author Jonathan Mejorado Lopez
5 * @details Initializes the AK09916 magnetometer through the ICM-20948 internal
6 * I2C master, iterates through continuous measurement modes, and prints CSV-like
7 * magnetic field samples to the Serial monitor.
8 */
9
10#include <Wire.h>
11#include <DevLab_ICM20948.h>
12// -----------------------------------------------------------
13// PINES I2C — ESP32C6 NANO Unit Electronics
14// -----------------------------------------------------------
15/** @brief I2C SDA pin used by the example board. */
16#define SDA_PIN 6
17/** @brief I2C SCL pin used by the example board. */
18#define SCL_PIN 7
19/** @brief I2C bus speed in hertz. */
20#define I2C_FREQ 400000UL
21/** @brief ICM-20948 I2C address selected by the AD0 pin. */
22#define ICM_ADDR 0x69
23
24/** @brief Global ICM-20948 driver instance. */
26
27/** @brief Print a separator line to Serial. */
28void printLinea() { Serial.println(F("------------------------------------------------------------")); }
29/** @brief Print a double separator line to Serial. */
30void printDobleLinea() { Serial.println(F("============================================================")); }
31
32/** @brief Magnetometer operating modes tested by the sweep. */
42
43/** @brief Text labels matching the operating modes printed in the sweep. */
44const char* etiquetasOpModeCfg[] = {
45 "PowerDown","SingleMeasure","Continuo1","Continuo2","Continuo3","Continuo4"
46};
47/** @brief Number of printable magnetometer operation modes. */
48const uint8_t N_OPM = 6;
49
50/**
51 * @brief Initialize the magnetometer and collect samples for each mode.
52 */
54 /* Initialize magnetometer. */
55 if (!imu.initMag()) {
56 Serial.println(F("ERROR: initMag() failed"));
57 while (1) delay(200);
58 }
59 for(int i = 2; i < N_OPM; i++){
61 delay(100);
62 Serial.println(F("Magnetometer ready"));
63 float mx, my, mz;
64
65 /* Read magnetometer data
66 * - Output in microtesla (uT)
67 * - Returns true on success
68 */
69 for(int r = 0; r < 500 ; r++){
70 if (imu.readMag(mx, my, mz)) {
71 Serial.printf("[%d]",r);
72 Serial.print(";");
73 Serial.print(etiquetasOpModeCfg[i]);
74 Serial.print(";");
75 Serial.print(mx, 2); Serial.print(F("; "));
76 Serial.print(my, 2); Serial.print(F("; "));
77 Serial.println(mz, 2);
78 } else {
79 Serial.println(F("Mag read failed"));
80 }
81 }
82 Serial.println(F("-----------------------------"));
83 delay(500);
84 }
85
86}
87
88/**
89 * @brief Initialize I2C, verify the IMU, enable sensors, and start the sweep.
90 */
91void setup() {
92 // put your setup code here, to run once:
93 Serial.begin(115200);
94 delay(200);
95 Serial.println(F("ICM-20948 (I2C) — Magnetometer Example"));
96
97 Wire.begin(SDA_PIN,SCL_PIN);
98 /* Initialize IMU. */
99 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
100 Serial.println(F("ERROR: beginI2C() failed."));
101 while (1) delay(200);
102 }
103
104 Serial.println(F("ICM-20948 ready (I2C)."));
105
106 /* Verify device. */
107 uint8_t who;
108 if (!imu.readWhoAmI(who) || who != 0xEA) {
109 Serial.println(F("ERROR: WHO_AM_I mismatch"));
110 while (1) delay(200);
111 }
112
113 Serial.print(F("WHO_AM_I = 0x"));
114 Serial.println(who, HEX);
115
116 /* Enable required sensors (mag uses internal I2C master)
117 * - accel/gyro not strictly required but safe to keep ON
118 */
119 if (!imu.setSensors(true, true, false)) {
120 Serial.println(F("setSensors failed."));
121 }
122
123 Serial.println(F(" Sensor listo.\n"));
124 Serial.println(F(" Sensor PLANO y QUIETO sobre la mesa."));
125 Serial.println(F(" Iniciando en 5 segundos..."));
126 for (int i = 5; i > 0; i--) {
127 Serial.printf(" %d...\n", i);
128 delay(1000);
129 }
130
132
133}
134
135/**
136 * @brief Empty loop; this sketch runs the magnetometer sweep once in setup().
137 */
138void loop() {
139 // put your main code here, to run repeatedly:
140
141}
ICM20948_Op_Mode
bool setMagOpMode(ICM20948_Op_Mode opMode)
bool readWhoAmI(uint8_t &whoAmI)
bool beginI2C(uint8_t address=0x69, TwoWire &i2cPort=Wire, uint32_t i2cSpeed=400000)
bool readMag(float &x, float &y, float &z)
bool setSensors(bool accel_on, bool gyro_on, bool temp_on)
#define SCL_PIN
I2C SCL pin used by the example board.
Definition mag_test.ino:18
void printDobleLinea()
Print a double separator line to Serial.
Definition mag_test.ino:30
void setup()
Initialize I2C, verify the IMU, enable sensors, and start the sweep.
Definition mag_test.ino:91
#define SDA_PIN
I2C SDA pin used by the example board.
Definition mag_test.ino:16
const uint8_t N_OPM
Number of printable magnetometer operation modes.
Definition mag_test.ino:48
void printLinea()
Print a separator line to Serial.
Definition mag_test.ino:28
#define ICM_ADDR
ICM-20948 I2C address selected by the AD0 pin.
Definition mag_test.ino:22
const char * etiquetasOpModeCfg[]
Text labels matching the operating modes printed in the sweep.
Definition mag_test.ino:44
DevLab_ICM20948 imu
Global ICM-20948 driver instance.
Definition mag_test.ino:25
const ICM20948_Op_Mode opMode[]
Magnetometer operating modes tested by the sweep.
Definition mag_test.ino:33
void applySettings()
Initialize the magnetometer and collect samples for each mode.
Definition mag_test.ino:53
void loop()
Empty loop; this sketch runs the magnetometer sweep once in setup().
Definition mag_test.ino:138