Projects / Vibration analyser

Vibration analyser

Advanced4 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Wire the sensor

    I2C and power.

  2. ESP32

    Print raw values

    Check gravity reads about 1 g.

  3. ESP32

    Sample at 800 Hz

    Count samples per second.

  4. ESP32

    Add the FFT

    Print the strongest peak.

  5. ESP32

    Add the web page

    Draw the spectrum.

  6. ESP32

    Test a fan

    Find the turning frequency.

  7. ESP32

    Add tape to one blade

    Watch the peak grow.

In short

How do you build a vibration analyser?

An accelerometer and FFT show the vibration spectrum of fans, pumps and motors on a web page. You need ESP32 DevKit V1, ADXL345 accelerometer and 5 V 2 A USB adapter. It takes 7 steps and about 240 minutes.

Level
Advanced
Time
About 240 min
Parts
3

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ADXL345 VCCESP32 3V3Power
ADXL345 GNDESP32 GNDGround
ADXL345 SDAESP32 GPIO21I2C data
ADXL345 SCLESP32 GPIO22I2C clock
ADXL345 CSESP32 3V3Selects I2C mode
ADXL345 SDOESP32 GNDAddress 0x53
ESP32 USB5 V adapterPower

Code

Every spinning thing shakes at its own speed, and a spectrum shows how much shaking happens at each frequency. A fan at 3000 rpm turns 50 times a second, so a healthy one shows a peak near 50 Hz; stick a bit of tape on one blade and the peak grows, which is what an unbalanced rotor looks like. The sensor is read 800 times a second and 512 readings give a spectrum with about 1.6 Hz steps up to 400 Hz. The average is removed so gravity does not swamp the picture, and a window smooths the edges. Fix the sensor firmly with double-sided tape or a screw, because a loose sensor measures its own wobble. This is a learning tool: it cannot see fast bearing faults, and it does not replace a proper machine health check. Put your own Wi-Fi details in the placeholders and keep the page on your own network, since it is not encrypted. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Vibration spectrum: samples an ADXL345 at 800 Hz, runs a 512-point FFT and serves the result as a web page.
// Board: ESP32 Dev Module. No extra libraries needed (the FFT is written out below).
#include <Wire.h>
#include <WiFi.h>
#include <WebServer.h>
const char* SSID = "YOUR_WIFI_NAME";
const char* PASS = "YOUR_WIFI_PASSWORD";
const int N = 512;                       // samples per spectrum
const float FS = 800.0;                  // sample rate in Hz (set in the sensor)
float re[N], im[N];
float mag[N / 2];
WebServer server(80);
void adxlWrite(uint8_t r, uint8_t v) { Wire.beginTransmission(0x53); Wire.write(r); Wire.write(v); Wire.endTransmission(); }
float readMagnitude() {
  Wire.beginTransmission(0x53); Wire.write(0x32); Wire.endTransmission(false);
  Wire.requestFrom(0x53, 6);
  int16_t x = Wire.read() | (Wire.read() << 8);
  int16_t y = Wire.read() | (Wire.read() << 8);
  int16_t z = Wire.read() | (Wire.read() << 8);
  return sqrtf((float)x * x + (float)y * y + (float)z * z) * 0.0039f;   // in g, full resolution mode
}
void fft() {
  for (int i = 1, j = 0; i < N; i++) {           // bit reversal
    int bit = N >> 1;
    for (; j & bit; bit >>= 1) j ^= bit;
    j ^= bit;
    if (i < j) { float t = re[i]; re[i] = re[j]; re[j] = t; t = im[i]; im[i] = im[j]; im[j] = t; }
  }
  for (int len = 2; len <= N; len <<= 1) {
    float ang = -2.0f * PI / len;
    float wr = cosf(ang), wi = sinf(ang);
    for (int i = 0; i < N; i += len) {
      float cr = 1, ci = 0;
      for (int k = 0; k < len / 2; k++) {
        int a = i + k, b = i + k + len / 2;
        float tr = re[b] * cr - im[b] * ci, ti = re[b] * ci + im[b] * cr;
        re[b] = re[a] - tr; im[b] = im[a] - ti; re[a] += tr; im[a] += ti;
        float ncr = cr * wr - ci * wi; ci = cr * wi + ci * wr; cr = ncr;
      }
    }
  }
}
void measure() {
  float mean = 0;
  unsigned long next = micros();
  for (int i = 0; i < N; i++) {
    while ((long)(micros() - next) < 0) {}
    next += 1250;                                   // 800 Hz
    re[i] = readMagnitude(); im[i] = 0; mean += re[i];
  }
  mean /= N;
  for (int i = 0; i < N; i++) re[i] = (re[i] - mean) * (0.5f - 0.5f * cosf(2 * PI * i / (N - 1)));   // Hann window
  fft();
  for (int i = 0; i < N / 2; i++) mag[i] = sqrtf(re[i] * re[i] + im[i] * im[i]) * 2.0f / N;
}
void handleData() {
  measure();
  int peak = 1;
  for (int i = 2; i < N / 2; i++) if (mag[i] > mag[peak]) peak = i;
  String s = "{\"peakHz\":" + String(peak * FS / N, 1) + ",\"res\":" + String(FS / N, 2) + ",\"m\":[";
  for (int i = 1; i < N / 2; i++) { s += String(mag[i], 4); if (i < N / 2 - 1) s += ","; }
  s += "]}";
  server.send(200, "application/json", s);
}
const char PAGE[] PROGMEM = R"HTML(<html><body style="font-family:sans-serif"><h3>Vibration spectrum</h3>
<canvas id="c" width="640" height="260" style="border:1px solid #888"></canvas><p id="t">Measuring...</p>
<script>
function go(){fetch('/data').then(r=>r.json()).then(d=>{var c=document.getElementById('c').getContext('2d');c.clearRect(0,0,640,260);
var mx=Math.max.apply(null,d.m)||1;c.beginPath();for(var i=0;i<d.m.length;i++){var x=i*640/d.m.length,y=255-d.m[i]/mx*240;if(i==0)c.moveTo(x,y);else c.lineTo(x,y);}c.stroke();
document.getElementById('t').innerHTML='Strongest peak '+d.peakHz+' Hz (x-axis 0 to 400 Hz, resolution '+d.res+' Hz)';setTimeout(go,300);});}
go();
</script></body></html>)HTML";
void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22); Wire.setClock(400000);
  adxlWrite(0x2D, 0x08);               // measure mode
  adxlWrite(0x31, 0x08);               // full resolution, +/-2 g
  adxlWrite(0x2C, 0x0D);               // 800 Hz output data rate
  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED) delay(300);
  Serial.println(WiFi.localIP());
  server.on("/", []() { server.send_P(200, "text/html", PAGE); });
  server.on("/data", handleData);
  server.begin();
}
void loop() { server.handleClient(); }

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Sensor flat and stillReads about 1 g, no clear peak
Desktop fan at speedPeak at its turning frequency
Tape on a bladePeak is taller
Tap the tableWide burst of low frequencies

If it does not work

Reads zero

Check CS goes to 3V3 and the address is 0x53.

Peak at the wrong place

Check the 1250 microsecond step.

Noisy picture

Fix the sensor firmly to the machine.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the vibration analyser?

ESP32 DevKit V1, ADXL345 accelerometer and 5 V 2 A USB adapter.

How long does the vibration analyser take to build?

About 240 minutes for a advanced maker, in 7 steps.

How do I know the vibration analyser works?

Sensor flat and still: Reads about 1 g, no clear peak. Desktop fan at speed: Peak at its turning frequency. Tape on a blade: Peak is taller.

Reads zero: what should I check?

Check CS goes to 3V3 and the address is 0x53.

Peak at the wrong place: what should I check?

Check the 1250 microsecond step.

Noisy picture: what should I check?

Fix the sensor firmly to the machine.