Back to Blog520 KB SRAM 4 MB Flash (typical) 240 MHz dual-core processor Inference time: < 5ms per prediction Accuracy: 94.2% on test data Power consumption: negligible increase
Machine LearningTFLiteESP32Edge AI
Running ML Models on Microcontrollers with TFLite
February 10, 202510 min read
Why Edge AI?
Sending all sensor data to the cloud for processing introduces latency and requires constant connectivity. Edge AI solves this by running models directly on the microcontroller.
The Challenge
ESP32 has limited resources:
Fitting a meaningful ML model in these constraints requires careful optimization.
Model Development
I trained an anomaly detection model in Python using TensorFlow:
python
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(16, activation='relu', input_shape=(5,)),
tf.keras.layers.Dense(8, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy')
model.fit(X_train, y_train, epochs=50, batch_size=16)Conversion to TFLite
python
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)The final model size was only 2.3 KB — perfect for ESP32.
Results
Conclusion
Edge AI on microcontrollers is not just possible — it's practical. The key is keeping models small and focused on specific tasks.