HomeArtificial Intelligence DIYMachine Learning DIYTinyML Project: Build a Smart Motion Detection Device

TinyML Project: Build a Smart Motion Detection Device

TinyML is one of the most consequential shifts in applied artificial intelligence right now — and you don’t need a data center or a GPU cluster to participate. A TinyML project runs a trained machine learning model directly on a microcontroller the size of your thumb, consuming milliwatts of power, with no cloud dependency. This hands-on tutorial walks you through building a Smart Motion Detection System from scratch using an Arduino Nano 33 BLE Sense, TensorFlow Lite, and a free Python environment. By the end, you’ll have a working edge AI device and a foundational understanding of how intelligence is moving out of the cloud and into the physical world.

What Is TinyML and Why Does It Matter?

TinyML sits at the intersection of embedded systems and machine learning. Instead of sending sensor data to a remote server for inference, the model lives on the device itself — running predictions locally in real time. This architecture delivers three core advantages:

  • Latency: Decisions happen in milliseconds because there is no network round-trip.
  • Privacy: Raw sensor data never leaves the device.
  • Power efficiency: Inference on a microcontroller can run on a coin-cell battery for months.

These properties explain why TinyML is already embedded in fitness trackers, industrial vibration monitors, smart-home motion sensors, and precision-agriculture nodes. Understanding this paradigm gives you a practical edge whether you’re building IoT products, pursuing a career in embedded AI, or simply want to understand where machine learning is heading.

What You Will Build

This TinyML project produces a Motion Detection Device capable of classifying three movement states in real time, entirely on-device:

  • Stationary (no movement)
  • Walking or rhythmic motion
  • Sudden shake or impact

The workflow mirrors what production TinyML systems do: collect sensor data, train a lightweight classification model, compress it for constrained hardware, deploy it, and run live inference. The same four-stage pipeline powers everything from wearable health monitors to smart-security intrusion detectors.

Tools and Hardware You Need

1. Arduino Nano 33 BLE Sense

This is the recommended starter board for TinyML beginners. It ships with an onboard 9-axis IMU (accelerometer, gyroscope, magnetometer), a microphone, a temperature/humidity/pressure sensor, and a Bluetooth LE radio — all on a board smaller than a USB stick. Crucially, it carries an ARM Cortex-M4 processor with enough flash memory to hold a compressed TensorFlow Lite model. You can find full specifications on the official Arduino website.

2. USB-C or Micro-USB Cable

Used to flash firmware and collect serial data from your computer.

3. Arduino IDE

The free desktop IDE for writing, compiling, and uploading sketches. Download it from the Arduino IDE official download page.

4. Python + Google Colab

You will train and convert the model using Python inside Google Colab, which requires no local GPU or installation. A standard Google account is all you need.

The Four-Stage TinyML Workflow

Before touching any code, internalize this pipeline. Every TinyML project — from beginner hobby boards to commercial wearables — follows the same four stages:

  1. Collect — Record raw sensor data representing the target classes.
  2. Train — Build and train a small neural network to classify those classes.
  3. Compress — Convert and quantize the model into TensorFlow Lite format so it fits in kilobytes of flash memory.
  4. Deploy — Upload the model to the microcontroller and run real-time inference.

This is the same framework used when deploying machine learning models in Android apps, except the target runtime is a bare-metal microcontroller rather than a mobile OS. The constraints are far tighter: typical TinyML models must fit within 256 KB of flash and use under 100 KB of RAM.

Step-by-Step Implementation Playbook

Step 1 — Install the Arduino IDE and Board Support

Download and install the Arduino IDE. Once open:

  1. Go to Tools → Board → Boards Manager.
  2. Search for Arduino Mbed OS Nano Boards and install it.
  3. Connect your Nano 33 BLE Sense via USB.
  4. Select Arduino Nano 33 BLE under Tools → Board.
  5. Select the correct COM/serial port under Tools → Port.

Step 2 — Install TinyML Libraries

Inside the Arduino IDE, navigate to Tools → Manage Libraries and install the following two libraries:

  • Arduino_TensorFlowLite — Provides the TensorFlow Lite for Microcontrollers runtime.
  • Arduino_LSM9DS1 — Driver for the onboard 9-axis IMU sensor.

