EDGE ComputingIoT HardwaresMicrocontrollersTutorials/DIY

Running Edge ML on Wio Terminal

Introduction

Edge Machine Learning (Edge ML) enables real-time data processing directly on low-power embedded devices, reducing dependency on cloud computation. The Wio Terminal by Seeed Studio is an ideal candidate for running Edge ML due to its powerful ATSAMD51 microcontroller, built-in sensors, and Wi-Fi/Bluetooth connectivity. In this guide, we will explore how to implement Edge ML on the Wio Terminal, including setting up TensorFlow Lite for Microcontrollers (TFLM), deploying trained models, and running real-time inferences.

Why Use Edge ML on Wio Terminal?

Edge ML provides several advantages:

  • Reduced Latency: Processes data locally without cloud dependency.
  • Lower Bandwidth Usage: No need to transmit large amounts of data.
  • Enhanced Privacy & Security: Keeps sensitive data on the device.
  • Efficient Power Consumption: Optimized for battery-operated IoT applications.

The Wio Terminal is particularly suited for Edge ML due to its powerful ARM Cortex-M4F processor, 192KB RAM, external flash storage, and built-in sensors, making it capable of running lightweight machine learning models.

Setting Up the Environment for Edge ML

1. Installing TensorFlow Lite for Microcontrollers (TFLM)

To run ML models on Wio Terminal, we need TensorFlow Lite for Microcontrollers (TFLM):

Using Arduino IDE

  1. Install Arduino IDE from Arduino official site.
  2. Add Seeed SAMD Boards via Board Manager:
    • Open Arduino IDE → Go to ToolsBoard Manager.
    • Search for “Seeed SAMD” and install it.
  3. Install TensorFlow Lite library:
    • Open Library Manager → Search for TensorFlow Lite for Microcontrollers.
    • Click Install.

Training an ML Model for Wio Terminal

1. Selecting a Model

You can use pre-trained models or train a custom model. Common models for Edge ML include:

  • Gesture Recognition (Accelerometer-based)
  • Keyword Spotting (Microphone-based)
  • Anomaly Detection (Sensor Data Analysis)

For example, let’s train a gesture recognition model using an accelerometer.

Training in Python (Google Colab)

import tensorflow as tf
import numpy as np

# Generate synthetic accelerometer data (X, Y, Z values)
data = np.random.rand(1000, 3)  # 1000 samples, 3 features
labels = np.random.randint(2, size=(1000, 1))  # Binary classification

# Define model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(16, activation='relu', input_shape=(3,)),
    tf.keras.layers.Dense(8, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(data, labels, epochs=10)

# Convert model to TensorFlow Lite format
tflite_model = tf.lite.TFLiteConverter.from_keras_model(model).convert()

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

Upload model.tflite to Wio Terminal for inference.

Deploying the ML Model on Wio Terminal

1. Upload the Model

  1. Connect Wio Terminal via USB.
  2. Copy the model.tflite file to the Wio Terminal’s storage.

2. Run Inference on Wio Terminal

Use the TensorFlow Lite Micro library in Arduino IDE to load and run the model:

#include <TensorFlowLite.h>
#include "model.tflite.h"  // Convert model.tflite to C array using xxd
#include "Arduino_HTS221.h"

void setup() {
    Serial.begin(115200);
    if (!HTS.begin()) {
        Serial.println("Failed to initialize sensor!");
        while (1);
    }
}

void loop() {
    float temperature = HTS.readTemperature();
    float humidity = HTS.readHumidity();
    
    Serial.print("Temperature: "); Serial.println(temperature);
    Serial.print("Humidity: "); Serial.println(humidity);
    
    delay(1000);
}

This code reads temperature and humidity, which can be used for ML-based anomaly detection.

Advanced Topics

1. Real-Time Sensor Data Classification

Use ML models to classify real-time sensor data (e.g., detect motion patterns from accelerometer readings).

#include <Wire.h>
#include "model.tflite.h"
#include <Adafruit_LIS3DH.h>

Adafruit_LIS3DH lis = Adafruit_LIS3DH();

void setup() {
    Serial.begin(115200);
    if (!lis.begin(0x18)) {
        Serial.println("Could not start accelerometer!");
        while (1);
    }
}

void loop() {
    lis.read();
    float x = lis.x;
    float y = lis.y;
    float z = lis.z;
    
    Serial.print("X: "); Serial.println(x);
    Serial.print("Y: "); Serial.println(y);
    Serial.print("Z: "); Serial.println(z);
    
    delay(100);
}

2. Connecting Wio Terminal to the Cloud

After running ML inferences, send the data to the cloud using MQTT or HTTP.

Sending Data to an MQTT Broker

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqtt_server = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
    }
    client.setServer(mqtt_server, 1883);
}

void loop() {
    if (!client.connected()) {
        client.connect("WioTerminalClient");
    }
    client.publish("sensor/data", "ML Inference Result");
    delay(5000);
}

This example sends the ML inference result to an MQTT broker.

Conclusion

Running Edge ML on Wio Terminal enables real-time intelligent decision-making on embedded devices. With TensorFlow Lite for Microcontrollers, Wio Terminal can process data locally and classify sensor readings efficiently. By integrating Edge ML with cloud services, you can build powerful, low-latency IoT applications.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *