Projects / Vibration timer for a machine

Vibration timer for a machine

Beginner1.3 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO19.7 mASSD1306

    Install the libraries

    SSD1306 and GFX.

  2. NANO

    Wire the sensor

    VCC, GND and DO to D2.

  3. NANO

    Wire the buzzer

    Plus to D8, minus to GND.

  4. NANO

    Wire the display

    A4 and A5 with power.

  5. NANO

    Test by hand

    Set QUIET_MS to 15000 and shake the module.

  6. NANO

    Stick it on the machine

    Outside only, on a flat panel.

  7. NANO

    Tune the sensitivity

    Turn the pot until the LED flickers only while running.

  8. NANO

    Run a cycle

    Wait for the ten beeps at the end.

In short

How do you build a vibration timer for a machine?

A vibration sensor on a machine tells the board when it starts and stops, and a buzzer says when the cycle ends. Stick it on the outside only. You need Arduino Nano, Vibration sensor module (SW-420), Passive buzzer, SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 8 steps and about 75 minutes.

Level
Beginner
Time
About 75 min
Parts
5

Parts list

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

Wiring

FromToWhy
Vibration module VCCNano 5VPower
Vibration module GNDNano GNDCommon ground
Vibration module DONano D2Digital output, interrupt pin
Buzzer plusNano D8Passive buzzer
Buzzer minusNano GNDCommon ground
OLED VCC and GNDNano 5V and GNDCheck the label on your module
OLED SDA and SCLNano A4 and A5I2C bus

Code

A SW-420 vibration module has a small spring switch and a comparator that makes a digital pulse each time the sensor shakes. A machine such as a washing machine or a dryer makes many pulses while it runs and almost none when it is still. The sketch counts the pulses with an interrupt in five second windows. Ten or more pulses in a window means the machine is running. When there have been no active windows for three minutes the sketch decides the cycle has ended, beeps ten times and goes back to waiting. The long quiet time is on purpose: many cycles pause while soaking, so a short quiet time would give a false finish. Stick the sensor to the outside of the machine with tape or putty, never inside it, and never open the machine or work on its wiring. The module has a small pot that sets how sensitive it is: turn it until the module's own LED flickers only when the machine is running. This is a convenience reminder, not a safety device, and it may beep early or late on some cycles.

Runs on a virtual Arduino Nano 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).

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int VIB = 2, BUZ = 8;
const bool VIB_IS_HIGH = true;                 // set to false if your module outputs LOW on vibration
const unsigned long WINDOW_MS = 5000;          // count vibrations in 5 second windows
const int ACTIVE_COUNT = 10;                   // this many in one window means the machine is running
const unsigned long QUIET_MS = 180000UL;       // 3 minutes of quiet means the cycle has ended
// For a quick test, set QUIET_MS to 15000 and shake the sensor by hand.

volatile unsigned int edges = 0;
enum State { IDLE, RUNNING, DONE };
State state = IDLE;
unsigned long windowStart = 0, lastActive = 0, startedAt = 0, doneAt = 0;

void onEdge() { edges++; }

void setup() {
  pinMode(BUZ, OUTPUT);
  pinMode(VIB, INPUT);
  attachInterrupt(digitalPinToInterrupt(VIB), onEdge, CHANGE);
  Serial.begin(9600);
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println("OLED not found"); while (1) { } }
  oled.setTextColor(SSD1306_WHITE);
  windowStart = millis();
}

void draw(const char* text, unsigned long mins) {
  oled.clearDisplay();
  oled.setTextSize(2);
  oled.setCursor(0, 0);
  oled.print(text);
  oled.setTextSize(1);
  oled.setCursor(0, 36);
  oled.print("Minutes: "); oled.println(mins);
  oled.display();
}

void loop() {
  unsigned long now = millis();

  if (now - windowStart >= WINDOW_MS) {
    noInterrupts(); unsigned int n = edges; edges = 0; interrupts();
    windowStart = now;
    bool active = (n >= ACTIVE_COUNT);
    Serial.println(n);

    if (state == IDLE && active) { state = RUNNING; startedAt = now; lastActive = now; }
    else if (state == RUNNING) {
      if (active) lastActive = now;
      else if (now - lastActive >= QUIET_MS) { state = DONE; doneAt = now; }
    }
  }

  if (state == DONE) {
    // Beep ten times, then go back to waiting
    int beeps = (now - doneAt) / 600;
    if (beeps < 10) { if (((now - doneAt) % 600) < 250) tone(BUZ, 2000); else noTone(BUZ); }
    else { noTone(BUZ); state = IDLE; }
  }

  if (state == IDLE) draw("Waiting", 0);
  else if (state == RUNNING) draw("Running", (now - startedAt) / 60000UL);
  else draw("Done!", (doneAt - startedAt) / 60000UL);
  delay(100);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

0

Test checklist

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

CheckExpected
Sensor on a still machineWaiting
Machine runningRunning with the minutes counting
Machine stops for three minutesTen beeps, then Waiting
Shake by handRunning within five seconds
Sensor too looseMisses vibration

If it does not work

Never reaches Running

Turn the sensitivity pot up or stick the sensor flat on a panel that shakes.

Always Running

Sensitivity is too high or the module output is inverted.

Beeps in the middle of a cycle

Increase QUIET_MS.

Pulses stay at zero

Check the DO wire on D2.

Display blank

Try address 0x3D instead of 0x3C.

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

Ask Lark AI

Common questions

What parts do I need for the vibration timer for a machine?

Arduino Nano, Vibration sensor module (SW-420), Passive buzzer, SSD1306 OLED Display and Breadboard 830 Tie Points.

How long does the vibration timer for a machine take to build?

About 75 minutes for a beginner maker, in 8 steps.

How do I know the vibration timer for a machine works?

Sensor on a still machine: Waiting. Machine running: Running with the minutes counting. Machine stops for three minutes: Ten beeps, then Waiting.

Never reaches Running: what should I check?

Turn the sensitivity pot up or stick the sensor flat on a panel that shakes.

Always Running: what should I check?

Sensitivity is too high or the module output is inverted.

Beeps in the middle of a cycle: what should I check?

Increase QUIET_MS.

Pulses stay at zero: what should I check?

Check the DO wire on D2.