Try Embedded

"TinyML: classify a motor's vibration on an Uno"

Turn a window of vibration samples into four features, then score them with a tiny trained model (a two-layer network in a few hundred bytes) that tells…

Sensors · Advanced

In a real product: Predictive maintenance sensor (Industrial): A sensor on a motor spots faults weeks before they stop the line, without sending raw data anywhere.

TinyML means running a machine-learning model on a microcontroller, next to the sensor. The hard part is rarely the model. It is turning raw samples into a few numbers, features, that separate the classes well. A good feature lets a tiny model do the job.

Here the Uno makes a window of 128 "accelerometer" samples at 1 kHz from one of three motor conditions, which the knob chooses: normal, imbalance (a strong vibration at the running speed, 25 Hz) and bearing wear (sharp, repeated impacts that add high-frequency energy). It extracts four features, runs a 4-6-3 neural network whose weights were trained on a PC, and lights the LED for the class it decided.

What you'll learn

  • Windowing a signal, and features that cost little: RMS, peak-to-peak, crest factor, and the energy of a high-pass filtered signal
  • Normalising features with the training set's mean and spread, so no feature swamps the others
  • A dense layer is a matrix multiply plus a bias; ReLU and argmax; where the weights come from
  • Why a model this size (57 weights and biases) fits where a full DSP library wouldn't

How it works

makeWindow() builds the signal. Every class has a 25 Hz running vibration and noise. Imbalance makes the 25 Hz part three times stronger. Bearing wear adds a short decaying "ring" at 600 Hz every 9 ms, the impact of a damaged ball passing the crack.

features() computes four numbers from the 128 samples:

  1. RMS, the overall vibration energy: high for imbalance.
  2. Peak-to-peak, the largest swing.
  3. Crest factor, peak divided by RMS: impacts give sharp peaks on a low RMS, so it is high for bearing wear.
  4. High-band RMS: the RMS after a first-order high-pass filter, y = 0.6 × (y + x − x_prev). Its corner is near 100 Hz, so it keeps most of the 600 Hz ringing and weakens the 25 Hz part about four times.

Each feature is then normalised, (f − mean) / spread, using numbers saved from the training data.

The network: 4 inputs, a hidden layer of 6 neurons with ReLU (negative values become 0), and 3 outputs, one per class. Each neuron is a weighted sum of its inputs plus a bias. The class with the largest output wins (argmax). The weights were trained on a PC, on 900 windows made exactly as makeWindow() makes them, and pasted in as const float arrays, rounded to two decimals. On 300 windows it had never seen, the model was right every time. A real project would export its weights from TensorFlow Lite or Edge Impulse in the same way, often as 8-bit integers. Measured on the training set, the classes differ most in RMS (imbalance) and crest factor (bearing wear).

Try this

  • Turn the knob to each third and check that the right LED lights. Then print the features and see which one separates each class.
  • Set the bearing impacts to half their strength (0.5 * ring): when does the model start to miss them?
  • Remove feature 4 (set its input to 0): which class gets confused now, and why?

sketch.ino

// TinyML on an Uno: four features from a 128-sample vibration window, then a 4-6-3 neural network (trained
// offline) decides normal / imbalance / bearing wear. The knob picks the condition to simulate.
#include <math.h>

const int N = 128;
const float RATE = 1000.0;
const int KNOB = A0, LED_NORMAL = 9, LED_IMBALANCE = 10, LED_BEARING = 11;
const char *const CLASSES[3] = { "normal", "imbalance", "bearing wear" };

float x[N];

void makeWindow(int condition) {
  float phase = random(0, 628) / 100.0;
  for (int n = 0; n < N; n++) {
    float t = n / RATE;
    float run = (condition == 1 ? 1.5 : 0.5) * sin(2 * M_PI * 25 * t + phase);
    float ring = 0;
    if (condition == 2) {
      float since = fmod(t, 0.009);                       // an impact every 9 ms
      ring = 1.2 * exp(-since * 900) * sin(2 * M_PI * 600 * since);
    }
    x[n] = run + ring + random(-100, 101) / 1000.0;
  }
}

