DevLab_ICM20948 1.0.0 ICM-20948 9-Axis Motion Sensor Driver

Loading...
Searching...
No Matches
spi_accel.ino
Go to the documentation of this file.
1/***************************************************************
2 * @file SPI_Accel.ino
3 * @brief Minimal SPI bring-up for 7Semi ICM-20948 on ESP32 +
4 * continuous accelerometer readout.
5 *
6 * Features
7 * - SPI init on custom pins (ESP32 VSPI)
8 * - IMU begin() on SPI (CS pin user-defined)
9 * - Safe defaults (applyBasicDefaults)
10 * - Optional sensor gating: accel only
11 * - Accel config: DLPF, full-scale, ODR, DEC3
12 * - Read accel (g) at ~2 Hz print
13 *
14* Wiring (Arduino UNO)
15 * - SCK -> GPIO13
16 * - MOSI(SDA) -> GPIO12
17 * - MISO(SDO) -> GPIO11
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); // usa el bus SPI por defecto del Arduino
40
41/**
42 * @brief Configure the IMU for accelerometer-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
51
52 if (!imu.beginSPI(CS_PIN, spi_bus, 1000000)) {
53 Serial.println("ERROR: beginSPI() failed");
54 while (1) delay(200);
55 }
56
57
58 Serial.println(F("ICM-20948 ready."));
59
60 /* Optional: gate sensors
61 * - setSensors(accel_on, gyro_on, temp_on)
62 * - Here: enable only accel (gyro/temp off)
63 */
64 if (!imu.setSensors(/*accel*/ true, /*gyro*/ false, /*temp*/ false)) {
65 Serial.println(F("setSensors failed."));
66 }
67 /* ---------------- ACCEL DLPF Config (reference) ---------------
68 * ACCEL_DLPFCFG (0..7) — 3dB & Noise Bandwidth (datasheet)
69 * DLPF path ODR = 1125 / (1 + ACCEL_SMPLRT_DIV), DIV: 0..4095
70 *
71 * FCHOICE=0 (Bypass) : 3dB ≈ 1209 Hz, NBW ≈ 1248 Hz, Rate ≈ 4500 Hz
72 * FCHOICE=1 (DLPF on):
73 * - ACCEL_DLPFCFG_0 : 3dB ≈ 246.0 Hz, NBW ≈ 265.0 Hz
74 * - ACCEL_DLPFCFG_1 : 3dB ≈ 246.0 Hz, NBW ≈ 265.0 Hz
75 * - ACCEL_DLPFCFG_2 : 3dB ≈ 111.4 Hz, NBW ≈ 136.0 Hz
76 * - ACCEL_DLPFCFG_3 : 3dB ≈ 50.4 Hz, NBW ≈ 68.8 Hz ← good default
77 * - ACCEL_DLPFCFG_4 : 3dB ≈ 23.9 Hz, NBW ≈ 34.4 Hz
78 * - ACCEL_DLPFCFG_5 : 3dB ≈ 11.5 Hz, NBW ≈ 17.0 Hz
79 * - ACCEL_DLPFCFG_6 : 3dB ≈ 5.7 Hz, NBW ≈ 8.3 Hz
80 * - ACCEL_DLPFCFG_7 : 3dB ≈ 473 Hz, NBW ≈ 499 Hz
81 */
82
83 /* Configure accelerometer
84 * AccelConfigure(DLPF, FS_SEL, dlpf_enable, dec3, stX, stY, stZ)
85 * - DLPF : ACCEL_DLPFCFG_0..7 (use 3 for balanced setting)
86 * - FS_SEL : 0..3 => ±2/±4/±8/±16 g (g2=0, g4=1, g8=2, g16=3)
87 * - dlpf_enable: true to use DLPF path (recommended)
88 * - dec3 : 0..3 (DEC3_CFG averaging)
89 * 0 = ACCEL_DEC3_AVG_1_OR_4 (depends on FCHOICE)
90 * 1 = ACCEL_DEC3_AVG_8
91 * 2 = ACCEL_DEC3_AVG_16
92 * 3 = ACCEL_DEC3_AVG_32
93 * - stX/Y/Z : enable self-test (false here)
94 */
95 if (!imu.AccelConfigure(ACCEL_DLPFCFG_3, /*FS_SEL*/ g4,
96 /*dlpf_enable*/ true, // DLPF really ON
97 /*dec3*/ ACCEL_DEC3_AVG_8,
98 /*stX*/ false, /*stY*/ false, /*stZ*/ false)) {
99 Serial.println(F("AccelConfigure failed."));
100 }
101
102 /* Set accelerometer output data rate (ODR)
103 * - Accel_SMPLRT(rate_hz)
104 * - Base (DLPF path) = 1125 Hz
105 * - Valid range: 1–1125 Hz
106 * - Example: 225 Hz
107 */
108 if (!imu.Accel_SMPLRT(225)) {
109 Serial.println(F("Accel_SMPLRT failed."));
110 }
111}
112
113/**
114 * @brief Read and print accelerometer data in g.
115 */
116void loop() {
117 float ax, ay, az; // accel X/Y/Z in g
118
119 /* - readAccel(x,y,z)
120 * - Returns:
121 * - true on success;
122 * - Output:
123 * - ax/ay/az in g
124 */
125 if (imu.readAccel(ax, ay, az)) {
126 Serial.print("ACCEL [g]: ");
127 Serial.print(ax, 3);
128 Serial.print(", ");
129 Serial.print(ay, 3);
130 Serial.print(", ");
131 Serial.println(az, 3);
132 } else {
133 Serial.println(F("ACCEL read failed"));
134 }
135
136 Serial.println(F("-----------------------------"));
137 delay(500);
138}