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