Kode Dot
IMU
Measure how the Kode Dot tilts and turns with its accelerometer and gyroscope, a QMI8658A.
What it is
An IMU, an inertial measurement unit, puts an accelerometer and a gyroscope in one chip, and the Kode Dot's is a QMI8658A from QST.
- The accelerometer measures acceleration, gravity included. Lying still it reads 1 g, and tilting the Kode Dot moves that 1 g from one axis to another. It is how an app knows which way you are holding it, and how a step counter counts.
- The gyroscope measures rotation rate, in degrees per second. It does not know where you are pointing, only how fast that is changing. Add its readings up over time and you get an angle, which slowly drifts.
With kode_imu_read() your app reads the acceleration and the rotation on the
three axes, and the chip's temperature. To count steps or
detect taps, you work them out from those readings. The chip can do it
too, but the SDK does not use those features.

With the magnetometer, which works as a compass, that
makes three sensors of three axes each, which is where "9-axis" comes from.
Together they correct each other. The accelerometer knows which way gravity
pulls, but not where north is. The compass finds north, although its reading changes when
the Kode Dot tilts, and the gyroscope responds fast and smoothly but drifts over
time. Combining the three is what "sensor fusion" means. For the heading,
kode_mag_heading() already combines the compass with the accelerometer, and
if you also want to add the gyroscope, you write that fusion in your app.
How it is connected
The QMI8658A sits on the Kode Dot's shared I2C bus,
the same one as the touch screen, the buttons and the LED. The P4 asks it for
each sample over that bus with kode_imu_read(). The chip has an alert line
that reaches the expander, but KodeOS does not use it, so it never tells you a
new sample is ready.

The full circuit is in the main board schematic.
Specifications
x grows to the left, y downwards and z out of the screen. With the Kode Dot standing on its bottom edge, accel_y reads −1 g.kode_imu_read() returns them in a kode_imu_t, all from the same sample. Acceleration is in g (accel_x, accel_y and accel_z), rotation in degrees per second (gyro_x, gyro_y and gyro_z) and temperature in degrees Celsius (temp).kode_imu_read_axis("accel_x") returns just one, by the name of its field.The calls are kode_imu_read() and kode_imu_read_axis(axis), in kode.h.
Both wait for the sample and, if they fail, return NAN.
Gotchas
isnan before you use it and, if it failed, kode_error() tells you why.Apps that use it
Example code
The program prints the acceleration, the rotation and the temperature twice a second. Lay the Kode Dot flat and one axis reads about 1000 mg with the other two near zero. Tilt it and the 1000 mg moves from one axis to another.
/* SPDX-License-Identifier: GPL-3.0-or-later */
/**
* @file imu_log.c
* @brief Prints the acceleration and rotation of the Kode Dot twice a second.
*
* Lay it flat and one acceleration axis reads about 1 g, the pull of gravity,
* while the rotation stays near 0. Pick it up and turn it, and the rotation
* values move. A failed read gives NAN, so the program skips those samples.
*
* from Kode with love
*/
#include <math.h>
#include "kode.h"
#define IMU_PERIOD_MS 500U /**< Time between two samples. */
#define MG_PER_G 1000.0f /**< Thousandths of a g in one g. */
/** @brief Start the system, then print one sample every 500 ms. */
void app_main(void)
{
kode_init();
kode_print("Running: tilt and turn the Kode Dot");
while ( true )
{
kode_imu_t imu = kode_imu_read();
if ( 0 == isnan(imu.accel_x) )
{
kode_print("accel %d %d %d mg gyro %d %d %d dps %d C",
(int)(imu.accel_x * MG_PER_G), (int)(imu.accel_y * MG_PER_G),
(int)(imu.accel_z * MG_PER_G), (int)imu.gyro_x,
(int)imu.gyro_y, (int)imu.gyro_z, (int)imu.temp);
}
kode_delay(IMU_PERIOD_MS);
}
}If you would rather build it yourself, follow the steps shared by every example.
In the SDK
kdrv/motion: the QMI8658A driver inkode-sdk, described in the QMI8658A driver.examples/imu: an SDK example for the same calls.

