DevLab_ICM20948 1.0.2
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 * @author 7Semi,Jonathan Mejorado Lopez
4 * @brief Minimal SPI bring-up for 7Semi ICM-20948 on ESP32 +
5 * continuous accelerometer readout.
6 *
7 * Features
8 * - SPI init on custom pins (ESP32 VSPI)
9 * - IMU begin() on SPI (CS pin user-defined)
10 * - Safe defaults (applyBasicDefaults)
11 * - Optional sensor gating: accel only
12 * - Accel config: DLPF, full-scale, ODR, DEC3
13 * - Read accel (g) at ~2 Hz print
14 *
15* Wiring (Arduino UNO)
16 * - SCK -> GPIO13
17 * - MOSI(SDA) -> GPIO12
18 * - MISO(SDO) -> GPIO11
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/** @brief SPI clock used during sensor initialization and reads. */
34#define SPI_FAST_SPEED 1000000
35
36#define CS_PIN D10 // Chip-select pin for SPI (change as needed)
37/** @brief Global ICM-20948 driver instance. */
39
40/** @brief SPI bus object used by the IMU driver. */
41SPIClass spi_bus(SPI); // usa el bus SPI por defecto del Arduino
42
43/**
44 * @brief Configure the IMU for accelerometer-only operation over SPI.
45 *
46 * @note The sketch expects a board-level CS_PIN definition to select the
47 * chip-select pin used by beginSPI().
48 */
49void setup() {
50 Serial.begin(115200);
51 delay(200);
52
53
54 if (!imu.beginSPI(CS_PIN, spi_bus, 1000000)) {
55 Serial.println("ERROR: beginSPI() failed");
56 while (1) delay(200);
57 }
58
59
60 Serial.println(F("ICM-20948 ready."));
61
62 /* Optional: gate sensors
63 * - setSensors(accel_on, gyro_on, temp_on)
64 * - Here: enable only accel (gyro/temp off)
65 */
66 if (!imu.setSensors(/*accel*/ true, /*gyro*/ false, /*temp*/ false)) {
67 Serial.println(F("setSensors failed."));
68 }
69 /* ---------------- ACCEL DLPF Config (reference) ---------------
70 * ACCEL_DLPFCFG (0..7) — 3dB & Noise Bandwidth (datasheet)
71 * DLPF path ODR = 1125 / (1 + ACCEL_SMPLRT_DIV), DIV: 0..4095
72 *
73 * FCHOICE=0 (Bypass) : 3dB ≈ 1209 Hz, NBW ≈ 1248 Hz, Rate ≈ 4500 Hz
74 * FCHOICE=1 (DLPF on):
75 * - ACCEL_DLPFCFG_0 : 3dB ≈ 246.0 Hz, NBW ≈ 265.0 Hz
76 * - ACCEL_DLPFCFG_1 : 3dB ≈ 246.0 Hz, NBW ≈ 265.0 Hz
77 * - ACCEL_DLPFCFG_2 : 3dB ≈ 111.4 Hz, NBW ≈ 136.0 Hz
78 * - ACCEL_DLPFCFG_3 : 3dB ≈ 50.4 Hz, NBW ≈ 68.8 Hz ← good default
79 * - ACCEL_DLPFCFG_4 : 3dB ≈ 23.9 Hz, NBW ≈ 34.4 Hz
80 * - ACCEL_DLPFCFG_5 : 3dB ≈ 11.5 Hz, NBW ≈ 17.0 Hz
81 * - ACCEL_DLPFCFG_6 : 3dB ≈ 5.7 Hz, NBW ≈ 8.3 Hz
82 * - ACCEL_DLPFCFG_7 : 3dB ≈ 473 Hz, NBW ≈ 499 Hz
83 */
84
85 /* Configure accelerometer
86 * AccelConfigure(DLPF, FS_SEL, dlpf_enable, dec3, stX, stY, stZ)
87 * - DLPF : ACCEL_DLPFCFG_0..7 (use 3 for balanced setting)
88 * - FS_SEL : 0..3 => ±2/±4/±8/±16 g (g2=0, g4=1, g8=2, g16=3)
89 * - dlpf_enable: true to use DLPF path (recommended)
90 * - dec3 : 0..3 (DEC3_CFG averaging)
91 * 0 = ACCEL_DEC3_AVG_1_OR_4 (depends on FCHOICE)
92 * 1 = ACCEL_DEC3_AVG_8
93 * 2 = ACCEL_DEC3_AVG_16
94 * 3 = ACCEL_DEC3_AVG_32
95 * - stX/Y/Z : enable self-test (false here)
96 */
97 if(!imu.setAccelDLPF(ACCEL_DLPFCFG_3,false)) {
98 Serial.println(F("setAccelDLPF failed."));
99 }
100
102 Serial.println(F("setAccelFS failed."));
103 }
105 Serial.println(F("setAccelAveraging failed."));
106 }
107
108 /* Set accelerometer output data rate (ODR)
109 * - Accel_SMPLRT(rate_hz)
110 * - Base (DLPF path) = 1125 Hz
111 * - Valid range: 1–1125 Hz
112 * - Example: 225 Hz
113 */
114 if (!imu.setAccelDivRate(4)) { // 1125 / (1 + 4) = 225 Hz
115 Serial.println(F("Accel_SMPLRT failed."));
116 }
117}
118
119/**
120 * @brief Read and print accelerometer data in g.
121 */
122void loop() {
123 float ax, ay, az; // accel X/Y/Z in g
124
125 /* - readAccel(x,y,z)
126 * - Returns:
127 * - true on success;
128 * - Output:
129 * - ax/ay/az in g
130 */
131 if (imu.readAccel(ax, ay, az)) {
132 Serial.print("ACCEL [g]: ");
133 Serial.print(ax, 3);
134 Serial.print(", ");
135 Serial.print(ay, 3);
136 Serial.print(", ");
137 Serial.println(az, 3);
138 } else {
139 Serial.println(F("ACCEL read failed"));
140 }
141
142 Serial.println(F("-----------------------------"));
143 delay(500);
144}
#define ACCEL_DLPFCFG_3
bool setAccelScale(ICM20948_Accel_FullScale fullScale)
bool readAccel(float &x, float &y, float &z)
bool beginSPI(uint8_t csPin, SPIClass &spiPort=SPI, uint32_t spiSpeed=1000000)
bool setAccelAveraging(ICM20948_Accel_Average avg)
bool setAccelDLPF(uint8_t dlpf, bool bypass)
bool setAccelDivRate(uint16_t divisor)
bool setSensors(bool accel_on, bool gyro_on, bool temp_on)
void setup()
Configure the IMU for accelerometer-only operation over SPI.
Definition spi_accel.ino:49
SPIClass spi_bus(SPI)
SPI bus object used by the IMU driver.
DevLab_ICM20948 imu
Global ICM-20948 driver instance.
Definition spi_accel.ino:38
#define CS_PIN
Definition spi_accel.ino:36
void loop()
Read and print accelerometer data in g.