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. */
41 DevLab_ICM20948 imu;
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 */
52 void setup() {
53 Serial.begin(115200);
54 delay(200);
55
56
57 if (!imu.beginSPI(CS_PIN, spi_bus, SPI_FAST_SPEED)) {
58 Serial.println("ERROR: beginSPI() failed");
59 while (1) delay(200);
60 }
61
62 imu.setSensors(true, true, true);
63 Serial.println("Ready.");
64}
65
66/**
67 * @brief Read and print accelerometer, gyroscope, and temperature data.
68 */
69 void loop() {
70 float ax, ay, az;
71 float gx, gy, gz;
72 float tC;
73
74 /* Read accelerometer. */
75 if (imu.readAccel(ax, ay, az)) {
76 Serial.print(F("ACC [g]: "));
77 Serial.print(ax, 3); Serial.print(", ");
78 Serial.print(ay, 3); Serial.print(", ");
79 Serial.println(az, 3);
80 } else {
81 Serial.println(F("ACC read failed"));
82 }
83
84 /* Read gyroscope. */
85 if (imu.readGyro(gx, gy, gz)) {
86 Serial.print(F("GYR [dps]: "));
87 Serial.print(gx, 2); Serial.print(", ");
88 Serial.print(gy, 2); Serial.print(", ");
89 Serial.println(gz, 2);
90 } else {
91 Serial.println(F("GYR read failed"));
92 }
93
94 /* Read temperature. */
95 if (imu.readTemperature(tC)) {
96 Serial.print(F("TMP [C]: "));
97 Serial.println(tC, 2);
98 } else {
99 Serial.println(F("TMP read failed"));
100 }
101
102 Serial.println(F("-----------------------------"));
103 delay(500);
104}
DevLab_ICM20948.h
DevLab_ICM20948
Definition DevLab_ICM20948.h:173
DevLab_ICM20948::readGyro
bool readGyro(float &x, float &y, float &z)
Definition DevLab_ICM20948.cpp:258
DevLab_ICM20948::readAccel
bool readAccel(float &x, float &y, float &z)
Definition DevLab_ICM20948.cpp:238
DevLab_ICM20948::beginSPI
bool beginSPI(uint8_t csPin, SPIClass &spiPort=SPI, uint32_t spiSpeed=1000000)
Definition DevLab_ICM20948.cpp:60
DevLab_ICM20948::readTemperature
bool readTemperature(float &temperature)
Definition DevLab_ICM20948.cpp:278
DevLab_ICM20948::setSensors
bool setSensors(bool accel_on, bool gyro_on, bool temp_on)
Definition DevLab_ICM20948.cpp:1006
setup
void setup()
Initialize Serial and start the ICM-20948 over SPI.
Definition spi_basic.ino:52
spi_bus
SPIClass spi_bus(SPI)
SPI bus object used by the IMU driver.
SPI_FAST_SPEED
#define SPI_FAST_SPEED
SPI clock used during sensor initialization and reads.
Definition spi_basic.ino:38
imu
DevLab_ICM20948 imu
Global ICM-20948 driver instance.
Definition spi_basic.ino:41
CS_PIN
#define CS_PIN
Chip-select pin for SPI.
Definition spi_basic.ino:35
loop
void loop()
Read and print accelerometer, gyroscope, and temperature data.
Definition spi_basic.ino:69