Arduino TKJHAT
Arduino library for Pico HAT extension board
Loading...
Searching...
No Matches
ICM42670.ino
1/* TKJHAT ICM42670 Example
2
3This example demonstrates how to use the ICM42670 IMU sensor on the TKJHAT.
4It initializes the sensor, performs calibration, and continuously reads and prints accelerometer,
5gyroscope, and temperature data.
6
7Circuit:
8- ICM42670 IMU integrated in Pico HAT
9
10created 2026
11*/
12
13#include <TKJHAT.h>
14
15TKJHAT hat;
16
17// Bias values for calibration (replace with actual calibration results)
18float accelBias[3] = {0.0, 0.0, 0.0};
19float gyroBias[3] = {0.0, 0.0, 0.0};
20
21
22void setup() {
23 // Start serial communication for output
24 Serial.begin(115200);
25 // Wait for serial connection
26 while (!Serial);
27
28 Serial.println("ax ay az gx gy gz temp");
29
30 // Initialize the HAT with the peripherals including IMU
31 hat.begin();
32
33 // Start the sensor and perform calibration
34 hat.icm42670.startWithDefaultValues();
35 hat.icm42670.calibrateAccel(accelBias);
36 hat.icm42670.calibrateGyro(gyroBias);
37}
38
39void loop() {
40 // Variables to hold sensor data
41 float ax, ay, az, gx, gy, gz, t;
42
43 // Read sensor data (ax, ay, az in g; gx, gy, gz in dps; t in °C)
44 hat.icm42670.readSensorData(ax, ay, az, gx, gy, gz, t);
45
46 // Apply calibration bias correction
47 ax -= accelBias[0];
48 ay -= accelBias[1];
49 az -= accelBias[2];
50 gx -= gyroBias[0];
51 gy -= gyroBias[1];
52 gz -= gyroBias[2];
53
54 // Print the sensor data in a space-separated format
55 Serial.print(ax); Serial.print(' ');
56 Serial.print(ay); Serial.print(' ');
57 Serial.print(az); Serial.print(' ');
58 Serial.print(gx); Serial.print(' ');
59 Serial.print(gy); Serial.print(' ');
60 Serial.print(gz); Serial.print(' ');
61 Serial.println(t);
62
63 // Delay before next reading
64 delay(200);
65}
Main interface for accessing TKJHAT board features.
Definition TKJHAT.h:35
void begin()
Initialize all peripherals on the HAT Also initializes the default I2C bus for sensors and display.