These two libraries are the foundation of almost every Arduino-based TinyML project. Good data preparation starts at the sensor driver level — the LSM9DS1 library handles raw register reads so your sketch receives calibrated floating-point acceleration values.

Step 3 — Collect Motion Data

Create a new Arduino sketch and paste the following code. This sketch streams accelerometer readings over the serial port at 10 Hz, which you will capture as your training dataset.

#include <Arduino_LSM9DS1.h>

void setup() {
  Serial.begin(9600);
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
}

void loop() {
  float x, y, z;
  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(x, y, z);
    Serial.print(x);
    Serial.print(",");
    Serial.print(y);
    Serial.print(",");
    Serial.println(z);
    delay(100);
  }
}

Upload the sketch, then open Tools → Serial Monitor (baud rate 9600). You will see comma-separated X, Y, Z values scrolling in real time. Now perform each of your target motion classes — stationary, walking, and shaking — for approximately 60 seconds each, copying the serial output into separate CSV files labeled by class. This labeled dataset is what you will feed into the training notebook.

Step 4 — Train a Classification Model in Google Colab

Open a new Colab notebook. The training process involves these substeps:

  1. Load your CSV files into a Pandas DataFrame, labelling each row with its motion class.
  2. Engineer features — compute a sliding window of, say, 50 consecutive readings (representing 5 seconds of data) and flatten or summarize each window as a feature vector. Proper feature scaling is critical here; reviewing normalization techniques for machine learning data preparation will help you avoid models that overfit to sensor offset.
  3. Build a small dense neural network using TensorFlow/Keras — two or three hidden layers with ReLU activations and a softmax output layer for three classes is sufficient for this task.
  4. Train for 50–100 epochs with an 80/20 train-validation split. Watch for overfitting; if validation loss diverges, reduce model depth or add dropout.
  5. Evaluate accuracy on a held-out test split before proceeding.

Because the dataset is small, you may also benefit from lightweight automated approaches. Techniques explored in hyperparameter optimization for machine learning can help you tune learning rate and batch size without exhaustive manual search.

Step 5 — Convert and Quantize the Model to TensorFlow Lite

Once training is complete, convert the Keras model to TensorFlow Lite format and apply post-training quantization. Quantization maps 32-bit floating-point weights to 8-bit integers, shrinking model size by roughly 75% with minimal accuracy loss — a non-negotiable step for microcontroller deployment.

import tensorflow as tf

# Convert to TFLite with integer quantization
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()

# Save the model
with open("motion_model.tflite", "wb") as f:
    f.write(tflite_model)

# Convert to C byte array for Arduino
with open("motion_model.h", "w") as f:
    f.write("const unsigned char model_data[] = {")
    f.write(",".join(str(b) for b in tflite_model))
    f.write("};n")
    f.write(f"const int model_data_len = {len(tflite_model)};n")

The output motion_model.h file is a C header containing your model as a byte array. This is how TensorFlow Lite for Microcontrollers loads models from flash memory — there is no file system on a microcontroller.

Step 6 — Deploy the Model to Arduino

Copy motion_model.h into your Arduino sketch folder. Write a new Arduino sketch that:

  1. Includes Arduino_TensorFlowLite.h and motion_model.h.
  2. Initialises the TensorFlow Lite interpreter, allocates tensors, and maps the model input/output buffers.
  3. In the main loop, reads a window of accelerometer samples into the model’s input tensor.
  4. Calls interpreter->Invoke() to run inference.
  5. Reads the output tensor, identifies the highest-probability class, and takes an action — for example, lighting an LED or printing the detected class over serial.

Once uploaded, the board classifies motion in real time, entirely locally, with no Wi-Fi or cloud required. That is edge AI in its purest form.

TinyML Feature Checklist

Capability Present in This Project
Machine Learning Inference
Embedded Microcontroller Hardware
Local On-Device Inference (No Cloud)
Low-Power Operation
Edge Computing Architecture
Model Quantization
Real-Time Sensor Processing

