Introduction
RS-485, also known as EIA-485, is defined as a differential multipoint bus system; it is perfect to transmit data with high speed to ranges up to 12m. One of its most important characteristics is that its twisted pair of cables reduce the noise-induced in the transmission line. Multiple receivers may be connected to such a network in a linear, multi-drop bus. These characteristics make RS-485 useful in industrial control systems and similar applications.
Previous reading
We recommend you read the following posts in order to understand the program of this post. We used the following blog posts to do this example:
How to program our industrial PLC with Arduino IDEÂ Â How to install Industrial Shields boards in the Arduino IDE.
Requirements
In order to work with serial TTL communication, you will need any of our industrial programmable logic controllers for industrial automation:
Industrial Shields controllers: Wifi & Bluetooth Controller Family, Ethernet Controller Family, GPRS / GSM Controller Family.
Hardware
To make the connection between the devices, we follow the schematic below.
Software
The following code has the function of communicating the Master and the Slave sending the instruction to activate the relay to turn on the water pump.
An example of Simplecomm Master communication:
#include <RS485.h>
#include <SimpleComm.h>
// Create SimplePacket for sending and receiving data
SimplePacket packet;
// Define master address
uint8_t masterAddress = 0;
// Define slave address to communicate with
uint8_t slaveAddress = 1;
// Value to send as packet data
int value = 5;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);
 // Start RS485
 RS485.begin(19200L);
 RS485.setTimeout(20);
 // Start SimpleComm
 SimpleComm.begin(masterAddress);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
 static unsigned long lastSent = millis();
 // Send packet periodically: once per second
 if (millis() - lastSent >= 10000) {
 // Set request packet data
 packet.setData(value);
 // Send request to slave
 if (SimpleComm.send(RS485, packet, slaveAddress)) {
 lastSent = millis();
 Serial.print("Sent value: ");
 Serial.println(value);
 }
 }
 // Get responses
 if (SimpleComm.receive(RS485, packet)) {
 // Update value from the response
 value = packet.getInt();
 Serial.print("Received value: ");
 Serial.println(value);
 }
}
An example of Simplecomm Slave communication:
#include <SimpleComm.h>
// Create SimplePacket for sending and receiving data
SimplePacket request;
SimplePacket response;
// Define slave address to communicate with
uint8_t slaveAddress = 1;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);
 // Start RS485
 RS485.begin(19200L);
 RS485.setTimeout(20);
 // Start SimpleComm
 SimpleComm.begin(slaveAddress);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
 // Get requests
 if (SimpleComm.receive(RS485, request)) {
 int value = request.getInt();
 Serial.print("Received value: ");
 Serial.println(value);
if ( value==5){
 digitalWrite(R0_8,HIGH);
 delay(5000);
 digitalWrite(R0_8,LOW);
}
 // Process value
 //value++;
 // Send response to the request packet source
 response.setData(value);
 if (SimpleComm.send(RS485, response, request.getSource())) {
 Serial.print("Sent value: ");
 Serial.println(value);
 }
 }
}
Â
Â
#include <SimpleComm.h>// Create SimplePacket for sending and receiving data
SimplePacket packet;
// Define master address
uint8_t masterAddress = 0;
// Define slave address to communicate with
uint8_t slaveAddress = 1;
// Value to send as packet data
int value = 5;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);
 // Start RS485
 RS485.begin(19200L);
 RS485.setTimeout(20);
 // Start SimpleComm
 SimpleComm.begin(masterAddress);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
 static unsigned long lastSent = millis();
 // Send packet periodically: once per second
 if (millis() - lastSent >= 10000) {
 // Set request packet data
 packet.setData(value);
 // Send request to slave
 if (SimpleComm.send(RS485, packet, slaveAddress)) {
 lastSent = millis();
 Serial.print("Sent value: ");
 Serial.println(value);
 }
 }
 // Get responses
 if (SimpleComm.receive(RS485, packet)) {
 // Update value from the response
 value = packet.getInt();
 Serial.print("Received value: ");
 Serial.println(value);
 }
}
An example of Simplecomm Slave communication:
#include <SimpleComm.h>
// Create SimplePacket for sending and receiving data
SimplePacket request;
SimplePacket response;
// Define slave address to communicate with
uint8_t slaveAddress = 1;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);
 // Start RS485
 RS485.begin(19200L);
 RS485.setTimeout(20);
 // Start SimpleComm
 SimpleComm.begin(slaveAddress);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
 // Get requests
 if (SimpleComm.receive(RS485, request)) {
 int value = request.getInt();
 Serial.print("Received value: ");
 Serial.println(value);
if ( value==5){
 digitalWrite(R0_8,HIGH);
 delay(5000);
 digitalWrite(R0_8,LOW);
}
 // Process value
 //value++;
 // Send response to the request packet source
 response.setData(value);
 if (SimpleComm.send(RS485, response, request.getSource())) {
 Serial.print("Sent value: ");
 Serial.println(value);
 }
 }
}
Â
Â
#include <RS485.h>
Subscribe to our  YouTube channel if you want to keep up to date with our tutorials.
How to connect two industrial PLCs through RS-485