DevLab_ICM20948 1.0.2
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 * @author 7Semi,Jonathan Mejorado Lopez
4 * @brief Minimal SPI bring-up for 7Semi ICM-20948 +
5 * basic Accel/Gyro/Temp readout (updated API).
6 *
7 * Features
8 * - SPI init (UNO / ESP32)
9 * - beginSPI() initialization
10 * - Manual sensor enable
11 * - Read Accel / Gyro / Temp
12 *
13 * Notes
14 * - WHO_AM_I must be 0xEA
15 * - Sensors must be explicitly enabled
16 * - SPI speed ~1MHz recommended during init
17 *
18 * Wiring (Arduino UNO SPI)
19 * - SCK(SCL) -> D13
20 * - MOSI(SDA) -> D11
21 * - MISO(SD0/AD0) -> D12
22 * - CS(nCS) -> D10 (changeable; update CS_PIN)
23 * - VCC -> 3V3
24 * - GND -> GND
25 *
26 * Wiring (ESP32 VSPI default)
27 * - SCK -> D13
28 * - MOSI -> D11
29 * - MISO -> D12
30 * - CS -> D10
31 ***************************************************************/
32#include <DevLab_ICM20948.h>
33/** @brief Chip-select pin for SPI. */
34
35#define CS_PIN D10
36/** @brief SPI clock used during sensor initialization and reads. */
37
38#define SPI_FAST_SPEED 1000000
39
40/** @brief Global ICM-20948 driver instance. */
42
43/** @brief SPI bus object used by the IMU driver. */
44SPIClass spi_bus(SPI);
45
46/**
47 * @brief Initialize Serial and start the ICM-20948 over SPI.
48 *
49 * @note The sketch expects a board-level CS_PIN definition to select the
50 * chip-select pin used by beginSPI().
51 */
52void setup() {
53 Serial.begin(115200);
54 delay(200);
55
56
58 Serial.println("ERROR: beginSPI() failed");
59 while (1) delay(200);
60 }
61
62 if (!imu.setSensors(true, true, true)) {
63 Serial.println(F("ERROR: setSensors failed"));
64 }
65 /* Initialize magnetometer. */
66 if (!imu.initMag()) {
67 Serial.println(F("Mag init failed"));
68 } else {
69 Serial.println(F("Mag initialized"));
70 }
71 Serial.println("Ready.");
72}
73
74/**
75 * @brief Read and print accelerometer, gyroscope, and temperature data.
76 */
77void loop() {
78 float ax, ay, az;
79 float gx, gy, gz;
80 float mx, my, mz;
81 float tC;
82
83 /* Read accelerometer. */
84 if (imu.readAccel(ax, ay, az)) {
85 Serial.print(F("ACC [g]: "));
86 Serial.print(ax, 3); Serial.print(", ");
87 Serial.print(ay, 3); Serial.print(", ");
88 Serial.println(az, 3);
89 } else {
90 Serial.println(F("ACC read failed"));
91 }
92
93 /* Read gyroscope. */
94 if (imu.readGyro(gx, gy, gz)) {
95 Serial.print(F("GYR [dps]: "));
96 Serial.print(gx, 2); Serial.print(", ");
97 Serial.print(gy, 2); Serial.print(", ");
98 Serial.println(gz, 2);
99 } else {
100 Serial.println(F("GYR read failed"));
101 }
102
103 /* Read magnetometer. */
104 if (imu.readMag(mx, my, mz)) {
105 Serial.print(F("MAG [uT]: "));
106 Serial.print(mx, 2); Serial.print(", ");
107 Serial.print(my, 2); Serial.print(", ");
108 Serial.println(mz, 2);
109 } else {
110 Serial.println(F("MAG read failed"));
111 }
112
113 /* Read temperature. */
114 if (imu.readTemperature(tC)) {
115 Serial.print(F("TMP [C]: "));
116 Serial.println(tC, 2);
117 } else {
118 Serial.println(F("TMP read failed"));
119 }
120
121 Serial.println(F("-----------------------------"));
122 delay(500);
123}
bool readGyro(float &x, float &y, float &z)
bool readAccel(float &x, float &y, float &z)
bool beginSPI(uint8_t csPin, SPIClass &spiPort=SPI, uint32_t spiSpeed=1000000)
bool readMag(float &x, float &y, float &z)
bool readTemperature(float &temperature)
bool setSensors(bool accel_on, bool gyro_on, bool temp_on)
void setup()
Initialize Serial and start the ICM-20948 over SPI.
Definition spi_basic.ino:52
SPIClass spi_bus(SPI)
SPI bus object used by the IMU driver.
#define SPI_FAST_SPEED
SPI clock used during sensor initialization and reads.
Definition spi_basic.ino:38
DevLab_ICM20948 imu
Global ICM-20948 driver instance.
Definition spi_basic.ino:41
#define CS_PIN
Chip-select pin for SPI.
Definition spi_basic.ino:35
void loop()
Read and print accelerometer, gyroscope, and temperature data.
Definition spi_basic.ino:77