Example Code for Arduino-Modify Module Baud Rate

Last revision 2025/12/20

The article provides a comprehensive guide on modifying the baud rate for Arduino modules using RS485. It includes hardware requirements, software setup, and detailed sample code. Ideal for Arduino enthusiasts looking to enhance their projects with specific baud rate configurations.

Hardware Preparation

  • 1 x Arduino Leonardo (Since RS485 to TTL will occupy one serial port, it is recommended using a device with two or more serial ports like Arduino Leonardo. Arduino Modbus protocol takes up a lot of memory, so we recommend to use Arduino mega2560 board.)
  • 1 x RS485 Shield for Arduino
  • 1 x DC Power Supply (DC 24V)
  • 1 x USB Cable (Connect Arduino board and a PC)

Software Preparation

  • Arduino IDE
  • Open "Library Manager"(Ctrl+Shift+I) in IDE, find and install ArduinoModbus and ArduinoRS485 libraries.
    SEN0310-1

Wiring Diagram

SEN0310-Connection

Other Preparation Work

Install ArduinoModbus and ArduinoRS485 libraries via Arduino IDE's Library Manager.

Sample Code

/**************************************************************************************************************
*This code tests the baudrate modification function of the URM12 ultrasonic sensor(1500cm&RS485)
*@ author : [email protected]
*@ data   : 30.09.2019
*@ version: 1.0
*RX(RS485 Shield) -> TX1/D1 (Arduino Leonardo)  TX(RS485 Shield)-> RX1/D0 (Arduino Leonardo)
**************************************************************************************************************/
#include <ArduinoModbus.h>
#include <ArduinoRS485.h>

#define  addr_default    0x0B
#define  baudrate_default   19200

typedef enum
{
  PID,
  VID,
  SLAVE_ADDR,
  COM_BAUDRATE,
  COM_PARITY_STOP,
  DISTANCE,
  INTERNAL_TEMP,
  EXTERN_TEMP,
  CR
} regindex;

uint16_t cr_t = 0;

uint16_t read_data(uint16_t addr_t, uint16_t reg)
{
  uint16_t data_t;
  if (!ModbusRTUClient.requestFrom(addr_t, HOLDING_REGISTERS, reg, 1))
  {
    Serial.print("failed to read registers! ");
    Serial.println(ModbusRTUClient.lastError());
    data_t = 0xffff;
  }
  else
  {
    data_t =  ModbusRTUClient.read();
  }
  return data_t;
}


uint16_t write_data(uint16_t addr_t, uint16_t reg, uint16_t data)
{

  if (!ModbusRTUClient.holdingRegisterWrite(addr_t, 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 baudrate_index, res;
void loop() {

  baudrate_index = 3;//0x0001---2400   0x0002---4800 0x0003---9600   0x0004---14400
                     //0x0005---19200  0x0006---38400 0x0007---57600 0x0008---115200 Other----115200
  res = write_data(addr_default, COM_BAUDRATE, baudrate_index);
  if(res)
    Serial.print("The baudrate has been modified as 9600.please power off and restart the device");
  while (1);
}

Result

SEN0310-Modify Baudrate

Was this article helpful?

TOP