Example Code for Arduino-Revise Module Address
Last revision 2026/01/21
This code tests the address modification function of the URM14 ultrasonic sensor. Users can learn how to change the sensor's slave address using Modbus-RTU communication.
Hardware Preparation
- Arduino Leonardo x 1 (RS485 to TTL needs to occupy one serial port, so we recommend you to use device with more than 2 serial ports. Since Arduino Modbust takes up a lot of memory, it is suggested to use Arduino Mega2560 controller.)
- RS485 Shield for Arduino x1
- DC Power Supply x1
- USB Data Cable (Connect the Arduino board to a computer via the USB cable)
Software Preparation
- Arduino IDE
- Open Library Manager(Ctrl+Shift+I) in Arduino IDE, find and install ArduinoModbus and ArduinoRS485 Libraries.

Wiring Diagram

Sample Code
/**************************************************************************************************************
This code tests the address modification function of the URM14 ultrasonic sensor
@ author : [email protected]
@ data : 11.08.2020
@ version: 1.0
RX(TTL-RS485转接板) -> TX1/D1 (Arduino Leonardo) TX(TTL-RS485转接板)-> RX1/D0 (Arduino Leonardo)
**************************************************************************************************************/
#include <ArduinoModbus.h>
#include <ArduinoRS485.h>
#define PUBLIC_ADDR ((uint16_t)0x00)
#define SLAVE_ADDR ((uint16_t)0x0C)
#define TEMP_CPT_SEL_BIT ((uint16_t)0x01)
#define TEMP_CPT_ENABLE_BIT ((uint16_t)0x01 << 1)
#define MEASURE_MODE_BIT ((uint16_t)0x01 << 2)
#define MEASURE_TRIG_BIT ((uint16_t)0x01 << 3)
typedef enum{
ePid,
eVid,
eAddr,
eComBaudrate,
eComParityStop,
eDistance,
eInternalTempreture,
eExternTempreture,
eControl,
eNoise
}eRegIndex_t;//Sensor register index
/*
*@brief Read data from holding register of client
*
*@param addr : Address of Client
*@param reg: Reg index
*@return data if execute successfully, false oxffff.
*/
uint16_t readData(uint16_t addr, eRegIndex_t reg)
{
uint16_t data;
if (!ModbusRTUClient.requestFrom(addr, HOLDING_REGISTERS, reg, 1)){
Serial.print("failed to read registers! ");
Serial.println(ModbusRTUClient.lastError());
data = 0xffff;
}else{
data = ModbusRTUClient.read();
}
return data;
}
/*
*@brief write data to holding register of client
*
*@param addr : Address of Client
*@param reg: Reg index
*@param data: The data to be written
*@return 1 if execute successfully, false 0.
*/
uint16_t writeData(uint16_t addr, eRegIndex_t reg, uint16_t data)
{
if (!ModbusRTUClient.holdingRegisterWrite(addr, reg, data)){
Serial.print("Failed to write coil! ");
Serial.println(ModbusRTUClient.lastError());
return 0;
}else
return 1;
}
void setup() {
Serial.begin(9600);
ModbusRTUClient.begin(19200);
delay(3000);
}
volatile uint16_t newAddr, res;
void loop() {
newAddr = 0x11;
res = writeData(PUBLIC_ADDR, eAddr, newAddr);//Writes the new address value to the register
Serial.print("The device address has been modified as ");
Serial.print(newAddr);
Serial.println(".please reset the device!");
while (1);
}
Result

Was this article helpful?
