DevLab_ICM20948 1.0.2
ICM-20948 9-Axis Motion Sensor Driver
Loading...
Searching...
No Matches
spi_magneto.ino
Go to the documentation of this file.
1/***************************************************************
2 * @file I2C_Magneto.ino
3 * @author Jonathan Mejorado Lopez
4 * @brief Minimal I2C bring-up for DevLab ICM-20948 +
5 * AK09916 magnetometer readout (updated API).
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(SCL) -> D13
19 * - MOSI(SDA) -> D11
20 * - MISO(SD0/AD0) -> D12
21 * - CS(nCS) -> 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 * License : MIT
32 ***************************************************************/
33
34#include <DevLab_ICM20948.h>
35
36/* ====================== User Config ======================= */
37/** @brief Chip-select pin for SPI. */
38#define CS_PIN D10
39
40/** @brief SPI clock used during sensor initialization and reads. */
41
42#define SPI_FAST_SPEED 1000000
43
44/** @brief Global ICM-20948 driver instance. */
46
47/** @brief SPI bus object used by the IMU driver. */
48SPIClass spi_bus(SPI);
49
50
51/**
52 * @brief Configure I2C, verify the IMU, and initialize the magnetometer.
53 *
54 * The magnetometer is accessed through the ICM-20948 internal I2C master, so
55 * initMag() must succeed before loop() can read magnetic field data.
56 */
57void setup() {
58 Serial.begin(115200);
59 delay(200);
60 Serial.println(F("ICM-20948 (SPI) — Magnetometer Example"));
61
63 Serial.println("ERROR: beginSPI() failed");
64 while (1) delay(200);
65 }
66
67 Serial.println(F("ICM-20948 ready (SPI)."));
68
69 if (!imu.initMag()) {
70 Serial.println(F("Mag init failed"));
71 } else {
72 Serial.println(F("Mag initialized"));
73 }
74 Serial.println("Ready.");
75
76 Serial.println(F("Magnetometer ready"));
77}
78
79/**
80 * @brief Read and print magnetometer data in microtesla.
81 */
82void loop() {
83 float mx, my, mz;
84
85 /* Read magnetometer data
86 * - Output in microtesla (uT)
87 * - Returns true on success
88 */
89 if (imu.readMag(mx, my, mz)) {
90 Serial.print(F("MAG [uT]: "));
91 Serial.print(mx, 2); Serial.print(F(", "));
92 Serial.print(my, 2); Serial.print(F(", "));
93 Serial.println(mz, 2);
94 } else {
95 Serial.println(F("Mag read failed"));
96 }
97
98 Serial.println(F("-----------------------------"));
99 delay(500);
100}
bool beginSPI(uint8_t csPin, SPIClass &spiPort=SPI, uint32_t spiSpeed=1000000)
bool readMag(float &x, float &y, float &z)
void setup()
Configure I2C, verify the IMU, and initialize the magnetometer.
SPIClass spi_bus(SPI)
SPI bus object used by the IMU driver.
#define SPI_FAST_SPEED
SPI clock used during sensor initialization and reads.
DevLab_ICM20948 imu
Global ICM-20948 driver instance.
#define CS_PIN
Chip-select pin for SPI.
void loop()
Read and print magnetometer data in microtesla.