Example Code for Arduino-Revise Module Baud Rate

Last revision 2026/01/21

This code tests the baudrate modification function of the URM14 ultrasonic sensor. Users can learn how to change the sensor's baud rate 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

Hardware Connection

Sample Code

/**************************************************************************************************************
     This code tests the baudrate 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   BAUDRATE_DEFAULT          ((uint32_t)19200)
#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(BAUDRATE_DEFAULT);
  delay(3000);
}
volatile uint16_t baudrateIndex, res;
void loop() {

  baudrateIndex = 3;       //0x0001---2400   0x0002---4800 0x0003---9600   0x0004---14400
                           //0x0005---19200  0x0006---38400 0x0007---57600 0x0008---115200 Other----115200
  res = writeData(SLAVE_ADDR, eComBaudrate, baudrateIndex);//Writes the new baud rate value to the corresponding register
  if (res)
    Serial.print("The baudrate has been modified as 9600.please reset the device!");
  while (1);
}

Result

Revise Baud Rate

Was this article helpful?

TOP