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