DevLab_ICM20948 1.0.0 Driver para sensor ICM-20948

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#define SDA_PIN 6
36#define SCL_PIN 7
37#define I2C_FREQ 400000UL
38#define ICM_ADDR 0x69
39
40DevLab_ICM20948 imu;
41
42void setup() {
43 Serial.begin(115200);
44 delay(200);
45 Serial.println(F("ICM-20948 (I2C) — Accel Example"));
46 Wire.begin(SDA_PIN, SCL_PIN);
47 /** Initialize IMU using I2C */
48 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
49 Serial.println(F("ERROR: ICM-20948 beginI2C() failed."));
50 while (1) delay(200);
51 }
52
53 Serial.println(F("ICM-20948 ready."));
54
55 /** Verify device identity */
56 uint8_t who;
57 if (imu.readWhoAmI(who)) {
58 Serial.print("WHO_AM_I: 0x");
59 Serial.println(who, HEX);
60 }
61
62 /** Enable only accelerometer
63 * - accel = true
64 * - gyro = false
65 * - temp = false
66 */
67 if (!imu.setSensors(true, false, false)) {
68 Serial.println(F("setSensors failed."));
69 }
70
71 /* ---------------- ACCEL CONFIG ----------------
72 * - Scale: ±2/±4/±8/±16 g
73 * - DLPF: noise filtering
74 * - Averaging: noise reduction
75 * - Sample rate: output frequency
76 */
77
78 /** Set accelerometer full-scale range */
79 if (!imu.setAccelScale(ICM20948_Accel_FullScale::G_4)) {
80 Serial.println(F("setAccelScale failed."));
81 }
82
83 /** Enable DLPF (recommended)
84 * - bypass = false → DLPF enabled
85 */
86 if (!imu.setAccelDLPF(ACCEL_DLPFCFG_3, false)) {
87 Serial.println(F("setAccelDLPF failed."));
88 }
89
90 /** Set averaging (noise reduction) */
91 if (!imu.setAccelAveraging(ICM20948_Accel_Average::AVG_8)) {
92 Serial.println(F("setAccelAveraging failed."));
93 }
94
95 /** Set output data rate (ODR)
96 * - Base = 1125 Hz
97 * - ODR = 1125 / (1 + divider)
98 * - Example: 225 Hz
99 */
100 if (!imu.setAccelSampleRate(225)) {
101 Serial.println(F("setAccelSampleRate failed."));
102 }
103
104 delay(10);
105}
106
107void loop() {
108 float ax, ay, az;
109
110 /** Read accelerometer data
111 * - Output in g units
112 * - Returns true on success
113 */
114 if (imu.readAccel(ax, ay, az)) {
115 Serial.print("ACCEL [g]: ");
116 Serial.print(ax, 3);
117 Serial.print(", ");
118 Serial.print(ay, 3);
119 Serial.print(", ");
120 Serial.println(az, 3);
121 } else {
122 Serial.println(F("ACCEL read failed"));
123 }
124
125 Serial.println(F("-----------------------------"));
126 delay(500);
127}