← Back to IoT Blog
AI/ML for IoT 35 min read

TinyML on ESP32

Complete guide to TinyML on ESP32. Run machine learning models on microcontrollers for gesture recognition, anomaly detection, and predictive maintenance.

What is TinyML?

TinyML brings machine learning to microcontrollers with <1MB RAM. Run inference in milliseconds with milliwatt power consumption.

TinyML Applications:
  • Gesture recognition (accelerometer)
  • Voice keyword spotting
  • Anomaly detection (vibration)
  • Predictive maintenance
  • Visual wake words (camera)

Setup & Installation

# Install TensorFlow Lite for Microcontrollers
# In Arduino IDE: Install "TensorFlowLite" library

# Or PlatformIO
[env:esp32dev]
platform = espressif32
board = esp32dev
lib_deps = 
  tensorflow/tensorflow@^2.10.0
  arduino-libraries/Arduino TensorFlowLite

Gesture Recognition

#include 
#include < MPU6050.h>
#include "gesture_model.h"

MPU6050 mpu;
tflite::MicroInterpreter* interpreter;

void setup() {
  mpu.initialize();
  
  // Load model
  static tflite::MicroModel model = 
    tflite::GetModel(gesture_model_tflite);
  
  // Create interpreter
  static tflite::MicroInterpreter interp(model, resolver, tensor_arena, kTensorArenaSize);
  interpreter = &interp;
  interpreter->AllocateTensors();
}

void loop() {
  // Read accelerometer
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);
  
  // Run inference
  TfLiteTensor* input = interpreter->input(0);
  input->data.f[0] = ax;
  input->data.f[1] = ay;
  input->data.f[2] = az;
  
  interpreter->Invoke();
  
  // Get prediction
  TfLiteTensor* output = interpreter->output(0);
  int gesture = argmax(output->data.f);
}

Anomaly Detection

Detect equipment failures from vibration patterns:

// Autoencoder for anomaly detection
float reconstruction_error = calculate_error(input_data);

if (reconstruction_error > THRESHOLD) {
  // Anomaly detected!
  sendAlert("MACHINE_ANOMALY");
}

Model Training

# Python training script
import tensorflow as tf

# Load sensor data
X_train, y_train = load_data()

# Create model
model = tf.keras.Sequential([
  tf.keras.layers.Dense(32, activation='relu'),
  tf.keras.layers.Dense(16, activation='relu'),
  tf.keras.layers.Dense(3, activation='softmax')
])

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

# Save for ESP32
with open('gesture_model.tflite', 'wb') as f:
  f.write(tflite_model)

Model Optimization

TechniqueSize ReductionAccuracy Impact
Post-training quantization4xMinimal
Pruning2-10xLow
Knowledge distillation10xMinimal

Next Steps

  • Try Edge Impulse for easy training
  • Add more sensor fusion
  • Implement continuous learning
  • Optimize for power efficiency