1/***************************************************************
3 * @brief Minimal SPI bring-up for 7Semi ICM-20948 on ESP32 +
4 * continuous accelerometer readout.
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
16 * - MOSI(SDA) -> GPIO12
17 * - MISO(SDO) -> GPIO11
18 * - CS -> GPIO10 (changeable)
21 * Wiring (ESP32 VSPI default)
25 * - CS -> GPIO5 (changeable)
28 ***************************************************************/
30#include <DevLab_ICM20948.h>
32/** @brief SPI clock used during sensor initialization and reads. */
33#define SPI_FAST_SPEED 1000000
35/** @brief Global ICM-20948 driver instance. */
38/** @brief SPI bus object used by the IMU driver. */
39SPIClass spi_bus(SPI); // usa el bus SPI por defecto del Arduino
42 * @brief Configure the IMU for accelerometer-only operation over SPI.
44 * @note The sketch expects a board-level CS_PIN definition to select the
45 * chip-select pin used by beginSPI().
52 if (!imu.beginSPI(CS_PIN, spi_bus, 1000000)) {
53 Serial.println("ERROR: beginSPI() failed");
58 Serial.println(F("ICM-20948 ready."));
60 /* Optional: gate sensors
61 * - setSensors(accel_on, gyro_on, temp_on)
62 * - Here: enable only accel (gyro/temp off)
64 if (!imu.setSensors(/*accel*/ true, /*gyro*/ false, /*temp*/ false)) {
65 Serial.println(F("setSensors failed."));
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
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
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)
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."));
102 /* Set accelerometer output data rate (ODR)
103 * - Accel_SMPLRT(rate_hz)
104 * - Base (DLPF path) = 1125 Hz
105 * - Valid range: 1–1125 Hz
108 if (!imu.Accel_SMPLRT(225)) {
109 Serial.println(F("Accel_SMPLRT failed."));
114 * @brief Read and print accelerometer data in g.
117 float ax, ay, az; // accel X/Y/Z in g
119 /* - readAccel(x,y,z)
125 if (imu.readAccel(ax, ay, az)) {
126 Serial.print("ACCEL [g]: ");
131 Serial.println(az, 3);
133 Serial.println(F("ACCEL read failed"));
136 Serial.println(F("-----------------------------"));