Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Created September 7, 2025 15:57
Show Gist options
  • Select an option

  • Save maxpromer/c45b31425cc8841ff56c7dfb83e16666 to your computer and use it in GitHub Desktop.

Select an option

Save maxpromer/c45b31425cc8841ff56c7dfb83e16666 to your computer and use it in GitHub Desktop.
#include <ModbusMaster.h>
#define RS485_RX_PIN (27)
#define RS485_TX_PIN (26)
ModbusMaster sensor;
void setup() {
Serial.begin(115200);
// Modbus communication runs at 9600 baud
Serial2.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN); // IOXESP32 Modbus RTU shield (Lite)
// Sensor init with Modbus slave ID 1
sensor.begin(1, Serial2);
}
void loop() {
// Read
uint8_t result;
result = sensor.readHoldingRegisters(0x0100, 1); // Read Holding Register start at 0 -> 1 word (0x0000 - 0x0000)
if (result == sensor.ku8MBSuccess) {
uint16_t value = sensor.getResponseBuffer(0);
Serial.print("Level: ");
Serial.print(value);
Serial.print(" MM");
Serial.println();
delay(1000);
} else {
Serial.println("Read sensor error, check your serial configs, wiring and power supply");
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment