Example Code for Arduino-RS485 Communication
Last revision 2025/12/15
This project demonstrates how to use the DFR0088 IO expansion shield to transmit and receive data via RS485 communication with Arduino. Users can learn how to control the RS485 enable pin for transmission and reception, and how to handle serial data communication.
Hardware Preparation
| Name | Model/SKU | Quantity |
|---|---|---|
| DFR0088 IO Expansion Shield | DFR0088 | 1 |
| Arduino Board | UNO/Duemilanov/Mega | 1 |
| RS485 Device | - | 1 |
| Jumper Wires | - | 5 |
Software Preparation
- Development Tool: Arduino IDE (version 1.8.x or 2.x)
- Download Link: Arduino IDE
- Library Loading: No additional libraries are required for this project.
Wiring Diagram
- Mount the DFR0088 IO expansion shield onto the Arduino board.
- Connect the A terminal of the DFR0088 RS485 screw terminal to the A terminal of the other RS485 device.
- Connect the B terminal of the DFR0088 RS485 screw terminal to the B terminal of the other RS485 device.
- Ensure the power supply for both devices is stable (5V for Arduino).
Other Preparation Work
- Set the three jumpers on the DFR0088 shield to 485 mode to enable RS485 communication.
- Open the Arduino IDE and select the correct board (e.g., Arduino UNO) and port in the Tools menu.
- Set the serial monitor baud rate to 19200 (matching the code).
Sample Code
RS485 Transmit Data
int EN = 2; //RS485 has a enable/disable pin to transmit or receive data. Arduino Digital Pin 2 = Rx/Tx 'Enable'; High to Transmit, Low to Receive
void setup()
{
pinMode(EN, OUTPUT);
Serial.begin(19200);
}
void loop()
{
// send data
digitalWrite(EN, HIGH);//Enable data transmit
Serial.print('A');
delay(1000);
}
RS485 Receiving Data
int ledPin = 13;
int EN = 2;
int val;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(EN, OUTPUT);
Serial.begin(19200);
}
void loop()
{
// receive data
digitalWrite(EN, LOW);//Enable Receiving Data
val = Serial.read();
if (-1 != val) {
if ('A' == val) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
}
Result
- RS485 Transmit Data: The Arduino sends the character
Avia RS485 every 1 second. Use a serial monitor on the receiving device to verify the data. - RS485 Receiving Data: When the character
Ais received, the Arduino’s built-in LED (pin 13) blinks once (on for 500ms, off for 500ms).
Was this article helpful?
