DevLab_ICM20948 1.0.0 ICM-20948 9-Axis Motion Sensor Driver

Loading...
Searching...
No Matches
i2c_gyro.ino
Go to the documentation of this file.
1/***************************************************************
2 * @file I2C_Gyro.ino
3 * @brief Minimal I2C bring-up for 7Semi ICM-20948 on ESP32/UNO +
4 * continuous gyroscope readout.
5 *
6 * Features
7 * - I2C init on default/custom pins
8 * - IMU begin() on I2C (addr 0x68/0x69)
9 * - Safe defaults (applyBasicDefaults)
10 * - Optional sensor gating: gyro only
11 * - Gyro config: DLPF, full-scale, ODR, AVG
12 * - Read gyro (dps) at ~2 Hz print
13 *
14 * Wiring (Arduino UNO, I2C)
15 * - SDA -> A4
16 * - SCL -> A5
17 * - VCC -> 3V3 (or 5V if your board supports it)
18 * - GND -> GND
19 *
20 * Wiring (ESP32, I2C)
21 * - SDA -> GPIO21 (default)
22 * - SCL -> GPIO22 (default)
23 * - VCC -> 3V3
24 * - GND -> GND
25 *
26 * Address
27 * - Default ICM-20948 I2C address is 0x68 (AD0=LOW). If AD0=HIGH, use 0x69.
28 ***************************************************************/
29
30#include <Wire.h>
31#include <DevLab_ICM20948.h>
32
33/* ====================== User Config =======================
34 * - Edit pins/addr if needed
35 * - UNO uses fixed SDA=A4, SCL=A5 (no need to call Wire.begin with pins)
36 * - ESP32 can use default Wire (SDA=21, SCL=22) or custom pins via Wire.begin(sda,scl)
37 */
38
39/* ====================== User Config ======================= */
40/** @brief I2C SDA pin used by the example board. */
41#define SDA_PIN 6
42/** @brief I2C SCL pin used by the example board. */
43#define SCL_PIN 7
44/** @brief I2C bus speed in hertz. */
45#define I2C_FREQ 400000UL
46/** @brief ICM-20948 I2C address selected by the AD0 pin. */
47#define ICM_ADDR 0x69
48
49/** @brief Global ICM-20948 driver instance. */
50DevLab_ICM20948 imu;
51
52/**
53 * @brief Configure the IMU for gyroscope-only operation over I2C.
54 *
55 * The function initializes the I2C interface, applies safe defaults, enables
56 * the gyroscope, and configures DLPF, full-scale range, and output data rate.
57 */
58void setup() {
59 Serial.begin(115200);
60 delay(200);
61 Serial.println(F("ICM-20948 — I2C Basic"));
62 Wire.begin(SDA_PIN, SCL_PIN);
63 /* Initialize IMU. */
64 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
65 Serial.println(F("ERROR: beginI2C() failed"));
66 while (1) delay(200);
67 }
68
69 /* Apply safe defaults. */
70 if (!imu.applyBasicDefaults()) {
71 Serial.println(F("ERROR: applyBasicDefaults() failed."));
72 while (1) delay(200);
73 }
74
75 Serial.println(F("ICM-20948 ready."));
76
77 /* Optional: gate sensors
78 * - setSensors(accel_on, gyro_on, temp_on)
79 * - Here: enable only gyro (accel/temp off)
80 */
81 if (!imu.setSensors(/*accel*/ false, /*gyro*/ true, /*temp*/ false)) {
82 Serial.println(F("setSensors failed."));
83 }
84
85 /* ----------------- GYRO DLPF Config (reference) ----------------
86 * GyroConfig(DLPFCFG, FS_SEL, FCHOICE, XGYRO, YGYRO, ZGYRO, AVGCFG)
87 * - DLPF : GYRO_DLPFCFG_0..7 (use 3 or 4 for balanced settings)
88 * - FS_SEL : dps250 / dps500 / dps1000 / dps2000
89 * - FCHOICE : false → DLPF path (recommended), true → bypass (very wide BW)
90 * - XGYRO/YGYRO/ZGYRO : per-axis enable (true = enable axis)
91 * - AVGCFG : 0..7 internal averaging (higher = more smoothing, more delay)
92 * FCHOICE=0 (DLPF on):
93 * - GYRO_DLPFCFG_1 : 3dB ≈ 196.6 Hz, NBW ≈ 229.8 Hz
94 * - GYRO_DLPFCFG_2 : 3dB ≈ 151.8 Hz, NBW ≈ 187.6 Hz
95 * - GYRO_DLPFCFG_3 : 3dB ≈ 119.5 Hz, NBW ≈ 154.3 Hz ← good default
96 * - GYRO_DLPFCFG_4 : 3dB ≈ 51.2 Hz, NBW ≈ 73.3 Hz
97 * - GYRO_DLPFCFG_5 : 3dB ≈ 23.9 Hz, NBW ≈ 35.9 Hz
98 * - GYRO_DLPFCFG_6 : 3dB ≈ 5.7 Hz, NBW ≈ 8.3 Hz
99 * - GYRO_DLPFCFG_7 : 3dB ≈ 473 Hz, NBW ≈ 499 Hz
100 *
101 * FCHOICE=1 (Bypass) : very wide (use with care; highest noise)
102 */
103 if (!imu.GyroConfig(/*DLPFCFG*/ GYRO_DLPFCFG_4,
104 /*FS_SEL*/ dps2000,
105 /*FCHOICE*/ true, // DLPF ON (recommended)
106 /*X*/ true, /*Y*/ true, /*Z*/ true,
107 /*AVGCFG*/ 0)) {
108 Serial.println(F("GyroConfig failed."));
109 }
110
111 /* Set gyroscope output data rate (ODR)
112 * - Gyro_SMPLRT(rate_hz)
113 * - Base (DLPF path) = 1100 Hz
114 * - Valid range: ~4.3–1100 Hz
115 * - Example: 1000 Hz
116 */
117 if (!imu.Gyro_SMPLRT(1000)) {
118 Serial.println(F("Gyro_SMPLRT failed."));
119 }
120}
121
122/**
123 * @brief Read and print gyroscope data in degrees per second.
124 */
125void loop() {
126 float gx, gy, gz; // gyro X/Y/Z in dps
127
128 /* - readGyro(x,y,z)
129 * - Returns:
130 * - true on success;
131 * - Output:
132 * - gx/gy/gz in dps
133 */
134 if (imu.readGyro(gx, gy, gz)) {
135 Serial.print("GYRO [dps]: ");
136 Serial.print(gx, 3);
137 Serial.print(", ");
138 Serial.print(gy, 3);
139 Serial.print(", ");
140 Serial.println(gz, 3);
141 } else {
142 Serial.println(F("GYRO read failed"));
143 }
144
145 Serial.println(F("-----------------------------"));
146 delay(500);
147}