Example Code for Arduino - RS485 Port

Last revision 2026/02/05

Function Description

The product features a fully isolated RS485 interface that supports both RS485 master and slave modes, as well as the standard Modbus protocol.
In RS485 master mode, it can be connected to devices such as industrial sensors, relays, data loggers, and motor drives, providing strong practicality for project deployment.
In RS485 slave mode, it can be used for controller cascading communication, light synchronization, and other functions.

RS485 is connected to pins IO12 and IO13, mapped as follows: IO12: TX, IO13: RX, mapped to Serial1. The RS485 send/receive control pin is IO11.

Wiring diagram

This example will demonstrate how to use the RS485 interface to read distance values from a laser ranging sensor and print them to the serial port. With this example, you can use the RS485 interface to connect more industrial devices, such as ADC acquisition, multi-channel switch quantity acquisition, etc., to achieve more interactive lighting effects. RS485 laser ranging sensor datasheet link:

https://wiki.dfrobot.com/Laser_Ranging_Sensor_RS485_4m_SKU_SEN0492
RS485

485 data transceiver

The following example demonstrates reading the distance measured by a laser sensor using the RS485 interface and printing the distance value in millimeters (mm) to the serial port.

#include "DFRobot_RTU.h"

DFRobot_RTU modbus(&Serial1,11); //Set the data channel for RS485 to Serial1 and the receive/transmit control pin to IO11.

void setup() {
    Serial.begin(115200);
    Serial1.begin(115200, SERIAL_8N1, /*rx =*/13, /*tx =*/12); //Baud rate 115200, data bits 8, stop bits 1, RX pin IO13, TX pin IO12.
    delay(1000);
}

void loop() {
    int ret = modbus.readHoldingRegister(/*485 address*/0x50, /*Register address*/ 0x0034); //Read the holding register, reading one register.
    Serial.println(ret);
    delay(1000);
}

Was this article helpful?

ON THIS PAGE

TOP