Practical Bench / Embedded & Edge AI · Advanced · 90 min
Predictive maintenance: vibration anomaly detector
Learn the normal vibration level of a motor on the device itself, flag unbalance with a statistical threshold and see how many standard deviations a fault is from normal.
Why it matters
Factories stop failures before they happen by listening to vibration. The simplest edge-AI method learns what normal looks like, then flags what is not normal.
Toy or everyday hack
Use a toy motor with a small piece of tape on the shaft as a fault you can add and remove
Safety
- Stay clear of spinning parts: tie back long hair and loose sleeves.
- Stop the motor before adding or removing the tape.
- Use a small, light fan or wheel only. Never unbalance a large rotor.
Parts list
| Part | Qty | Note |
|---|---|---|
| ESP32 DevKit | 1 | |
| MPU6050 accelerometer breakout | 1 | |
| Toy DC motor with small fan or wheel | 1 | Run it from the Practical 1 driver |
| Small piece of tape or putty | 1 | To unbalance the rotor |
| Double-sided tape or a hot-glue base | 1 | To fix the sensor to the motor body |
Tools and equipment
Connections
| From | To | Note |
|---|---|---|
| MPU6050 VCC / GND | ESP32 3V3 / GND | |
| MPU6050 SDA / SCL | GPIO21 / GPIO22 | |
| Motor | Driver from Practical 1 | Fixed duty, for example 60 % |
What you should see
A balanced motor gives a stable RMS vibration with small spread. Adding a small mass raises vibration clearly, often by many standard deviations. A z-score above 3 is a common alarm level because it is unlikely to be noise alone. The method needs normal data to learn from, and it must be re-learned if speed or mounting changes.
Step by step
- Fix the MPU6050 firmly to the motor housing. A loose sensor measures its own wobble.
- Upload the sketch. Start the motor at a fixed duty. The sketch spends about 20 seconds learning the baseline of the balanced motor and prints mean and standard deviation.
- Let it print several normal lines (z close to 0, "ok"). Write 5 normal RMS values in the logbook as "Balanced".
- Stop the motor, add a small piece of tape on the fan or shaft, and restart at the same duty.
- Write 3 RMS values for the unbalanced motor in the logbook.
- The logbook finds the baseline mean and spread from the balanced rows, then gives a z-score for every other row.
- Try a smaller or larger tape piece and see how the z-score scales.
Challenge: Compute the vibration spectrum with a simple FFT and find the peak at the motor rotation frequency. Compare balanced and unbalanced spectra.
Code (ESP32 DevKit, Arduino IDE)
Check pin numbers against your board. Test the sketch on your own hardware before a class demo.
// Practical 10 - Motor vibration anomaly detector (ESP32 + MPU6050) - simplest form of edge AI
#include <Wire.h>
const uint8_t MPU = 0x68;
const int N = 200; // samples per window (about 1 s)
const int LEARN_WINDOWS = 20; // windows used to learn "normal"
float sum = 0, sum2 = 0, mean0 = 0, sd0 = 1;
int learned = 0;
bool readAccel(float &ax, float &ay, float &az) {
Wire.beginTransmission(MPU); Wire.write(0x3B); Wire.endTransmission(false);
if (Wire.requestFrom(MPU, (uint8_t)6) != 6) return false;
int16_t x = (Wire.read() << 8) | Wire.read();
int16_t y = (Wire.read() << 8) | Wire.read();
int16_t z = (Wire.read() << 8) | Wire.read();
ax = x / 16384.0f; ay = y / 16384.0f; az = z / 16384.0f; // +/-2 g range -> g
return true;
}
float windowRmsMg() { // vibration = spread of |a| around its mean (gravity cancels)
static float buf[N];
float m = 0, m2 = 0, x, y, z;
for (int i = 0; i < N; i++) {
buf[i] = readAccel(x, y, z) ? sqrtf(x * x + y * y + z * z) : 1.0f;
m += buf[i]; delay(3);
}
m /= N;
for (int i = 0; i < N; i++) { float d = buf[i] - m; m2 += d * d; }
return sqrtf(m2 / N) * 1000.0f; // milli-g
}
void setup() {
Serial.begin(115200); Wire.begin(); Wire.setClock(400000);
Wire.beginTransmission(MPU); Wire.write(0x6B); Wire.write(0); Wire.endTransmission(true); // wake sensor
Serial.println("LEARNING: run the motor in its normal, balanced state...");
}
void loop() {
float r = windowRmsMg();
if (learned < LEARN_WINDOWS) {
sum += r; sum2 += r * r; learned++;
if (learned == LEARN_WINDOWS) {
mean0 = sum / LEARN_WINDOWS;
sd0 = sqrtf(max(sum2 / LEARN_WINDOWS - mean0 * mean0, 1e-4f));
Serial.printf("Baseline: mean=%.1f mg sd=%.2f mg\n", mean0, sd0);
}
return;
}
float zs = (r - mean0) / sd0;
Serial.printf("rms=%.1f mg z=%.1f %s\n", r, zs, zs > 3 ? "ANOMALY" : "ok");
}Readings logbook
Type your measured values. Calculated columns and the analysis update as you type, and your entries are saved in this browser.
If it does not work
| Symptom | Likely cause and fix |
|---|---|
| Baseline spread is large | Sensor loose or motor speed changing. Fix both before learning. |
| Normal state flagged as anomaly | Speed changed after learning, or battery sagged. Learn at the same operating point you monitor. |
| Fault not detected | Sensor too far from the source, or the fault is too small for the 2 g range resolution. Fix the sensor closer to the bearing. |
| All values 0 | Sensor still in sleep mode. The sketch writes 0 to register 0x6B to wake it. |