DevLab_ICM20948 1.0.0 ICM-20948 9-Axis Motion Sensor Driver

Loading...
Searching...
No Matches
spi_gyro.ino
Go to the documentation of this file.
1/***************************************************************
2 * @file ICM20948_SPI_Gyro.ino
3 * @brief Minimal SPI bring-up for 7Semi ICM-20948 on ESP32/UNO +
4 * continuous gyroscope readout.
5 *
6 * Features
7 * - SPI init on custom pins
8 * - IMU begin() on SPI (CS pin user-defined)
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)
15 * - SCK -> GPIO13
16 * - MOSI(SDA) -> GPIO11
17 * - MISO(SDO) -> GPIO12
18 * - CS -> GPIO10 (changeable)
19 * - VCC -> 3V3
20 * - GND -> GND
21 * Wiring (ESP32 VSPI default)
22 * - SCK -> GPIO18
23 * - MOSI -> GPIO23
24 * - MISO -> GPIO19
25 * - CS -> GPIO5 (changeable)
26 * - VCC -> 3V3
27 * - GND -> GND
28 ***************************************************************/
29
30#include <DevLab_ICM20948.h>
31
32/** @brief SPI clock used during sensor initialization and reads. */
33#define SPI_FAST_SPEED 1000000
34
35/** @brief Global ICM-20948 driver instance. */
36DevLab_ICM20948 imu;
37
38/** @brief SPI bus object used by the IMU driver. */
39SPIClass spi_bus(SPI);
40
41/**
42 * @brief Configure the IMU for gyroscope-only operation over SPI.
43 *
44 * @note The sketch expects a board-level CS_PIN definition to select the
45 * chip-select pin used by beginSPI().
46 */
47void setup() {
48 Serial.begin(115200);
49 delay(200);
50 Serial.println(F("ICM-20948 (SPI) — Gyro Example"));
51
52 /* Init SPI bus on your pins. */
53 // SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, CS_PIN);
54
55 if (!imu.beginSPI(CS_PIN, spi_bus, 1000000)) {
56 Serial.println("ERROR: beginSPI() failed");
57 while (1) delay(200);
58 }
59
60 /* Apply safe defaults. */
61 if (!imu.applyBasicDefaults()) {
62 Serial.println(F("ERROR: applyBasicDefaults() failed."));
63 while (1) delay(200);
64 }
65
66 Serial.println(F("ICM-20948 ready."));
67
68 /* Optional: gate sensors
69 * - setSensors(accel_on, gyro_on, temp_on)
70 * - Here: enable only gyro (accel/temp off)
71 */
72 if (!imu.setSensors(/*accel*/ false, /*gyro*/ true, /*temp*/ false)) {
73 Serial.println(F("setSensors failed."));
74 }
75
76 /* ----------------- GYRO DLPF Config (reference) ----------------
77 * GyroConfig(DLPFCFG, FS_SEL, FCHOICE, XGYRO, YGYRO, ZGYRO, AVGCFG)
78 * - DLPF : GYRO_DLPFCFG_0..7 (use 3 for balanced setting)
79 * - FS_SEL : dps250 / dps500 / dps1000 / dps2000
80 * - FCHOICE : false → DLPF path (recommended), true → bypass (very wide BW)
81 * - XGYRO/YGYRO/ZGYRO : per-axis enable (true = enable axis)
82 * - AVGCFG : 0..7 internal averaging (higher = more smoothing, more delay)
83 * FCHOICE=0 (DLPF on):
84 * - GYRO_DLPFCFG_0 : 3dB ≈ 12106 Hz, NBW ≈ 12316 Hz (very wide)
85 * - GYRO_DLPFCFG_1 : 3dB ≈ 196.6 Hz, NBW ≈ 229.8 Hz
86 * - GYRO_DLPFCFG_2 : 3dB ≈ 151.8 Hz, NBW ≈ 187.6 Hz
87 * - GYRO_DLPFCFG_3 : 3dB ≈ 119.5 Hz, NBW ≈ 154.3 Hz ← good default
88 * - GYRO_DLPFCFG_4 : 3dB ≈ 51.2 Hz, NBW ≈ 73.3 Hz
89 * - GYRO_DLPFCFG_5 : 3dB ≈ 23.9 Hz, NBW ≈ 35.9 Hz
90 * - GYRO_DLPFCFG_6 : 3dB ≈ 5.7 Hz, NBW ≈ 8.3 Hz
91 * - GYRO_DLPFCFG_7 : 3dB ≈ 473 Hz, NBW ≈ 499 Hz
92 *
93 * FCHOICE=1 (Bypass) : very wide (use with care; highest noise)
94 */
95 if (!imu.GyroConfig(/*DLPFCFG*/ GYRO_DLPFCFG_4,
96 /*FS_SEL*/ dps2000,
97 /*FCHOICE*/ true, // DLPF ON
98 /*X*/ true, /*Y*/ true, /*Z*/ true,
99 /*AVGCFG*/ 0)) {
100 Serial.println(F("GyroConfig failed."));
101 }
102
103 /* Set gyroscope output data rate (ODR)
104 * - Gyro_SMPLRT(rate_hz)
105 * - Base (DLPF path) = 1100 Hz
106 * - Valid range: ~4.3–1100 Hz
107 * - Example: 1000 Hz
108 */
109 if (!imu.Gyro_SMPLRT(1000)) {
110 Serial.println(F("Gyro_SMPLRT failed."));
111 }
112}
113
114/**
115 * @brief Read and print gyroscope data in degrees per second.
116 */
117void loop() {
118 float gx, gy, gz; // gyro X/Y/Z in dps
119
120 /* - readGyro(x,y,z)
121 * - Returns:
122 * - true on success;
123 * - Output:
124 * - gx/gy/gz in dps
125 */
126 if (imu.readGyro(gx, gy, gz)) {
127 Serial.print("GYRO [dps]: ");
128 Serial.print(gx, 3);
129 Serial.print(", ");
130 Serial.print(gy, 3);
131 Serial.print(", ");
132 Serial.println(gz, 3);
133
134 } else {
135 Serial.println(F("GYRO read failed"));
136 }
137
138 Serial.println(F("-----------------------------"));
139 delay(500);
140}