DevLab_ICM20948 1.0.0 ICM-20948 9-Axis Motion Sensor Driver

Loading...
Searching...
No Matches
i2c_accel.ino
Go to the documentation of this file.
1/***************************************************************
2 * @file I2C_Accel.ino
3 * @brief Minimal I2C bring-up for 7Semi ICM-20948 on UNO/ESP32 +
4 * continuous accelerometer readout
5 *
6 * Features
7 * - I2C init on default/custom pins
8 * - IMU beginI2C() on I2C (addr 0x68/0x69)
9 * - Manual sensor enable (setSensors)
10 * - Accel config: scale, DLPF, averaging, sample rate
11 * - Read accel (g) at ~2 Hz print
12 *
13 * Notes
14 * - WHO_AM_I must be 0xEA
15 * - Sensors must be explicitly enabled
16 * - Bank switching handled internally by library
17 *
18 * Wiring (Arduino UNO I2C)
19 * - SDA -> A4
20 * - SCL -> A5
21 * - VCC -> 3V3 (or 5V if your board supports it)
22 * - GND -> GND
23 *
24 * Wiring (ESP32 default I2C)
25 * - SDA -> GPIO21
26 * - SCL -> GPIO22
27 * - VCC -> 3V3
28 * - GND -> GND
29 ***************************************************************/
30
31#include <Wire.h>
32#include <DevLab_ICM20948.h>
33
34/* ====================== User Config ======================= */
35/** @brief I2C SDA pin used by the example board. */
36#define SDA_PIN 6
37/** @brief I2C SCL pin used by the example board. */
38#define SCL_PIN 7
39/** @brief I2C bus speed in hertz. */
40#define I2C_FREQ 400000UL
41/** @brief ICM-20948 I2C address selected by the AD0 pin. */
42#define ICM_ADDR 0x69
43
44/** @brief Global ICM-20948 driver instance. */
45DevLab_ICM20948 imu;
46
47/**
48 * @brief Configure the IMU for accelerometer-only operation over I2C.
49 *
50 * The function initializes the bus, verifies the device identity, enables the
51 * accelerometer, and applies range, DLPF, averaging, and sample-rate settings.
52 */
53void setup() {
54 Serial.begin(115200);
55 delay(200);
56 Serial.println(F("ICM-20948 (I2C) — Accel Example"));
57 Wire.begin(SDA_PIN, SCL_PIN);
58 /* Initialize IMU using I2C. */
59 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
60 Serial.println(F("ERROR: ICM-20948 beginI2C() failed."));
61 while (1) delay(200);
62 }
63
64 Serial.println(F("ICM-20948 ready."));
65
66 /* Verify device identity. */
67 uint8_t who;
68 if (imu.readWhoAmI(who)) {
69 Serial.print("WHO_AM_I: 0x");
70 Serial.println(who, HEX);
71 }
72
73 /* Enable only accelerometer
74 * - accel = true
75 * - gyro = false
76 * - temp = false
77 */
78 if (!imu.setSensors(true, false, false)) {
79 Serial.println(F("setSensors failed."));
80 }
81
82 /* ---------------- ACCEL CONFIG ----------------
83 * - Scale: ±2/±4/±8/±16 g
84 * - DLPF: noise filtering
85 * - Averaging: noise reduction
86 * - Sample rate: output frequency
87 */
88
89 /* Set accelerometer full-scale range. */
90 if (!imu.setAccelScale(ICM20948_Accel_FullScale::G_4)) {
91 Serial.println(F("setAccelScale failed."));
92 }
93
94 /* Enable DLPF (recommended)
95 * - bypass = false → DLPF enabled
96 */
97 if (!imu.setAccelDLPF(ACCEL_DLPFCFG_3, false)) {
98 Serial.println(F("setAccelDLPF failed."));
99 }
100
101 /* Set averaging (noise reduction). */
102 if (!imu.setAccelAveraging(ICM20948_Accel_Average::AVG_8)) {
103 Serial.println(F("setAccelAveraging failed."));
104 }
105
106 /* Set output data rate (ODR)
107 * - Base = 1125 Hz
108 * - ODR = 1125 / (1 + divider)
109 * - Example: 225 Hz
110 */
111 if (!imu.setAccelSampleRate(225)) {
112 Serial.println(F("setAccelSampleRate failed."));
113 }
114
115 delay(10);
116}
117
118/**
119 * @brief Read and print accelerometer data in g.
120 */
121void loop() {
122 float ax, ay, az;
123
124 /* Read accelerometer data
125 * - Output in g units
126 * - Returns true on success
127 */
128 if (imu.readAccel(ax, ay, az)) {
129 Serial.print("ACCEL [g]: ");
130 Serial.print(ax, 3);
131 Serial.print(", ");
132 Serial.print(ay, 3);
133 Serial.print(", ");
134 Serial.println(az, 3);
135 } else {
136 Serial.println(F("ACCEL read failed"));
137 }
138
139 Serial.println(F("-----------------------------"));
140 delay(500);
141}