The RS485 standard is a protocol for serial communication that allows for the implementation of multi-point systems that are robust, reliable, and can operate at high speeds over large distances. It is widely used in industrial environments for connecting various devices like sensors, controllers, and actuators, as well as in building automation, and other applications requiring multiple nodes communication.
Pin Number | Name | Description |
---|---|---|
1 | RO | Receiver Output. This pin outputs the data received via RS485. |
2 | RE | Receiver Enable. A low level on this pin enables the receiver. |
3 | DE | Driver Enable. A high level on this pin enables the driver. |
4 | DI | Driver Input. This pin inputs the data to be sent over RS485. |
5 | VCC | Positive Supply Voltage. Typically +5V. |
6 | GND | Ground. Reference voltage for the power supply. |
7 | A | Non-inverting Receiver/Driver input/output. |
8 | B | Inverting Receiver/Driver input/output. |
Q: Can I connect more than 32 devices on an RS485 bus? A: Yes, but you will need to use repeaters to extend the number of allowable nodes.
Q: How can I increase the maximum distance for RS485 communication? A: Lowering the baud rate can increase the maximum cable length.
Q: What is the difference between RS485 and RS232? A: RS485 supports multi-point connections and longer distances at higher speeds compared to RS232, which is for point-to-point communication.
#include <SoftwareSerial.h>
// RS485 control pins
const int RE_PIN = 2; // Receiver Enable
const int DE_PIN = 3; // Driver Enable
// Initialize software serial port
SoftwareSerial rs485Serial(10, 11); // RX, TX
void setup() {
pinMode(RE_PIN, OUTPUT);
pinMode(DE_PIN, OUTPUT);
// Start serial communication
rs485Serial.begin(9600);
Serial.begin(9600);
// Set RS485 module to receive mode
digitalWrite(RE_PIN, LOW);
digitalWrite(DE_PIN, LOW);
}
void loop() {
// Check if data is available to read
if (rs485Serial.available()) {
int incomingByte = rs485Serial.read();
Serial.print("Received: ");
Serial.println(incomingByte, DEC);
}
// Check if data is available to send from the serial monitor
if (Serial.available()) {
// Set RS485 module to transmit mode
digitalWrite(RE_PIN, LOW);
digitalWrite(DE_PIN, HIGH);
char outgoingByte = Serial.read();
rs485Serial.write(outgoingByte);
// Return to receive mode after sending
digitalWrite(RE_PIN, LOW);
digitalWrite(DE_PIN, LOW);
}
}
This example demonstrates basic RS485 communication with an Arduino UNO. The SoftwareSerial
library is used to create a serial port on pins 10 and 11. The RE and DE pins are controlled to switch between receiving and transmitting modes. Data received from the RS485 bus is printed to the Serial Monitor, and data entered into the Serial Monitor is sent onto the RS485 bus.