Risks, Limitations, and Honest Caveats

  • Dataset size: A 60-second-per-class dataset is small. Models trained on limited data can fail to generalise to new users or environments. Collect more data across different people and surfaces before relying on the device for anything critical.
  • Memory constraints: The Arduino Nano 33 BLE Sense has 1 MB of flash and 256 KB of RAM. Complex models will not fit. Keep your network architecture minimal.
  • Library versions: Arduino_TensorFlowLite is tightly coupled to specific TensorFlow versions. Pin your Colab TF version to match the library’s expectations to avoid operator compatibility errors at runtime.
  • Quantization accuracy drop: Post-training integer quantization typically causes a small accuracy regression. Evaluate your quantized model before deployment; if the drop is unacceptable, use quantization-aware training instead.
  • Sensor drift: IMU sensors can drift with temperature changes. For production use, implement calibration routines.

Extension Challenges

Beginner

  • Add an RGB LED that changes colour based on detected motion class.
  • Log classification results over Bluetooth LE to a phone app.

Intermediate

  • Add a fourth class: fall detection (a high-G impact signature).
  • Incorporate the onboard microphone to detect sound events alongside motion.

Advanced

  • Build a complete AI Smart Activity Tracker that classifies workouts, logs activity, and syncs summaries to a mobile app via BLE — a genuine portfolio project for AI + IoT roles.
  • Port the model to an ESP32 with a camera module and add a visual anomaly detection layer.

If you want to explore model quality improvement before deployment, reducing overfitting with adversarial validation is a practical technique applicable even to small embedded datasets.

Real-World Applications of This Architecture

The motion detection pipeline you just built is a simplified version of the same architecture used in:

  • Healthcare: Wearable fall-detection bands for elderly patients.
  • Industrial IoT: Predictive maintenance sensors on rotating machinery.
  • Agriculture: Soil and livestock activity monitors running on solar-charged nodes.
  • Security: Battery-powered intrusion detection sensors that process video or motion locally.
  • Fitness: Activity classification in smartwatches and sports wearables.

In each case, the compelling advantage is the same: intelligence at the edge means faster response, lower bandwidth cost, and stronger data privacy.

Key Takeaways

  • A TinyML project runs a trained ML model directly on a microcontroller — no cloud, no Wi-Fi required for inference.
  • The four-stage pipeline (Collect → Train → Compress → Deploy) applies to every TinyML system, from hobby boards to commercial wearables.
  • Post-training quantization is essential: it shrinks model size by ~75% so it fits in constrained flash memory.
  • The Arduino Nano 33 BLE Sense is the best beginner TinyML board because it bundles multiple sensors and an ARM Cortex-M4 on a single board.
  • Data quality and quantity are the primary bottlenecks for beginner TinyML models — invest time in collecting diverse, labelled sensor data.
  • Edge AI delivers three structural advantages over cloud inference: lower latency, better privacy, and reduced power consumption.
  • This skill set — embedded AI, sensor programming, model deployment — is increasingly valued in IoT, wearables, and industrial automation careers.

Frequently Asked Questions

Do I need prior machine learning experience to attempt this TinyML project?

Basic Python familiarity is helpful, and an introductory understanding of what a neural network does will make the training steps easier to follow. However, the workflow is designed to be beginner-accessible — you do not need to derive backpropagation or understand advanced optimisation theory.

Can I use a different microcontroller board?

Yes. The ESP32, Raspberry Pi Pico, and STM32 series all support TensorFlow Lite for Microcontrollers. The Arduino Nano 33 BLE Sense is recommended here because its bundled sensor suite eliminates extra wiring for beginners. On other boards you may need to source and wire external IMU modules.

How small does the model need to be?

For the Arduino Nano 33 BLE Sense, aim for a quantized model under 200 KB. In practice, a two-hidden-layer dense network for three-class motion classification typically compresses to well under 50 KB after integer quantization, leaving ample room for the TFLite interpreter overhead.

Is Google Colab the only option for training?

No. Any environment with TensorFlow 2.x and Python 3 works — local Jupyter notebooks, Kaggle Kernels, or cloud notebooks from other providers. Colab is recommended because it is free, requires no setup, and is well-documented in the TinyML community.

What happens if my model accuracy is too low?

Collect more data, ensure your class labels are consistent, check your feature window size, and experiment with slightly larger model architectures. Avoid increasing model size beyond memory constraints. Techniques like learning-rate scheduling and dropout regularisation can also improve generalisation on small datasets.

Most Popular