DevLab_ICM20948 1.0.0
ICM-20948 9-Axis Motion Sensor Driver
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/** @brief I2C SDA pin used by the example board. */
32#define SDA_PIN 6
33/** @brief I2C SCL pin used by the example board. */
34#define SCL_PIN 7
35/** @brief I2C bus speed in hertz. */
36#define I2C_FREQ 400000UL
37/** @brief ICM-20948 I2C address selected by the AD0 pin. */
38#define ICM_ADDR 0x69
39
40/** @brief Global ICM-20948 driver instance. */
41DevLab_ICM20948 imu;
42
43/**
44 * @brief Configure Serial, I2C, sensor identity check, and enabled sensors.
45 *
46 * The sketch stops in an infinite loop if the IMU cannot be initialized or
47 * the WHO_AM_I register does not match the expected ICM-20948 value.
48 */
49void setup() {
50 Serial.begin(115200);
51 delay(200);
52 Serial.println(F("ICM-20948 — I2C Basic"));
53 Wire.begin(SDA_PIN, SCL_PIN);
54 /* Initialize IMU. */
55 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
56 Serial.println(F("ERROR: beginI2C() failed"));
57 while (1) delay(200);
58 }
59
60 /* WHO_AM_I check. */
61 uint8_t who;
62 if (!imu.readWhoAmI(who) || who != 0xEA) {
63 Serial.println(F("ERROR: WHO_AM_I mismatch"));
64 while (1) delay(200);
65 }
66
67 Serial.print(F("WHO_AM_I = 0x"));
68 Serial.println(who, HEX);
69
70 /* Enable all sensors. */
71 if (!imu.setSensors(true, true, true)) {
72 Serial.println(F("ERROR: setSensors failed"));
73 }
74
75 /* Initialize magnetometer. */
76 if (!imu.initMag()) {
77 Serial.println(F("Mag init failed"));
78 } else {
79 Serial.println(F("Mag initialized"));
80 }
81
82 Serial.println(F("Ready.\n"));
83}
84
85/**
86 * @brief Read accelerometer, gyroscope, magnetometer, and temperature data.
87 *
88 * Values are printed periodically to the Serial monitor in engineering units.
89 */
90void loop() {
91 float ax, ay, az;
92 float gx, gy, gz;
93 float mx, my, mz;
94 float tC;
95
96 /* Read accelerometer. */
97 if (imu.readAccel(ax, ay, az)) {
98 Serial.print(F("ACC [g]: "));
99 Serial.print(ax, 3); Serial.print(", ");
100 Serial.print(ay, 3); Serial.print(", ");
101 Serial.println(az, 3);
102 } else {
103 Serial.println(F("ACC read failed"));
104 }
105
106 /* Read gyroscope. */
107 if (imu.readGyro(gx, gy, gz)) {
108 Serial.print(F("GYR [dps]: "));
109 Serial.print(gx, 2); Serial.print(", ");
110 Serial.print(gy, 2); Serial.print(", ");
111 Serial.println(gz, 2);
112 } else {
113 Serial.println(F("GYR read failed"));
114 }
115
116 /* Read magnetometer. */
117 if (imu.readMag(mx, my, mz)) {
118 Serial.print(F("MAG [uT]: "));
119 Serial.print(mx, 2); Serial.print(", ");
120 Serial.print(my, 2); Serial.print(", ");
121 Serial.println(mz, 2);
122 } else {
123 Serial.println(F("MAG read failed"));
124 }
125
126 /* Read temperature. */
127 if (imu.readTemperature(tC)) {
128 Serial.print(F("TMP [C]: "));
129 Serial.println(tC, 2);
130 } else {
131 Serial.println(F("TMP read failed"));
132 }
133
134 Serial.println(F("-----------------------------"));
135 delay(500);
136}
137
138/* Note on magnetometer:
139 * - Uses internal I2C master (AK09916 via ICM20948)
140 * - initMag() must be called before readMag()
141 * - If values are zero:
142 * → check initMag()
143 * → verify power + pull-ups
144 */