Building a Smart Motion Detector with TinyML and ESP32 WROVER

Motion detection is a classic embedded project, but with TinyML, it becomes far more intelligent. Instead of simply triggering whenever movement is detected, a smart motion detector can recognize patterns, distinguish between types of movement, and filter out false alarms. Using an ESP32 WROVER kit and a simple PIR (passive infrared) sensor, you can build a compact, low-power system that detects motion and classifies it in real time.

Why Add TinyML to Motion Detection?

Traditional motion detectors sense infrared changes and trigger on/off events. While effective, they often produce false positives from pets, curtains, or heat sources. TinyML allows the system to analyze sensor data over short time windows, determining whether the movement is caused by a person, a pet, or simply background activity.

This approach is valuable in security systems, home automation, and energy-saving lighting. It reduces unnecessary alerts or activations, ensuring the system responds only to meaningful events.

Hardware You’ll Need

The ESP32 WROVER kit is ideal because of its ample RAM, dual-core processor, and built-in Wi-Fi. The PIR motion sensor connects directly to the microcontroller’s GPIO pins. Optional additions like a buzzer, LED indicator, or relay module can trigger alarms or control lights when motion is detected.

Collecting and Preparing Data

Data collection is one of the most critical stages in building a reliable TinyML motion detector. The quality, diversity, and quantity of training data determine how well the model performs in real-world conditions.

Decide on the motion categories to recognize. A basic setup might distinguish between “human,” “pet,” and “no motion,” but you can later add more nuanced classes like “fast movement” versus “slow movement.” The PIR sensor produces a simple high/low signal, but patterns in this signal over time carry valuable information.

Sample the PIR sensor at a consistent rate, such as 10 readings per second, and organize the readings into fixed-length windows, like two to five seconds per example. Even though the readings are binary, the sequence of changes provides the temporal patterns needed for classification.

Record motion events in varied conditions to create a balanced dataset. Walk past the sensor at different speeds, distances, and angles for “human” examples. Allow a pet to move freely or simulate it with an object at a lower height for “pet” examples. Capture periods of “no motion” in multiple lighting and temperature scenarios. Include “hard negatives” like swaying curtains or oscillating fans to teach the model to ignore non-human movements.

Logging can be done directly on the ESP32 using an SD card or over serial to a computer. CSV format works well for later preprocessing. Clean the dataset by removing incomplete or corrupted sequences and normalize the data so all windows are the same length. A well-prepared dataset ensures your TinyML model performs reliably and resists false triggers.

Training the Model

With a clean dataset, the next step is feature extraction and model training. Even though PIR readings are binary, temporal patterns are informative. Use a sliding window to segment readings into fixed-size sequences. You can feed the raw sequences directly to the model or compute additional features, such as the number of state changes, average “on” duration, or time between transitions.

For architecture, a compact design is essential. A simple dense network can work, but a 1D convolutional neural network (CNN) often captures temporal patterns more effectively. Small LSTMs can be used for more complex sequences but require slightly more computational resources.

Train your model in TensorFlow or with platforms like Edge Impulse. Split the dataset into training, validation, and test sets. Normalize the input data and use an optimizer like Adam with categorical cross-entropy for multi-class classification. Once trained, convert the model to TensorFlow Lite for Microcontrollers format. Apply integer quantization to reduce model size and speed up inference on the ESP32. Evaluate the quantized model to ensure performance remains high before deployment.

Deploying to the ESP32 WROVER

Deploying the model involves embedding the .tflite file into your firmware. Convert it into a C array and include it in your project so the ESP32 can access it directly. Use TensorFlow Lite for Microcontrollers to run inference on the device.

Your firmware continuously reads PIR values, stores them in a circular buffer, and runs inference once a complete window is collected. Based on the predicted class, the ESP32 can trigger alarms, LEDs, or relays. For battery-powered systems, consider using the PIR sensor to wake the ESP32 from deep sleep, run inference, and return to sleep to conserve energy.

Wi-Fi integration allows the ESP32 to send notifications, trigger webhooks, or communicate with home automation systems. Logging predictions over serial or Wi-Fi also helps in debugging and retraining if needed.

Dealing with Real-World Challenges

Real environments introduce variability. PIR sensors can trigger from non-human sources like curtains, sunlight, or HVAC vents. Sensor placement is critical—mount sensors where they capture human motion without facing direct sunlight or heat sources. Narrowing the field of view with a physical shield and applying software filtering or moving averages on model outputs can further reduce false positives.

Calibration is important when moving to a new location. Logging initial data and adjusting thresholds ensures accuracy. Dynamic thresholds based on temperature or time of day can help compensate for environmental changes.

Periodic retraining keeps the model accurate over time. Collect new examples of changing conditions and retrain the model. Edge-to-cloud synchronization is an option if your ESP32 has Wi-Fi, allowing small datasets to be uploaded for retraining and updated models to be downloaded.

Power management and sensor fusion enhance real-world performance. Interrupt-based wakeups, batch inference, or integrating ultrasonic, sound, or light sensors can improve accuracy and efficiency.

Final Thoughts

A smart motion detector powered by TinyML transforms a simple sensor into an intelligent, reliable system. By combining a well-placed PIR sensor with a trained model on the ESP32 WROVER, you can differentiate between humans, pets, and background activity, minimizing false alarms and enhancing automation. With careful data collection, training, deployment, and real-world tuning, this project demonstrates how TinyML can bring practical AI to embedded devices, opening the door for more advanced smart home, security, and automation applications.