DevLab_ICM20948 1.0.0 ICM-20948 9-Axis Motion Sensor Driver

Loading...
Searching...
No Matches
i2c_magneto.ino
Go to the documentation of this file.
1/***************************************************************
2 * @file I2C_Magneto.ino
3 * @brief Minimal I2C bring-up for 7Semi ICM-20948 +
4 * AK09916 magnetometer readout (updated API).
5 *
6 * Features
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
12 *
13 * Wiring (Arduino UNO, I2C)
14 * - SDA -> A4
15 * - SCL -> A5
16 * - VCC -> 3V3 (or 5V if board supports it)
17 * - GND -> GND
18 *
19 * Wiring (ESP32, I2C default)
20 * - SDA -> GPIO21
21 * - SCL -> GPIO22
22 * - VCC -> 3V3
23 * - GND -> GND
24 *
25 * Notes
26 * - WHO_AM_I must be 0xEA
27 * - initMag() must be called before readMag()
28 * - Magnetometer runs via internal I2C master
29 *
30 * Author : 7Semi
31 * License : MIT
32 ***************************************************************/
33#include <Wire.h>
34#include <DevLab_ICM20948.h>
35
36/* ====================== User Config ======================= */
37/** @brief I2C SDA pin used by the example board. */
38#define SDA_PIN 6
39/** @brief I2C SCL pin used by the example board. */
40#define SCL_PIN 7
41/** @brief I2C bus speed in hertz. */
42#define I2C_FREQ 400000UL
43/** @brief ICM-20948 I2C address selected by the AD0 pin. */
44#define ICM_ADDR 0x69
45
46/** @brief Global ICM-20948 driver instance. */
47DevLab_ICM20948 imu;
48
49/**
50 * @brief Configure I2C, verify the IMU, and initialize the magnetometer.
51 *
52 * The magnetometer is accessed through the ICM-20948 internal I2C master, so
53 * initMag() must succeed before loop() can read magnetic field data.
54 */
55void setup() {
56 Serial.begin(115200);
57 delay(200);
58 Serial.println(F("ICM-20948 (I2C) — Magnetometer Example"));
59 Wire.begin(SDA_PIN, SCL_PIN);
60 /* Initialize IMU. */
61 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
62 Serial.println(F("ERROR: beginI2C() failed."));
63 while (1) delay(200);
64 }
65
66 Serial.println(F("ICM-20948 ready (I2C)."));
67
68 /* Verify device. */
69 uint8_t who;
70 if (!imu.readWhoAmI(who) || who != 0xEA) {
71 Serial.println(F("ERROR: WHO_AM_I mismatch"));
72 while (1) delay(200);
73 }
74
75 Serial.print(F("WHO_AM_I = 0x"));
76 Serial.println(who, HEX);
77
78 /* Enable required sensors (mag uses internal I2C master)
79 * - accel/gyro not strictly required but safe to keep ON
80 */
81 if (!imu.setSensors(true, true, false)) {
82 Serial.println(F("setSensors failed."));
83 }
84
85 /* Initialize magnetometer. */
86 if (!imu.initMag()) {
87 Serial.println(F("ERROR: initMag() failed"));
88 while (1) delay(200);
89 }
90
91 Serial.println(F("Magnetometer ready"));
92}
93
94/**
95 * @brief Read and print magnetometer data in microtesla.
96 */
97void loop() {
98 float mx, my, mz;
99
100 /* Read magnetometer data
101 * - Output in microtesla (uT)
102 * - Returns true on success
103 */
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);
109 } else {
110 Serial.println(F("Mag read failed"));
111 }
112
113 Serial.println(F("-----------------------------"));
114 delay(500);
115}