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