Skip to content

Instantly share code, notes, and snippets.

@fxprime
Created April 21, 2026 17:25
Show Gist options
  • Select an option

  • Save fxprime/a4c526f8689c9d4b62e01db6115b1014 to your computer and use it in GitHub Desktop.

Select an option

Save fxprime/a4c526f8689c9d4b62e01db6115b1014 to your computer and use it in GitHub Desktop.
RS-LDO-N01-1 — Auto Scanner
/* -------------------------------------------------------------------------- */
/* RS-LDO-N01-1 — Auto Scanner (address + baud rate) */
/* -------------------------------------------------------------------------- */
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <ModbusMaster.h>
EspSoftwareSerial::UART mySerial;
#define MAX485_DE 4
#define MAX485_RO 18
#define MAX485_DI 19
ModbusMaster node;
void preTransmission() { digitalWrite(MAX485_DE, 1); }
void postTransmission() { digitalWrite(MAX485_DE, 0); }
float convertRegistersToFloat(uint16_t reg0, uint16_t reg1) {
union { uint8_t b[4]; float f; } c;
c.b[3] = (reg0 >> 8) & 0xFF;
c.b[2] = reg0 & 0xFF;
c.b[1] = (reg1 >> 8) & 0xFF;
c.b[0] = reg1 & 0xFF;
return c.f;
}
// All baud rates listed in the manual
const uint32_t BAUD_RATES[] = { 4800, 9600, 2400, 19200, 38400, 57600, 115200, 1200 };
const uint8_t BAUD_COUNT = sizeof(BAUD_RATES) / sizeof(BAUD_RATES[0]);
// Found device info
bool deviceFound = false;
uint32_t foundBaud = 0;
uint8_t foundAddress = 0;
void tryBaudAndAddresses(uint32_t baud) {
Serial.printf("\n>>> Trying baud rate: %lu\n", baud);
mySerial.end();
delay(50);
mySerial.begin(baud, EspSoftwareSerial::SWSERIAL_8N1,
MAX485_RO, MAX485_DI, false, 95, 11);
delay(100);
// Try address 1 first (factory default), then 2–10, then the rest up to 247
// Full Modbus range is 1–247; address 0 is broadcast (skip)
for (uint16_t addr = 1; addr <= 247; addr++) {
node.begin((uint8_t)addr, mySerial);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
uint8_t result = node.readHoldingRegisters(0x0000, 6);
if (result == node.ku8MBSuccess) {
float do_sat = convertRegistersToFloat(node.getResponseBuffer(0), node.getResponseBuffer(1));
float do_conc = convertRegistersToFloat(node.getResponseBuffer(2), node.getResponseBuffer(3));
float temp_c = convertRegistersToFloat(node.getResponseBuffer(4), node.getResponseBuffer(5));
// Sanity-check: values must be in plausible physical range
bool sane = (do_sat >= 0.0f && do_sat <= 200.0f) &&
(do_conc >= 0.0f && do_conc <= 20.0f) &&
(temp_c >= 0.0f && temp_c <= 40.0f);
if (sane) {
Serial.println("\n========== DEVICE FOUND ==========");
Serial.printf(" Baud rate : %lu\n", baud);
Serial.printf(" Address : %d (0x%02X)\n", addr, addr);
Serial.printf(" DO Sat : %.2f %%\n", do_sat);
Serial.printf(" DO Conc : %.2f mg/L\n", do_conc);
Serial.printf(" Temp : %.2f °C\n", temp_c);
Serial.println("==================================");
deviceFound = true;
foundBaud = baud;
foundAddress = (uint8_t)addr;
return;
}
}
// Small gap between attempts to let the bus settle
delay(30);
// Print a dot every 20 addresses so you can see progress
if (addr % 20 == 0) Serial.print('.');
}
}
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(MAX485_DE, OUTPUT);
digitalWrite(MAX485_DE, 0);
Serial.println("\nRS-LDO-N01-1 Auto Scanner");
Serial.println("Scanning all baud rates x addresses 1-247 ...");
for (uint8_t b = 0; b < BAUD_COUNT && !deviceFound; b++) {
tryBaudAndAddresses(BAUD_RATES[b]);
}
if (!deviceFound) {
Serial.println("\n[FAIL] Device not found on any baud rate or address.");
Serial.println("Check wiring: Brown=12V, Black=GND, Yellow/Green=A, Blue=B");
}
}
void loop() {
if (!deviceFound) {
delay(5000);
return;
}
// Normal polling loop once device is found
node.begin(foundAddress, mySerial);
uint8_t result = node.readHoldingRegisters(0x0000, 6);
if (result == node.ku8MBSuccess) {
float do_sat = convertRegistersToFloat(node.getResponseBuffer(0), node.getResponseBuffer(1));
float do_conc = convertRegistersToFloat(node.getResponseBuffer(2), node.getResponseBuffer(3));
float temp_c = convertRegistersToFloat(node.getResponseBuffer(4), node.getResponseBuffer(5));
Serial.printf("[Addr:0x%02X @ %lu baud] DO: %.2f mg/L Sat: %.1f%% Temp: %.1f°C\n",
foundAddress, foundBaud, do_conc, do_sat, temp_c);
} else {
Serial.printf("[WARN] Read failed (err 0x%02X)\n", result);
}
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment