Skip to main content
Reacgen Biosystems

Arduino examples

We provide eight Arduino/ESP32 sketches covering every common usage scenario, using the TurbidVision library as the I²C master.

These sketches require the TurbidVision Arduino library. Install it first, then open any sketch below. The host must use 3.3 V logic. On a 5 V board (classic Uno/Nano) use a level shifter, or use an ESP32, ESP8266, or Arduino Nano.

Wiring

Arduino masterTurbid Vision Qwiic
SCLSCL
SDASDA
3.3 V3.3 V
GNDGND

The sensor has onboard I²C pull-ups, so no external resistors are needed. sensor.begin() calls Wire.begin() and sets the bus to 400 kHz; for custom ESP32 pins call Wire.begin(sda, scl) yourself and use sensor.begin(false).

1. Discovery and status check

Confirm the sensor responds at 0x69 and decode the live status byte. At power-on with no job running, all status bits are 0.

#include <Wire.h>
#include <TurbidVision.h>

TurbidVision sensor;  // default address 0x69 on Wire

void setup() {
  Serial.begin(115200);
  while (!Serial) {}

  if (!sensor.begin()) {  // Wire.begin() + 400 kHz + presence check
    Serial.println(F("ERROR: Turbid Vision not found at 0x69."));
    Serial.println(F("Check wiring, 3.3 V power, and that the firmware is running."));
    return;
  }

  Serial.println(F("Turbid Vision found at 0x69."));
  sensor.printStatus();  // reads register 0x20 and prints decoded flags
}

void loop() {}

2. Start a job and stream readings

Start a measurement job, allow at least 180 seconds for the current 60-sample initial reference, then stream calibrated density, printing only when a new reading is available.

#include <Wire.h>
#include <TurbidVision.h>

TurbidVision sensor;

void setup() {
  Serial.begin(115200);
  while (!Serial) {}

  if (!sensor.begin()) {
    Serial.println(F("ERROR: Turbid Vision not found. Check wiring/power."));
    while (true) { delay(1000); }
  }

  sensor.startJob();
  Serial.println(F("Start-job command sent."));

  if (!sensor.waitForStatusBit(TV_STATUS_JOB_RUNNING, 10000)) {
    Serial.println(F("ERROR: job did not start."));
    while (true) { delay(1000); }
  }
  Serial.println(F("Job running. Waiting for initial reference (allow at least 180 s)..."));
  if (!sensor.waitForStatusBit(TV_STATUS_HAS_INITIAL, 180000)) {
    Serial.println(F("ERROR: initial reference never captured. Check fiber."));
    while (true) { delay(1000); }
  }

  Serial.println(F("\n Reading   Density(g/L)   Laser(mW)"));
  Serial.println(F("------------------------------------"));
}

uint32_t lastReading = 0xFFFFFFFFUL;

void loop() {
  uint32_t n = sensor.readingNumber();
  if (n != lastReading) {
    float density = sensor.calibratedDensity();
    float laser   = sensor.laserPowerMw();

    Serial.print(F("  "));    Serial.print(n);
    Serial.print(F("\t   "));
    if (isnan(density)) Serial.print(F("NaN")); else Serial.print(density, 4);
    Serial.print(F("\t  "));
    if (isnan(laser)) Serial.println(F("NaN")); else Serial.println(laser, 4);

    lastReading = n;
  }
  delay(200);
}

3. Take a blank reference

Interactive sketch (open the Serial Monitor at 115200 baud, line ending = Newline). Stops the job, captures a blank in clear media, restarts, and reads back normalized reflectance.

#include <Wire.h>
#include <TurbidVision.h>

TurbidVision sensor;

