- 01M5 update
- 02New IMU sample
- 03Policy sample
- 04LCD + serial
Open the complete sketch
Open counter/counter.ino in Arduino IDE, with counter.hpp in the same directory. Select the vendor's verified CoreS3 profile and compile the project with M5Unified installed.
The complete sketch was compiled and linked on 8 November 2026 with M5Stack 3.3.10, M5Unified 0.3.0, M5GFX 0.3.0 and Arduino CLI 1.5.1. Select M5CoreS3, USB Mode: Hardware CDC and JTAG and USB CDC On Boot: Enabled. The download's README includes the exact CLI setup. Compilation checks the SDK integration; the physical IMU and display still need the observations below.
#include <M5Unified.h>
#include "counter.hpp"
MotionCounter counter;
bool ready = false;
void setup() {
auto config = M5.config();
M5.begin(config);
Serial.begin(115200);
ready = M5.Imu.getType() != m5::imu_none;
M5.Display.setTextSize(2);
M5.Display.println(ready ? "Motion counter\nGently move, then rest" : "IMU unavailable");
}
void loop() {
M5.update();
if (ready && M5.Imu.update()) {
const auto data = M5.Imu.getImuData();
if (counter.sample(static_cast<std::uint32_t>(millis()), data.accel.x, data.accel.y, data.accel.z)) {
M5.Display.fillRect(0, 100, M5.Display.width(), 60, 0);
M5.Display.setCursor(0, 100);
M5.Display.printf("Count: %lu", static_cast<unsigned long>(counter.count()));
Serial.printf("count=%lu\n", static_cast<unsigned long>(counter.count()));
}
}
delay(5);
}The application tests whether an IMU was detected at startup. Each new IMU sample is passed to the independent policy. An accepted event updates a small region of the display and writes the count to serial output. Nothing connects to Wi-Fi or sends the sensor stream to a server.
The five-millisecond delay yields between loop iterations; it is not a precise sampling-frequency guarantee. The policy uses elapsed timestamps and new sample availability, not an assumed fixed frame rate.
Observe the complete path
Begin with the device stationary. It should not count gravity as repeated motion. Move it gently, return to rest and wait before repeating. Keep it supported and do not drop or strike it to reach a threshold. Tune using synthetic samples first.
Try it
Watch the serial output while the screen updates. If serial events are correct but the screen is not, investigate the display adapter. If the vendor IMU example is healthy but counts are wrong, inspect the policy trace and units.