HUM: RS485 MODULE

RS-485 transciever breakout-Communication

You are a beginner with Dasduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by soldered where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Dasduino and the basic code. Everything else is left to your imagination.

INTRODUCTION

RS485 is a standard of physical connectivity for data transmission that defines electrical characteristics of serial communication. It can be used to connect 2 or more microcontrollers as well as various I / O devices that support this standard. The characteristics of this standard make it very useful in data transmission over large distances in industrial facilities where electric noise is more pronounced.

RS485:
• Allows serial communication over large distances (up to 1.2 km (100 kbps max))
• Differential transmission
• Half-duplex
• High transmission speed (up to 10 Mbit/s (12 m max))
• Enables the connection of multiple transmitters / receivers along the same cable – „Multi-Drop“
• Voltage levels: +/- 1.5V do +/- 6V

HOW DOES IT WORK?

Let us first get acquainted with the electrical characteristics of the RS485 communication. Unlike the familiar RS232 standard which uses a single-ended signaling mode, RS485 uses the differential signaling which has its advantages.

Single-ended signaling is the simplest and most commonly used method of transmitting electric signals over wires (R232, I2C, PS/2, WGA,..). One wire carries a varying voltage that represents the signal, while the other one is connected to a reference voltage (usually ground, 0V). This type of system is less expensive to implement than differential, but it lacks the ability to reject noise caused by the differences in ground voltages between transmitting and receiving circuits or induction on the signal wires.

The main advantage of single-ended over differential signaling is that fewer wires are needed to transmit multiple signals. If there are n signals, then we need n+1 wires (one for each signal and one for ground), while the differential signaling uses at least 2n wires.

As the main alternative to single-ended signaling, we use the differential signaling (RS485, HDMI, USB,…). This method sends differential pairs of signals, each in its own conductor. The receiving side responds to the difference in voltages between the two signals, rather than reading the signal level in relation to the reference level (ground). The reason why this type of technique is more resistant to noise than the single-ended signaling is the fact that any external electromagnetic noise will equally affect both signals and voltage difference will remain the same. This is why this standard is suitable for use in industry.

RS485 differential signaling (n signal, 2n wires)

RS485 allows multiple devices to communicate in both ways but not simultaneously (half-duplex), i.e. the communication is possible in both ways, but when one master device “speaks” all other slave devices “listen”. Also, RS485 can be configured in full-duplex mode that allows simultaneous communication in both directions, but it requires 4 wires which is a bit more expensive to implement.

Turnaround time: since communication takes place in one and the other direction through the same pair, the device which has sent data so far must switch to the receiver mode. When designing a system, it is important to keep an eye on the interval during which the device is changing its state. If the interval is too long, the device may miss the first (starting) bit or the first few bits essential for further communication, and an error occurs. Usually, the interval takes the duration of the transfer of 1 symbol, depending on the baud rate. The good news is that most converters automatically perform this transition, so the developer’s work is easier.

Termination resistors: when sending bits from one end of the cable to another, there is always a reflection of the voltage that returns along the cable and is larger when the raising edge is steeper (sharper) and when the transmission lines are longer. This negatively affects communication, and for such a phenomenon to be reduced, resistors between lines A and B are used. Their value should be equal to the characteristic impedance of the lines (typically 120 Ohms per pair).

The image that shows signals with non-adapted (left) and adapted resistance (right)

Also, when sending data, high-frequency components that cause electromagnetic interference also appear with the emergence of a rising edge. At higher transmission rates and longer transmission lines, it is even more pronounced, and because of this, the signal lines are usually used in pairs (twisted pair).

Bias resistors: With the RS485 networks, it may happen that at one time none of the devices send data. Then, the differential distinction between the transmission lines is 0V and this is an indefinite state for the receivers. Such a condition may cause inaccurate data in the receiver due to the noise collected on the transmission lines, and for that purpose, resistors are used to set the signal lines to the proper voltage in the voltage configuration of the divider.

Arrangement of bias and termination resistors at the ends of the wires

 

 

 

HOW TO CONNECT IT?

GND ————> GND
Vcc ————-> +5 V
RO ————-> Pin 7
RE ————> Pin 13
DE ————> Pin 13
DI ————> Pin 6
A ————> A
B ————> B
RO (receiver output) – a pin that registers the voltage difference between signal lines A and B, and it will be high if A>B for 200mV, and low if A<B for 200mV. It is the pin through which the microcontroller receives data (Rx)
/RE (receiver output enable) – when RE is low, RO is enabled, i.e. this device switches to the receiver mode. When RE is high, RO is in high impendance state and the device can not receive data
DE (driver output enable) – if DE is high, the device is in the transmitter mode, if DE is low, the device can not send data
DI (driver input) – a pin through which the microcontroller sends data (Tx,) if it is set to high, line A is at 0V, while line B is at 5V, and vice versa for low

ARDUINO CODE

The following code will allow interchangeable communication using two Dasduino boards. First #1 sends what we have written in the SerialMonitor, #2 reads it and sends back the same data. If both sent and received data are equal to what is written in the SerialMonitor, the communication process is successfully and seamlessly executed.

TRANSMITER #1

#include "SoftwareSerial.h"
SoftwareSerial RS485(7, 6);  //RO, DI (receiver output, driver input) - Rx, Tx
                            //communication lines between Croduino and MAX485
                            
#define RE_DE 13            //receiver output enable_driver output enable
                            //RE_DE - HIGH  TRANSMITTER MODE
                            //RE_DE - LOW   RECEIVER MODE
                            
char data;        //data to be sent to the other Croduino
char data_returned;   //returned data from the other Croduino
void setup(){
  Serial.begin(9600);     //start serial communication
  RS485.begin(9600);      //start RS485 communication
  pinMode(RE_DE, OUTPUT);  
  digitalWrite(RE_DE, HIGH);
  Serial.println("Transmitter:");
}
void loop(){
  if(Serial.available()){       //check if there is any data available at serial monitor
    data = Serial.read();       //read that data and store it in a variable
    digitalWrite(RE_DE, HIGH);  //go into transmitting mode
    RS485.write(data);          //send data over RS485 lines
    digitalWrite(RE_DE, LOW);   //go back into listening mode
  }
  delay(50);
  if (RS485.available()){    //Look for response data from other Croduino
    data_returned = RS485.read();   //read that data
    Serial.print(data_returned);    //print on serial monitor
  
}

RECEIVER #2

#include "SoftwareSerial.h"
SoftwareSerial RS485(7,6);
#define RE_DE 13
char data_received;
void setup(){
  Serial.begin(9600);
  RS485.begin(9600);
  pinMode(RE_DE, OUTPUT);
  digitalWrite(RE_DE, LOW);
  Serial.println("Receiver:");
}
void loop(){
  if(RS485.available()){          //check if there is any data available at RS485 lines
    data_received = RS485.read();   //read and store that data in a variable
    Serial.print(data_received);    //output data on serial monitor
    delay(50);
    digitalWrite(RE_DE, HIGH);        // Enable RS485 Transmit   
    RS485.write(data_received);     // Send back read data 
    digitalWrite(RE_DE, LOW);       //return to the listening mode
  }
}