This tutorial assumes you have already installed the QMI8658 library and know how to flash the board. See the library installation guide and getting started guide if needed.
What is an IMU?
An Inertial Measurement Unit (IMU) is a sensor that measures motion. The QMI8658 on E-Spin is a 6-axis IMU, meaning it provides two types of measurements:- Accelerometer (3 axes) — measures proper acceleration, i.e. the force applied to the sensor. At rest flat on a table, it reads ~1g on the Z axis (Earth’s gravity). In free fall, it reads 0g. This lets you determine tilt and detect sudden impacts.
- Gyroscope (3 axes) — measures angular velocity in degrees per second (°/s). It tells you how fast the sensor is rotating around each axis, not the absolute angle.
Library
This tutorial uses the QMI8658 Arduino Library by Lahav Gahali, MIT licensed.platformio.ini:
Wiring
No external wiring needed. The QMI8658 is connected directly to the ESP32-C3 on the PCB. Check your board’s silkscreen or schematic for the exact pin assignment. On current E-Spin hardware:| Signal | GPIO |
|---|---|
| SDA | 5 |
| SCL | 4 |
0x6A (SA0 low) or 0x6B (SA0 high) — the library auto-detects it.
Code
Walkthrough
What the ranges mean
Accelerometer range controls how large an acceleration the sensor can measure before clipping.±8g covers normal use — a hard tap registers around 3–5g. If you were mounting this on a drone or rocket, you’d go higher.
Gyroscope range is critical for E-Spin. A spinner at 300 RPM rotates at 1800°/s. At 500 RPM that’s 3000°/s, which exceeds ±2048 dps. For very fast spins, this channel will saturate — the fusion filter tutorial addresses this.
Reading the output
Open the Serial Monitor at 115200 baud. You should see a table like:- A (accel): at rest flat, Z ≈ 9.81 m/s² (gravity). Tilt the board and the gravity vector redistributes across X/Y/Z.
- G (gyro): at rest, values should be near zero with a small constant offset called bias — that’s normal and expected. Rotate the board and the relevant axis spikes.
- T: die temperature in °C. Gyro bias drifts with temperature; the fusion filter tutorial will handle compensation.
Checking the device ID
imu.getWhoAmI() should return 0x05. If it returns 0x00 or 0xFF, the sensor isn’t responding — run the I2C scanner first to confirm the address and connection.
Gyro range for spinning
The E-Spin board is designed to spin. UseQMI8658_GYRO_RANGE_2048DPS as your default — lower ranges will saturate and give you flat-topped garbage data the moment you flick it. If you’re only reading at rest (tap detection, orientation), 512DPS gives better resolution.
What the raw data cannot tell you
Raw gyroscope values give you rotation rate, not angle. Raw accelerometer values give you force, which blends gravity and motion together. Neither alone gives you a clean orientation. To get tilt angles, heading, or quaternions — that’s the next tutorial.IMU Fusion Filter
Combine accel and gyro with a complementary or Madgwick filter to get stable orientation angles.