Home / Tutorials / Arduino Tutorial / Arduino RS-485 Protocol Tutorial
pcbway
RS485-TTL-Module

Arduino RS-485 Protocol Tutorial

RS-485 is a popular serial communication standard used in industrial control systems, automation, and other applications where long-distance communication is required. Unlike RS-232, RS-485 uses differential signaling, which makes it more robust to noise and interference, making it ideal for industrial environments. Moreover, RS-485 supports multi-device communication on the same bus, allowing for a master-slave or peer-to-peer communication model, with up to 32 devices typically supported.

Why Use RS-485?

RS-485 is highly reliable for long-distance communication (up to 1.2 km at lower baud rates) and is often used in environments prone to electromagnetic interference (EMI), such as factories or industrial sites. It allows multiple devices to share a single bus, which is a major advantage in systems that require distributed control, such as sensors, actuators, and controllers communicating over the same wire.

Using RS-485 with Arduino

To establish RS-485 communication with an Arduino, you’ll need an RS-485 module, such as the MAX485 or other similar transceiver modules. These modules convert TTL-level signals from the Arduino to RS-485 differential signals, enabling communication with other RS-485 devices.

Here’s how you can wire and code an Arduino for RS-485 communication:

Components Needed:

  1. Arduino (any model like UNO, Nano, etc.)
  2. RS-485 Module (MAX485 or equivalent)
  3. Jumper wires
  4. Power supply (optional)

RS-485 Pinout (Typical for MAX485):

  • DI (Driver Input): Connects to Arduino’s TX pin (transmit data).
  • RO (Receiver Output): Connects to Arduino’s RX pin (receive data).
  • DE (Driver Enable): Used to switch between transmit and receive modes.
  • RE (Receiver Enable): Also controls receive mode. Often tied with DE.
  • A/B: The differential data lines used for RS-485 communication.

Wiring Diagram:

Arduino Pin RS-485 Module Pin
TX (Pin 1) DI
RX (Pin 0) RO
Digital Pin 2 DE (Driver Enable)
Digital Pin 3 RE (Receiver Enable)
GND GND
5V VCC
A/B A/B (RS-485 bus)

Arduino RS-485 Tutorial

For multi-device communication, connect all the A terminals together, and all the B terminals together. Ensure termination resistors (typically 120 ohms) are placed between the A and B lines at the bus ends to reduce signal reflections.

Example Code

Here is an example sketch to demonstrate sending and receiving data using RS-485:

Transmitter Code (Master):

#define DE_PIN 2  // Define Driver Enable pin
#define RE_PIN 3  // Define Receiver Enable pin

void setup() {
  pinMode(DE_PIN, OUTPUT);
  pinMode(RE_PIN, OUTPUT);

  // Enable RS-485 transmitter
  digitalWrite(DE_PIN, HIGH);
  digitalWrite(RE_PIN, HIGH);  // RE tied with DE to switch between TX and RX

  Serial.begin(9600); // Start Serial communication at 9600 baud rate
}


void loop() {
  // Transmit message every 1 second
  const char message[] = "Hello from Master";

  // Enable transmission mode
  digitalWrite(DE_PIN, HIGH);
  Serial.write(message, sizeof(message) - 1);  // Send data over RS-485
  delay(100);  // Wait for data to finish sending
  digitalWrite(DE_PIN, LOW);  // Switch to receiving mode

  delay(1000);  // Wait before next message
}

Receiver Code (Slave):

#define DE_PIN 2  // Define Driver Enable pin
#define RE_PIN 3  // Define Receiver Enable pin

void setup() {
  pinMode(DE_PIN, OUTPUT);
  pinMode(RE_PIN, OUTPUT);
  
  // Enable RS-485 receiver
  digitalWrite(DE_PIN, LOW);
  digitalWrite(RE_PIN, LOW);  // RE tied with DE for receiving mode

  Serial.begin(9600); // Start Serial communication at 9600 baud rate
}


void loop() {
  // Check if data is available to read
  if (Serial.available()) {
    while (Serial.available()) {
      char received = Serial.read();  // Read received data byte by byte
      Serial.print(received);         // Print received data to Serial Monitor
    }
    Serial.println();  // Add a newline after message is received
  }
}

Explanation:

  1. Transmitting (Master):
    • The DE_PIN and RE_PIN are set high to enable the transmit mode.
    • The message is sent using Serial.write(), and then the DE pin is set low to switch back to receive mode after transmission.
  2. Receiving (Slave):
    • The DE and RE pins are kept low to enable the receiver mode.
    • The slave reads incoming data using Serial.read() and prints it to the Serial Monitor.

Why Are Termination Resistors Needed?

If you are using the RS485 to TTL module shown in the images above, you don’t need terminating resistors as they are already mounted on the modules!

In an RS-485 bus, the communication lines (A and B) form a transmission line. Without termination, the signals transmitted through the A and B lines can reflect back when they reach the end of the line, causing interference and data corruption. Termination resistors, typically 120 ohms, are placed at each end of the bus to absorb the signal energy and prevent reflections, ensuring a clean and stable signal.

When Do You Need Termination Resistors?

  • Long Distances: If your RS-485 network spans a considerable length (typically more than 10 meters or 30 feet), termination resistors are essential to maintain data integrity.
  • Higher Baud Rates: For higher communication speeds (e.g., 115200 baud or more), the transmission time is shorter, and reflections can cause significant issues without termination.
  • Multiple Devices: If you have multiple devices on the RS-485 bus, the combined signal reflections from the un-terminated ends could affect the communication quality.

When Can You Avoid Termination Resistors?

  • Short Distances: In very short distances (under a few meters), termination resistors may not be strictly necessary since the signal reflections will be negligible.
  • Lower Baud Rates: If your baud rate is relatively low (e.g., 9600 baud or less) and the cable distance is short, the communication may work reliably without termination resistors.

Placement of Termination Resistors

  • Place one 120-ohm resistor between A and B at each end of the RS-485 bus.
  • If you only have two devices (one transmitter and one receiver), place a resistor at each device. For multi-node setups, place resistors at the two devices that are physically at the ends of the bus, not at each device in between.

Error Handling

In a real-world application, you might need to implement error checking, such as:

  • Timeout mechanisms for checking if a message has been lost.
  • CRC (Cyclic Redundancy Check) or Checksum for verifying data integrity.

Conclusion

RS-485 is an ideal choice for reliable communication in harsh environments where long distances are involved. Using Arduino with RS-485 modules allows you to create robust communication networks between multiple devices like sensors, actuators, and controllers. The example code and setup provided demonstrate how to easily get started with RS-485 communication on the Arduino platform, offering a practical solution for industrial automation or distributed control systems.

Check Also

Arduino data types

Converting Between Data Types in Arduino

When writing code for Arduino, you’ll often need to convert between different data types. This …

Index