DevLab_ICM20948 1.0.0 Driver para sensor ICM-20948

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#define SDA_PIN 6
38#define SCL_PIN 7
39#define I2C_FREQ 400000UL
40#define ICM_ADDR 0x69
41
42DevLab_ICM20948 imu;
43
44void setup() {
45 Serial.begin(115200);
46 delay(200);
47 Serial.println(F("ICM-20948 (I2C) — Magnetometer Example"));
48 Wire.begin(SDA_PIN, SCL_PIN);
49 /** Initialize IMU */
50 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
51 Serial.println(F("ERROR: beginI2C() failed."));
52 while (1) delay(200);
53 }
54
55 Serial.println(F("ICM-20948 ready (I2C)."));
56
57 /** Verify device */
58 uint8_t who;
59 if (!imu.readWhoAmI(who) || who != 0xEA) {
60 Serial.println(F("ERROR: WHO_AM_I mismatch"));
61 while (1) delay(200);
62 }
63
64 Serial.print(F("WHO_AM_I = 0x"));
65 Serial.println(who, HEX);
66
67 /** Enable required sensors (mag uses internal I2C master)
68 * - accel/gyro not strictly required but safe to keep ON
69 */
70 if (!imu.setSensors(true, true, false)) {
71 Serial.println(F("setSensors failed."));
72 }
73
74 /** Initialize magnetometer */
75 if (!imu.initMag()) {
76 Serial.println(F("ERROR: initMag() failed"));
77 while (1) delay(200);
78 }
79
80 Serial.println(F("Magnetometer ready"));
81}
82
83void loop() {
84 float mx, my, mz;
85
86 /** Read magnetometer data
87 * - Output in microtesla (uT)
88 * - Returns true on success
89 */
90 if (imu.readMag(mx, my, mz)) {
91 Serial.print(F("MAG [uT]: "));
92 Serial.print(mx, 2); Serial.print(F(", "));
93 Serial.print(my, 2); Serial.print(F(", "));
94 Serial.println(mz, 2);
95 } else {
96 Serial.println(F("Mag read failed"));
97 }
98
99 Serial.println(F("-----------------------------"));
100 delay(500);
101}