Skip to Content

← All functionalities

CSV Data Logging on an Ardbox Q Lite: full example

The Ardbox Q Lite saves its inputs every second to a daily CSV file on its internal eMMC, ready to open in a spreadsheet.

What it does

The microcontroller reads the four digital inputs and the six 0-10 V inputs. The Linux app collects them through Arduino Bridge and adds one line per second to a CSV file in /home/arduino/ardbox-logs. A new file starts every day.

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. Copy the files with scp or open them over SSH.

sketch/sketch.ino

/*
  Ardbox Q Lite example 04: 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 04: data logging

Every second, reads the four digital inputs and the six analog inputs
and adds a line to a CSV file on the internal eMMC. A new file starts each day.

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

from arduino.app_utils import *

LOG_DIR = "/home/arduino/ardbox-logs"
PERIOD_S = 1.0
HEADER = ["time"] + [f"I0.{i}" for i in range(10)]

os.makedirs(LOG_DIR, exist_ok=True)


def loop():
    now = datetime.now()
    row = [now.isoformat(timespec="seconds")]
    row += [Bridge.call("read_digital", ch) for ch in range(4)]
    row += [Bridge.call("read_analog", ch) for ch in range(6)]

    path = os.path.join(LOG_DIR, now.strftime("%Y%m%d") + ".csv")
    new_file = not os.path.exists(path)
    with open(path, "a", newline="") as f:
        writer = csv.writer(f)
        if new_file:
            writer.writerow(HEADER)
        writer.writerow(row)

    time.sleep(PERIOD_S)


App.run(user_loop=loop)

app.yaml

name: Ardbox Q Lite data logging
description: Saves the inputs to a CSV file every second
icon: "📈"
bricks: []