Projects / Machine run-hours meter

Machine run-hours meter

Intermediate1.7 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the sensor

    VCC to 5V, GND, DO to D2.

  2. NANO

    Wire the button

    D4 to GND through the button.

  3. NANO

    Wire the display

    5V or 3V3, GND, SDA to A4, SCL to A5.

  4. NANO

    Upload the sketch

    The display shows Idle and the stored hours.

  5. NANO

    Fix the sensor to a fan or other small vibrating machine

    The display says RUNNING while it works.

  6. NANO

    Turn the machine off

    Five seconds later it returns to Idle.

  7. NANO

    Hold the button for 3 seconds

    The hours reset to zero.

In short

How do you build a machine run-hours meter?

A vibration sensor stuck to a fan, pump or small workshop machine tells a Nano when it is running, and the board counts the hours and keeps them through power cuts for service planning. It needs no connection to the machine's wiring. You need Arduino Nano, Vibration sensor module (SW-420), SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 100 minutes.

Level
Intermediate
Time
About 100 min
Parts
6

Parts list

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

Wiring

FromToWhy
SW-420 module VCCNano 5VPower the sensor
SW-420 module GNDNano GNDCommon ground
SW-420 module DONano D2Changes with vibration
Reset buttonNano D4 and GNDHold for three seconds to clear the hours
OLED VCCNano 5VPower (check your OLED accepts 5 V, otherwise use 3V3)
OLED GNDNano GNDCommon ground
OLED SDA and SCLNano A4 and A5I2C, address 0x3C

Code

The SW-420 module has a small spring that makes and breaks contact when it shakes, and a comparator that gives a clean on or off output. Stick it firmly to the body of the machine and count how many times the output changes each second. Four or more changes in a second counts as vibration, and the meter treats the machine as running until five seconds pass without any, so a pause does not flicker the display. While it runs, the Nano adds up the seconds and shows the total as hours. To protect the Nano's memory, which wears out after about a hundred thousand writes, the total is saved only every ten running minutes and when the machine stops, in a ring of sixty-four places, and on start-up the largest stored value is taken as the latest total. Holding the reset button for three seconds clears the hours. This is a service-planning aid and nothing more. It does not make a machine safe, never replaces proper maintenance rules or guards, and must not be fitted inside any enclosure with live mains parts.

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 <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);

const uint8_t VIB_PIN = 2;
const uint8_t RESET_PIN = 4;
const uint8_t EDGE_MIN = 4;                  // edges per second that count as vibration
const unsigned long HOLD_ON_MS = 5000;       // still "running" for 5 s after the last vibration
const unsigned int SLOTS = 64;               // rotate writes across 64 places to spare the memory
const unsigned long SAVE_EVERY_S = 600;      // save every 10 running minutes

volatile unsigned int edges = 0;
unsigned long runSeconds = 0;                // total running seconds
unsigned long lastSaveS = 0;
unsigned long lastVib = 0;
unsigned long lastTick = 0;
bool running = false;
unsigned long resetHeldSince = 0;

void onEdge() { edges++; }

unsigned long slotAddr(unsigned int i) { return i * 4UL; }

unsigned long readSlot(unsigned int i) {
  unsigned long v;
  EEPROM.get(slotAddr(i), v);
  return (v == 0xFFFFFFFFUL) ? 0 : v;
}

void saveTotal() {
  unsigned int slot = (runSeconds / SAVE_EVERY_S) % SLOTS;
  EEPROM.put(slotAddr(slot), runSeconds);
  lastSaveS = runSeconds;
}

void clearAll() {
  unsigned long blank = 0xFFFFFFFFUL;
  for (unsigned int i = 0; i < SLOTS; i++) EEPROM.put(slotAddr(i), blank);
  runSeconds = 0;
  lastSaveS = 0;
}

void draw() {
  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);
  oled.print(running ? "RUNNING" : "Idle");
  oled.setTextSize(3);
  oled.setCursor(0, 18);
  oled.print(runSeconds / 3600.0, 1);
  oled.setTextSize(1);
  oled.setCursor(0, 54);
  oled.print("hours run");
  oled.display();
}

void setup() {
  pinMode(VIB_PIN, INPUT);
  pinMode(RESET_PIN, INPUT_PULLUP);
  Wire.begin();
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
  for (unsigned int i = 0; i < SLOTS; i++) {          // the largest stored total is the latest
    unsigned long v = readSlot(i);
    if (v > runSeconds) runSeconds = v;
  }
  lastSaveS = runSeconds;
  attachInterrupt(digitalPinToInterrupt(VIB_PIN), onEdge, CHANGE);
  draw();
}

void loop() {
  if (millis() - lastTick >= 1000) {
    lastTick = millis();
    noInterrupts();
    unsigned int e = edges;
    edges = 0;
    interrupts();
    if (e >= EDGE_MIN) lastVib = millis();
    bool nowRunning = (lastVib != 0) && (millis() - lastVib < HOLD_ON_MS);
    if (nowRunning) runSeconds++;
    if (running && !nowRunning && runSeconds - lastSaveS > 60) saveTotal();   // save when it stops
    if (nowRunning && runSeconds - lastSaveS >= SAVE_EVERY_S) saveTotal();
    running = nowRunning;
    draw();
  }

  if (digitalRead(RESET_PIN) == LOW) {
    if (resetHeldSince == 0) resetHeldSince = millis();
    if (millis() - resetHeldSince > 3000) {
      clearAll();
      draw();
      resetHeldSince = millis() + 5000;
    }
  } else {
    resetHeldSince = 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 the tableIdle, hours unchanged
Tap the sensor repeatedlyRUNNING and hours rise
Stop tappingIdle after about 5 seconds
Unplug and plug inHours kept
Hold reset 3 secondsZero hours

If it does not work

Always RUNNING

Turn the module's pot to lower the sensitivity.

Never RUNNING

Turn the pot up, or fix the sensor firmly against the machine body.

Hours lost after power cut

The total saves every 10 running minutes and when it stops.

Display blank

Check the OLED address 0x3C and the wires.

Counts too fast

Make sure the sensor is not picking up the table or other machines.

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

Ask Lark AI

Common questions

What parts do I need for the machine run-hours meter?

Arduino Nano, Vibration sensor module (SW-420), SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the machine run-hours meter take to build?

About 100 minutes for a intermediate maker, in 7 steps.

How do I know the machine run-hours meter works?

Sensor on the table: Idle, hours unchanged. Tap the sensor repeatedly: RUNNING and hours rise. Stop tapping: Idle after about 5 seconds.

Always RUNNING: what should I check?

Turn the module's pot to lower the sensitivity.

Never RUNNING: what should I check?

Turn the pot up, or fix the sensor firmly against the machine body.

Hours lost after power cut: what should I check?

The total saves every 10 running minutes and when it stops.

Display blank: what should I check?

Check the OLED address 0x3C and the wires.