DevLab_ICM20948 1.0.0
Driver para sensor ICM-20948
Loading...
Searching...
No Matches
I2C_Basic.ino
Go to the documentation of this file.
1/***************************************************************
2 * @file I2C_Basic.ino
3 * @brief Minimal I2C bring-up for 7Semi ICM-20948 +
4 * basic Accel/Gyro/Temp/Mag readout (updated API).
5 *
6 * Features
7 * - I2C init (UNO / ESP32)
8 * - beginI2C() initialization
9 * - Manual sensor enable (setSensors)
10 * - Read Accel / Gyro / Temp / Mag
11 *
12 * Notes
13 * - WHO_AM_I must be 0xEA
14 * - Sensors must be explicitly enabled
15 * - Magnetometer requires initMag()
16 *
17 * Wiring (Arduino UNO I2C)
18 * - SDA -> A4
19 * - SCL -> A5
20 * - VCC -> 3V3 (or 5V if supported)
21 * - GND -> GND
22 *
23 * Wiring (ESP32 default I2C)
24 * - SDA -> GPIO21
25 * - SCL -> GPIO22
26 ***************************************************************/
27#include <Wire.h>
28#include <DevLab_ICM20948.h>
29
30/* ====================== User Config ======================= */
31#define SDA_PIN 6
32#define SCL_PIN 7
33#define I2C_FREQ 400000UL
34#define ICM_ADDR 0x69
35
36DevLab_ICM20948 imu;
37
38void setup() {
39 Serial.begin(115200);
40 delay(200);
41 Serial.println(F("ICM-20948 — I2C Basic"));
42 Wire.begin(SDA_PIN, SCL_PIN);
43 /** Initialize IMU */
44 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
45 Serial.println(F("ERROR: beginI2C() failed"));
46 while (1) delay(200);
47 }
48
49 /** WHO_AM_I check */
50 uint8_t who;
51 if (!imu.readWhoAmI(who) || who != 0xEA) {
52 Serial.println(F("ERROR: WHO_AM_I mismatch"));
53 while (1) delay(200);
54 }
55
56 Serial.print(F("WHO_AM_I = 0x"));
57 Serial.println(who, HEX);
58
59 /** Enable all sensors */
60 if (!imu.setSensors(true, true, true)) {
61 Serial.println(F("ERROR: setSensors failed"));
62 }
63
64 /** Initialize magnetometer */
65 if (!imu.initMag()) {
66 Serial.println(F("Mag init failed"));
67 } else {
68 Serial.println(F("Mag initialized"));
69 }
70
71 Serial.println(F("Ready.\n"));
72}
73
74void loop() {
75 float ax, ay, az;
76 float gx, gy, gz;
77 float mx, my, mz;
78 float tC;
79
80 /** Read Accelerometer */
81 if (imu.readAccel(ax, ay, az)) {
82 Serial.print(F("ACC [g]: "));
83 Serial.print(ax, 3); Serial.print(", ");
84 Serial.print(ay, 3); Serial.print(", ");
85 Serial.println(az, 3);
86 } else {
87 Serial.println(F("ACC read failed"));
88 }
89
90 /** Read Gyroscope */
91 if (imu.readGyro(gx, gy, gz)) {
92 Serial.print(F("GYR [dps]: "));
93 Serial.print(gx, 2); Serial.print(", ");
94 Serial.print(gy, 2); Serial.print(", ");
95 Serial.println(gz, 2);
96 } else {
97 Serial.println(F("GYR read failed"));
98 }
99
100 /** Read Magnetometer */
101 if (imu.readMag(mx, my, mz)) {
102 Serial.print(F("MAG [uT]: "));
103 Serial.print(mx, 2); Serial.print(", ");
104 Serial.print(my, 2); Serial.print(", ");
105 Serial.println(mz, 2);
106 } else {
107 Serial.println(F("MAG read failed"));
108 }
109
110 /** Read Temperature */
111 if (imu.readTemperature(tC)) {
112 Serial.print(F("TMP [C]: "));
113 Serial.println(tC, 2);
114 } else {
115 Serial.println(F("TMP read failed"));
116 }
117
118 Serial.println(F("-----------------------------"));
119 delay(500);
120}
121
122/* Note on magnetometer:
123 * - Uses internal I2C master (AK09916 via ICM20948)
124 * - initMag() must be called before readMag()
125 * - If values are zero:
126 * → check initMag()
127 * → verify power + pull-ups
128 */