// Block until the user sends a line in the Serial Monitor, then drain it.
void waitForKeypress(const __FlashStringHelper *prompt) {
  Serial.println(prompt);
  while (Serial.available()) Serial.read();      // drain stale input
  while (!Serial.available()) { delay(10); }     // wait for new input
  while (Serial.available()) Serial.read();       // drain the line
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {}

  if (!sensor.begin()) {
    Serial.println(F("ERROR: Turbid Vision not found. Check wiring/power."));
    while (true) { delay(1000); }
  }

  // Blanks must be taken with the job stopped.
  TVStatus s = sensor.readStatus();
  if (s.jobRunning) {
    Serial.println(F("Job is running, stopping it first."));
    sensor.stopJob();
    sensor.waitForStatusBit(TV_STATUS_JOB_RUNNING, 10000, /*waitForClear=*/true);
  }

  waitForKeypress(F("Place sensor in blank media, then send any character..."));

  sensor.takeBlank();
  Serial.print(F("Take-blank sent. Waiting for has_blank"));
  if (sensor.waitForStatusBit(TV_STATUS_HAS_BLANK, 35000))
    Serial.println(F("  -> blank captured."));
  else
    Serial.println(F("  -> TIMEOUT: blank did not complete."));

  waitForKeypress(F("Place sensor in your sample, then send any character..."));
  sensor.startJob();
  Serial.println(F("Job restarted. Waiting for initial reference..."));
  sensor.waitForStatusBit(TV_STATUS_HAS_INITIAL, 180000);

  delay(1000);
  float norm = sensor.normalizedReflectance();
  Serial.print(F("Normalized reflectance (with blank): "));
  if (isnan(norm)) Serial.println(F("NaN")); else Serial.println(norm, 4);
  Serial.println(F("(~1.0 in blank media; lower values indicate cell density.)"));
}

void loop() {}

4. Read all registers

Print a full snapshot of every measurement register plus the decoded status byte every 2 seconds.

#include <Wire.h>
#include <TurbidVision.h>

TurbidVision sensor;

void setup() {
  Serial.begin(115200);
  while (!Serial) {}

  if (!sensor.begin()) {
    Serial.println(F("ERROR: Turbid Vision not found. Check wiring/power."));
    while (true) { delay(1000); }
  }
  Serial.println(F("Turbid Vision connected. Dumping all registers every 2 s."));
  Serial.println(F("(Start a job from another example or the web UI to see live data.)"));
}

void loop() {
  Serial.println();
  sensor.printReadings();  // reading number + all 8 float registers
  sensor.printStatus();    // decoded status byte
  delay(2000);
}

5. Set laser power

Set the laser power target as a percentage of maximum (0.0–100.0 %), wait for the laser to stabilize, then read back the measured power.

#include <Wire.h>
#include <TurbidVision.h>

TurbidVision sensor;

const float TARGET_PCT = 80.0f;  // 0.0 - 100.0 %

void setup() {
  Serial.begin(115200);
  while (!Serial) {}

  if (!sensor.begin()) {
    Serial.println(F("ERROR: Turbid Vision not found. Check wiring/power."));
    while (true) { delay(1000); }
  }

  Serial.print(F("Setting laser target to "));
  Serial.print(TARGET_PCT, 1);
  Serial.println(F(" %"));
  sensor.setLaserPowerPct(TARGET_PCT);

  Serial.println(F("Waiting for laser_stable (bit 4)..."));
  if (sensor.waitForStatusBit(TV_STATUS_LASER_STABLE, 15000)) {
    delay(100);
    float mw = sensor.laserPowerMw();
    Serial.print(F("Laser power settled at: "));
    if (isnan(mw)) Serial.println(F("NaN"));
    else { Serial.print(mw, 4); Serial.println(F(" mW")); }
  } else {
    Serial.println(F("Laser did not stabilise within timeout."));
  }
}

void loop() {}

6. Plot density (Serial Plotter)

Stream density in Arduino Serial Plotter format (Tools → Serial Plotter, 115200 baud). The filtered series only appears when the Kalman filter is enabled on the sensor.

#include <Wire.h>
#include <TurbidVision.h>

TurbidVision sensor;

void setup() {
  Serial.begin(115200);
  while (!Serial) {}

  if (!sensor.begin()) {
    Serial.println(F("ERROR: Turbid Vision not found. Check wiring/power."));
    while (true) { delay(1000); }
  }

  // Ensure a job is running so we get live density data.
  TVStatus s = sensor.readStatus();
  if (!s.jobRunning) {
    sensor.startJob();
    sensor.waitForStatusBit(TV_STATUS_HAS_INITIAL, 180000);
  }
}

