DevLab_ICM20948 1.0.0
ICM-20948 9-Axis Motion Sensor Driver
Loading...
Searching...
No Matches
spi_basic.ino
Go to the documentation of this file.
1/***************************************************************
2 * @file SPI_Basic.ino
3 * @brief Minimal SPI bring-up for 7Semi ICM-20948 +
4 * basic Accel/Gyro/Temp readout (updated API).
5 *
6 * Features
7 * - SPI init (UNO / ESP32)
8 * - beginSPI() initialization
9 * - Manual sensor enable
10 * - Read Accel / Gyro / Temp
11 *
12 * Notes
13 * - WHO_AM_I must be 0xEA
14 * - Sensors must be explicitly enabled
15 * - SPI speed ~1MHz recommended during init
16 *
17 * Wiring (Arduino UNO SPI)
18 * - SCK -> D13
19 * - MOSI -> D11
20 * - MISO -> D12
21 * - CS -> D10 (changeable; update CS_PIN)
22 * - VCC -> 3V3
23 * - GND -> GND
24 *
25 * Wiring (ESP32 VSPI default)
26 * - SCK -> D13
27 * - MOSI -> D11
28 * - MISO -> D12
29 * - CS -> D10
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/** @brief Global ICM-20948 driver instance. */
37DevLab_ICM20948 imu;
38
39/** @brief SPI bus object used by the IMU driver. */
40SPIClass spi_bus(SPI);
41
42/**
43 * @brief Initialize Serial and start the ICM-20948 over SPI.
44 *
45 * @note The sketch expects a board-level CS_PIN definition to select the
46 * chip-select pin used by beginSPI().
47 */
48void setup() {
49 Serial.begin(115200);
50 delay(200);
51
52
53 if (!imu.beginSPI(CS_PIN, spi_bus, 1000000)) {
54 Serial.println("ERROR: beginSPI() failed");
55 while (1) delay(200);
56 }
57
58 imu.setSensors(true, true, true);
59 Serial.println("Ready.");
60}
61
62/**
63 * @brief Read and print accelerometer, gyroscope, and temperature data.
64 */
65void loop() {
66 float ax, ay, az;
67 float gx, gy, gz;
68 float tC;
69
70 /* Read accelerometer. */
71 if (imu.readAccel(ax, ay, az)) {
72 Serial.print(F("ACC [g]: "));
73 Serial.print(ax, 3); Serial.print(", ");
74 Serial.print(ay, 3); Serial.print(", ");
75 Serial.println(az, 3);
76 } else {
77 Serial.println(F("ACC read failed"));
78 }
79
80 /* Read gyroscope. */
81 if (imu.readGyro(gx, gy, gz)) {
82 Serial.print(F("GYR [dps]: "));
83 Serial.print(gx, 2); Serial.print(", ");
84 Serial.print(gy, 2); Serial.print(", ");
85 Serial.println(gz, 2);
86 } else {
87 Serial.println(F("GYR read failed"));
88 }
89
90 /* Read temperature. */
91 if (imu.readTemperature(tC)) {
92 Serial.print(F("TMP [C]: "));
93 Serial.println(tC, 2);
94 } else {
95 Serial.println(F("TMP read failed"));
96 }
97
98 Serial.println(F("-----------------------------"));
99 delay(500);
100}