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