DevLab_ICM20948 1.0.2
ICM-20948 9-Axis Motion Sensor Driver
Loading...
Searching...
No Matches
i2c_basic.ino
Go to the documentation of this file.
1/***************************************************************
2 * @file I2C_Basic.ino
3 * @author 7Semi,Jonathan Mejorado Lopez
4 * @brief Minimal I2C bring-up for 7Semi ICM-20948 +
5 * basic Accel/Gyro/Temp/Mag readout (updated API).
6 *
7 * Features
8 * - I2C init (UNO / ESP32)
9 * - beginI2C() initialization
10 * - Manual sensor enable (setSensors)
11 * - Read Accel / Gyro / Temp / Mag
12 *
13 * Notes
14 * - WHO_AM_I must be 0xEA
15 * - Sensors must be explicitly enabled
16 * - Magnetometer requires initMag()
17 *
18 * Wiring (Arduino UNO I2C)
19 * - SDA -> A4
20 * - SCL -> A5
21 * - VCC -> 3V3 (or 5V if supported)
22 * - GND -> GND
23 *
24 * Wiring (ESP32 default I2C)
25 * - SDA -> GPIO21
26 * - SCL -> GPIO22
27 ***************************************************************/
28#include <Wire.h>
29#include <DevLab_ICM20948.h>
30
31/* ====================== User Config ======================= */
32/** @brief I2C SDA pin used by the example board. */
33#define SDA_PIN 6
34/** @brief I2C SCL pin used by the example board. */
35#define SCL_PIN 7
36/** @brief I2C bus speed in hertz. */
37#define I2C_FREQ 400000UL
38/** @brief ICM-20948 I2C address selected by the AD0 pin. */
39#define ICM_ADDR 0x69
40
41/** @brief Global ICM-20948 driver instance. */
43
44/**
45 * @brief Configure Serial, I2C, sensor identity check, and enabled sensors.
46 *
47 * The sketch stops in an infinite loop if the IMU cannot be initialized or
48 * the WHO_AM_I register does not match the expected ICM-20948 value.
49 */
50void setup() {
51 Serial.begin(115200);
52 delay(200);
53 Serial.println(F("ICM-20948 — I2C Basic"));
54 Wire.begin(SDA_PIN, SCL_PIN);
55 /* Initialize IMU. */
56 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
57 Serial.println(F("ERROR: beginI2C() failed"));
58 while (1) delay(200);
59 }
60
61 /* WHO_AM_I check. */
62 uint8_t who;
63 if (!imu.readWhoAmI(who) || who != 0xEA) {
64 Serial.println(F("ERROR: WHO_AM_I mismatch"));
65 while (1) delay(200);
66 }
67
68 Serial.print(F("WHO_AM_I = 0x"));
69 Serial.println(who, HEX);
70
71 /* Enable all sensors. */
72 if (!imu.setSensors(true, true, true)) {
73 Serial.println(F("ERROR: setSensors failed"));
74 }
75
76 /* Initialize magnetometer. */
77 if (!imu.initMag()) {
78 Serial.println(F("Mag init failed"));
79 } else {
80 Serial.println(F("Mag initialized"));
81 }
82
83 Serial.println(F("Ready.\n"));
84}
85
86/**
87 * @brief Read accelerometer, gyroscope, magnetometer, and temperature data.
88 *
89 * Values are printed periodically to the Serial monitor in engineering units.
90 */
91void loop() {
92 float ax, ay, az;
93 float gx, gy, gz;
94 float mx, my, mz;
95 float tC;
96
97 /* Read accelerometer. */
98 if (imu.readAccel(ax, ay, az)) {
99 Serial.print(F("ACC [g]: "));
100 Serial.print(ax, 3); Serial.print(", ");
101 Serial.print(ay, 3); Serial.print(", ");
102 Serial.println(az, 3);
103 } else {
104 Serial.println(F("ACC read failed"));
105 }
106
107 /* Read gyroscope. */
108 if (imu.readGyro(gx, gy, gz)) {
109 Serial.print(F("GYR [dps]: "));
110 Serial.print(gx, 2); Serial.print(", ");
111 Serial.print(gy, 2); Serial.print(", ");
112 Serial.println(gz, 2);
113 } else {
114 Serial.println(F("GYR read failed"));
115 }
116
117 /* Read magnetometer. */
118 if (imu.readMag(mx, my, mz)) {
119 Serial.print(F("MAG [uT]: "));
120 Serial.print(mx, 2); Serial.print(", ");
121 Serial.print(my, 2); Serial.print(", ");
122 Serial.println(mz, 2);
123 } else {
124 Serial.println(F("MAG read failed"));
125 }
126
127 /* Read temperature. */
128 if (imu.readTemperature(tC)) {
129 Serial.print(F("TMP [C]: "));
130 Serial.println(tC, 2);
131 } else {
132 Serial.println(F("TMP read failed"));
133 }
134
135 Serial.println(F("-----------------------------"));
136 delay(500);
137}
138
139/* Note on magnetometer:
140 * - Uses internal I2C master (AK09916 via ICM20948)
141 * - initMag() must be called before readMag()
142 * - If values are zero:
143 * → check initMag()
144 * → verify power + pull-ups
145 */
bool readGyro(float &x, float &y, float &z)
bool readAccel(float &x, float &y, float &z)
bool readWhoAmI(uint8_t &whoAmI)
bool beginI2C(uint8_t address=0x69, TwoWire &i2cPort=Wire, uint32_t i2cSpeed=400000)
bool readMag(float &x, float &y, float &z)
bool readTemperature(float &temperature)
bool setSensors(bool accel_on, bool gyro_on, bool temp_on)
#define SCL_PIN
I2C SCL pin used by the example board.
Definition i2c_basic.ino:35
void setup()
Configure Serial, I2C, sensor identity check, and enabled sensors.
Definition i2c_basic.ino:50
#define SDA_PIN
I2C SDA pin used by the example board.
Definition i2c_basic.ino:33
#define ICM_ADDR
ICM-20948 I2C address selected by the AD0 pin.
Definition i2c_basic.ino:39
DevLab_ICM20948 imu
Global ICM-20948 driver instance.
Definition i2c_basic.ino:42
void loop()
Read accelerometer, gyroscope, magnetometer, and temperature data.
Definition i2c_basic.ino:91