Skip to Content

← All functionalities

Ardbox Q Lite ADArdbox Q Lite ADR0-10 VCondition monitoring

Condition Monitoring with an Alarm Output on an Ardbox Q Lite: full example

The Ardbox Q Lite learns the normal range of a 0-10 V signal and switches an alarm output when the signal drifts.

What it does

Connect a 0-10 V transmitter, for example motor current or vibration, to I0.4. During the first 10 minutes the Linux app learns the normal mean and spread. After that, if the 1-minute average moves more than 4 standard deviations, it switches on the alarm output: Q0.0 on the AD, relay R1 on the ADR.

What you need

  • Ardbox Q Lite AD or ADR with a 12 to 24 Vdc supply.
  • Arduino IDE 2.0+ with the Industrial Shields boards package (board: Ardbox Q, your version).
  • Arduino App Lab to run the Linux app.

How to run it

  1. Upload sketch/sketch.ino with Arduino IDE.
  2. Create an app in Arduino App Lab with app.yaml and python/main.py from this page, and run it.
  3. Leave the machine running normally for the first 10 minutes while the app learns.

sketch/sketch.ino

/*
  Ardbox Q Lite example 07: microcontroller side

  Gives the Linux app access to the inputs and one output through Arduino Bridge.
  Output: Q0.0 on Ardbox Q Lite AD, relay R1 on Ardbox Q Lite ADR.
  Upload with Arduino IDE 2.0+ or arduino-cli, board Ardbox Q, your version.
*/

#include <Arduino_RouterBridge.h>

#if defined(IS_ARDBOX_Q_RELAY)
const pin_size_t OUT = R1;    // Ardbox Q Lite ADR: relay R1
#else
const pin_size_t OUT = Q0_0;  // Ardbox Q Lite AD: digital output Q0.0
#endif

const pin_size_t DIGITAL_IN[] = { I0_0, I0_1, I0_2, I0_3 };
const pin_size_t ANALOG_IN[]  = { I0_4, I0_5, I0_6, I0_7, I0_8, I0_9 };

// Digital input 0 to 3 (I0.0 to I0.3). Returns -1 if the channel does not exist.
int readDigital(int ch) {
  if (ch < 0 || ch > 3) return -1;
  return digitalRead(DIGITAL_IN[ch]);
}

// Analog input 0 to 5 (I0.4 to I0.9), raw value 0 to 1023 (10 bits by default).
int readAnalog(int ch) {
  if (ch < 0 || ch > 5) return -1;
  return analogRead(ANALOG_IN[ch]);
}

bool setOutput(bool on) {
  digitalWrite(OUT, on ? HIGH : LOW);
  return on;
}

void setup() {
  for (pin_size_t p : DIGITAL_IN) pinMode(p, INPUT);
  pinMode(OUT, OUTPUT);
  Bridge.begin();
  Bridge.provide_safe("read_digital", readDigital);
  Bridge.provide_safe("read_analog", readAnalog);
  Bridge.provide_safe("set_output", setOutput);
}

void loop() {}

python/main.py

"""
Ardbox Q Lite example 07: condition monitoring

Watches one 0-10 V signal (for example a motor current or vibration
transmitter on I0.4). For the first 10 minutes it learns the normal mean and
spread. After that, if the 1-minute average moves more than 4 standard
deviations from normal, it switches on the alarm output (Q0.0 on AD, R1 on ADR).

1. Upload sketch/sketch.ino with Arduino IDE (board Ardbox Q, your version).
2. Run this app from Arduino App Lab.
"""
import statistics
import time
from collections import deque

from arduino.app_utils import *

CHANNEL = 0              # 0 = I0.4
SAMPLE_S = 1.0
LEARN_SAMPLES = 600      # 10 minutes
WINDOW = 60              # 1-minute moving average
THRESHOLD = 4.0          # standard deviations

learn = []
window = deque(maxlen=WINDOW)
mean = std = None
alarm = False


def loop():
    global mean, std, alarm
    value = Bridge.call("read_analog", CHANNEL)

    if mean is None:
        learn.append(value)
        if len(learn) >= LEARN_SAMPLES:
            mean = statistics.fmean(learn)
            std = max(statistics.pstdev(learn), 1.0)
            print(f"Normal range learned: mean {mean:.1f}, std {std:.1f}")
    else:
        window.append(value)
        if len(window) == WINDOW:
            score = abs(statistics.fmean(window) - mean) / std
            new_alarm = score > THRESHOLD
            if new_alarm != alarm:
                alarm = new_alarm
                Bridge.call("set_output", alarm)
                print(("ALARM" if alarm else "Back to normal") + f", score {score:.1f}")

    time.sleep(SAMPLE_S)


App.run(user_loop=loop)

app.yaml

name: Ardbox Q Lite condition monitoring
description: Learns the normal range of an analog signal and raises an alarm output when it drifts
icon: "🔧"
bricks: []