uint32_t lastReading = 0xFFFFFFFFUL;

void loop() {
  uint32_t n = sensor.readingNumber();
  if (n != lastReading) {
    float raw  = sensor.calibratedDensity();
    float filt = sensor.filteredCalibratedDensity();

    // Serial Plotter parses "Label:value" pairs separated by spaces/tabs.
    if (!isnan(raw))  { Serial.print(F("Raw:"));        Serial.print(raw, 4); }
    if (!isnan(filt)) { Serial.print(F("\tFiltered:")); Serial.print(filt, 4); }
    Serial.println();
    lastReading = n;
  }
  delay(200);
}

7. Kalman filter growth-rate monitoring

Tabulate the Kalman filter density and growth-rate estimates. These columns read NaN unless the Kalman filter is enabled (enable it from the web interface or over I²C with control command 0x08; disabling is web-interface only).

#include <Wire.h>
#include <TurbidVision.h>

TurbidVision sensor;

static void printField(float v, uint8_t width) {
  char buf[16];
  if (isnan(v)) snprintf(buf, sizeof(buf), "%*s", width, "NaN");
  else {
    char num[16];
    dtostrf(v, 0, 4, num);
    snprintf(buf, sizeof(buf), "%*s", width, num);
  }
  Serial.print(buf);
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {}

  if (!sensor.begin()) {
    Serial.println(F("ERROR: Turbid Vision not found. Check wiring/power."));
    while (true) { delay(1000); }
  }

  TVStatus s = sensor.readStatus();
  if (!s.jobRunning) {
    sensor.startJob();
    sensor.waitForStatusBit(TV_STATUS_HAS_INITIAL, 180000);
  }

  Serial.println(F("(NaN columns mean the Kalman filter is disabled.)"));
  Serial.println(F("     #   Density   FiltDens    mu(1/h)   AbsGR(g/L/h)"));
  Serial.println(F("-----------------------------------------------------"));
}

uint32_t lastReading = 0xFFFFFFFFUL;

void loop() {
  uint32_t n = sensor.readingNumber();
  if (n != lastReading) {
    char head[10];
    snprintf(head, sizeof(head), "%6lu", (unsigned long)n);
    Serial.print(head);
    printField(sensor.calibratedDensity(), 10);
    printField(sensor.filteredCalibratedDensity(), 11);
    printField(sensor.growthRate(), 11);
    printField(sensor.calibratedAbsoluteGrowthRate(), 14);
    Serial.println();
    lastReading = n;
  }
  delay(500);
}

8. Graceful shutdown

Stop the OD job and deactivate the laser before disconnecting. The register bank keeps its last valid readings until the next job start.

#include <Wire.h>
#include <TurbidVision.h>

TurbidVision sensor;

void setup() {
  Serial.begin(115200);
  while (!Serial) {}

  if (!sensor.begin()) {
    Serial.println(F("ERROR: Turbid Vision not found. Check wiring/power."));
    while (true) { delay(1000); }
  }

  TVStatus s = sensor.readStatus();
  Serial.print(F("Before shutdown: "));
  sensor.printStatus();

  if (s.jobRunning) {
    Serial.println(F("Stopping OD job..."));
    sensor.stopJob();
    sensor.waitForStatusBit(TV_STATUS_JOB_RUNNING, 10000, /*waitForClear=*/true);
  } else {
    Serial.println(F("Job is not running."));
  }

  if (s.laserPwrctlRunning) {
    Serial.println(F("Deactivating laser..."));
    sensor.deactivateLaser();
    delay(100);
  } else {
    Serial.println(F("Laser power controller is not running."));
  }

  TVStatus after = sensor.readStatus();
  Serial.print(F("After shutdown:  "));
  sensor.printStatus();

  if (!after.jobRunning && !after.laserPwrctlRunning)
    Serial.println(F("Shutdown complete. Sensor is idle."));
  else
    Serial.println(F("WARNING: a subsystem did not stop cleanly."));
}

void loop() {}

For the equivalent walkthrough in Python, see the Python examples page. For every method and constant, see the Arduino API reference; for install and downloads, see Code libraries.