void features(float f[4]) {
  float sum2 = 0, lo = x[0], hi = x[0], y = 0, prev = x[0], high2 = 0;
  for (int n = 0; n < N; n++) {
    sum2 += x[n] * x[n];
    if (x[n] < lo) lo = x[n];
    if (x[n] > hi) hi = x[n];
    y = 0.6 * (y + x[n] - prev);                         // first-order high-pass
    prev = x[n];
    high2 += y * y;
  }
  float rms = sqrt(sum2 / N);
  f[0] = rms;
  f[1] = hi - lo;
  f[2] = max(fabs(hi), fabs(lo)) / rms;                  // crest factor
  f[3] = sqrt(high2 / N);
}

// The model, trained offline on 900 windows made as makeWindow() makes them (100 % right on 300 more): the
// training set's mean and spread of each feature, then 4 -> 6 (ReLU) -> 3.
const float MEAN[4] = { 0.60, 1.94, 1.76, 0.15 };
const float SPREAD[4] = { 0.33, 0.86, 0.29, 0.06 };
const float W1[6][4] = {
  { -0.24, -0.52, -1.73, -1.32 }, {  0.19, -0.08, -0.72,  0.75 }, { -0.47, -1.44, -2.13, -0.79 },
  {  1.10,  0.57,  0.39,  1.57 }, { -0.29, -0.16, -0.41,  0.35 }, {  0.71,  0.48, -0.81,  0.98 },
};
const float BIAS1[6] = { -0.09, -0.03, -0.21, 0.31, -0.28, 0.29 };
const float W2[3][6] = {
  {  1.40, -0.31,  2.46, -0.63, -1.33, -0.39 },
  { -0.46,  1.08, -0.34,  1.51, -0.21,  1.00 },
  { -1.85, -0.11, -0.97, -0.50,  0.63, -0.92 },
};
const float BIAS2[3] = { -2.59, -2.66, 5.26 };

int classify(const float f[4], float out[3]) {
  float in[4], hidden[6];
  for (int i = 0; i < 4; i++) in[i] = (f[i] - MEAN[i]) / SPREAD[i];
  for (int j = 0; j < 6; j++) {
    float s = BIAS1[j];
    for (int i = 0; i < 4; i++) s += W1[j][i] * in[i];
    hidden[j] = s > 0 ? s : 0;                           // ReLU
  }
  int best = 0;
  for (int k = 0; k < 3; k++) {
    float s = BIAS2[k];
    for (int j = 0; j < 6; j++) s += W2[k][j] * hidden[j];
    out[k] = s;
    if (s > out[best]) best = k;
  }
  return best;
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_NORMAL, OUTPUT);
  pinMode(LED_IMBALANCE, OUTPUT);
  pinMode(LED_BEARING, OUTPUT);
}

void loop() {
  int condition = analogRead(KNOB) * 3L / 1024;           // 0, 1 or 2
  makeWindow(condition);
  float f[4], out[3];
  features(f);
  int guess = classify(f, out);

  digitalWrite(LED_NORMAL, guess == 0);
  digitalWrite(LED_IMBALANCE, guess == 1);
  digitalWrite(LED_BEARING, guess == 2);

  Serial.print(F("simulating "));
  Serial.print(CLASSES[condition]);
  Serial.print(F(": rms "));
  Serial.print(f[0], 2);
  Serial.print(F(" p-p "));
  Serial.print(f[1], 2);
  Serial.print(F(" crest "));
  Serial.print(f[2], 2);
  Serial.print(F(" high "));
  Serial.print(f[3], 2);
  Serial.print(F(" -> "));
  Serial.println(CLASSES[guess]);
  delay(400);
}

Lessons behind it