RS-485 Two Way Communications with Arduino

In this article, we will make two Arduino control each other by turning off /on their built in LED using the RS-485 module

You will need the following items to carry out this project

Hardware

  • 2 x Arduino UNO R3, get it here
  • 2 x RS-485 module, get it here
  • USB to RS-485 converter, get it here

Software

  • Latest Arduino IDE, get it here

Introducing the RS-485 module

The RS-485 module is a device that allows sending and receiving of data up to 1.2 km at a rate of 20 Mbps. It can support up to 32 devices. It runs off 5 volts meaning your Arduino can supply power to it.

RS-485
RS-485

IMPLEMENTATION

STEP 1: CIRCUIT CONNECTION

Below is the circuit diagram of the project.

rs-485 arduino circuit
rs-485 arduino circuit
RS-485 Arduino Connection

STEP 2: UPLOAD THE CODE

1. Start your Arduino IDE and copy the following code into your Arduino IDE

#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin

#define SSerialTxControl 3   //RS485 Direction control

#define RS485Transmit    HIGH
#define RS485Receive     LOW

#define Pin13LED         13

/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  Serial.println("RS-485 EXAMPLE");
  Serial.println("Use Serial Monitor, type Y/N to turn off/ON LED and press ENTER");
  
  pinMode(Pin13LED, OUTPUT);   
  pinMode(SSerialTxControl, OUTPUT);  
  digitalWrite(Pin13LED, LOW);  
  
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver   
  
  // Start the software serial port, to another device
  RS485Serial.begin(9600);   // set the data rate 

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if (Serial.available())
  {
    byteReceived = Serial.read();
    
    digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit   
    RS485Serial.write(byteReceived);          // Send byte to Remote Arduino    
    delay(10);
    digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit       
  }
  
  if (RS485Serial.available())  //Look for data from other Arduino
   {
     byteReceived = RS485Serial.read();    // Read received byte
     if (byteReceived == 'Y' || byteReceived == 'y')
     {
       digitalWrite(Pin13LED, HIGH);
     }
     else if (byteReceived == 'N' || byteReceived == 'n')
     {
       digitalWrite(Pin13LED, LOW);
     }    
     Serial.write(byteReceived);        // Show on Serial Monitor
     delay(10);
     digitalWrite(Pin13LED, LOW);  // Show activity   
   }  

}


2. Click the upload button

(Note: Upload code to both Arduino)

3. Open your serial monitor and type Y or N to turn on/off the built in LED

Feel free to write any comments, thank you.

Wanna do more?

Build a Carbon Monoxide sensor with LCD display. Get the parts

  • MQ-7 Carbon Monoxide sensor, get it here
  • 16 x 2 LCD module, get it here

 

2 Replies to “RS-485 Two Way Communications with Arduino”

Leave a Reply

Your email address will not be published. Required fields are marked *