Arduino Projects

Arduino Modbus RS485, Arduino RS485 Master and Slave

Arduino Modbus RS485, Overview:

Arduino Modbus RS485 Master and Slave– In this tutorial, you will learn how to communicate between two Arduino’s using Modbus Serial Communication protocol. One Arduino will act as the Master while the other Arduino will be configured as the Slave. Using the Arduino Modbus RS485 boards, one master Arduino can communicate with Multiple Slave Arduino’s.

Without any further delay, let’s get started!!!


Amazon Purchase links:

12v Adaptor:

Arduino Uno

Arduino Nano

max485 TTL RS485

Other Tools and Components:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

What is Modbus?

Modbus is a serial communication protocol developed by Modicon in 1979. It was created specifically for use in modicon PLC for industrial application but later it become standard  protocol used by a wide range of automation products for connecting industrial electronic devices. Modbus is used in monitoring; to communicate between intelligent devices, sensors or instruments and to monitor field devices using computer and HMI. It also a great protocol to use in RTU application where wireless communication is required  making it a great choice for gas and oil substation application because of its openness, simplicity, low cost development and minimum hardware requirement to support it over. Modbus has become very popular. Ethernet device is compatible with it.

In a typical Modbus implementation there is a Modbus master device and up to 247 slave devices attached to that master device each with their own unique address. The master device manages and writes data to the slave device. Data is sent over Modbus as a series of bits in the form of ones and zeros. Each of those bits is sent as voltage with zeros being sent as positive voltage and ones sent as negative voltage. Since the individual bits are extremely small. They are sent extremely quickly between the devices. There are basically three types or variations of the Modbus protocol:

  • Modbus ASCII
  • Modbus RTU
  • Modbus TCP and IP

Modbus ASCII:

Modbus was originally developed using ASCII character using messages and this version of the protocol is still in use today.



Modbus RTU:

Modbus RTU means remote terminal unit which is based on serial protocol which is used building management system and industrial automation system. It is also use in data system which can be connected up to 15 meters. It is point to point communication which means it has only one master and slave.

Modbus RTU is using binary coding and CRC error checking for transmission implementation. The two modes are incompatible when they do not use the same communication protocol for example if we are using binary or ascii at the transmission side than at the receiving end we will also use the same protocols, so a device is configured for ASCII mode cannot communicate with one using RTU. Modbus RTU devices typically use one of three electrical interfaces which are:

  • RS232
  • RS485
  • RS422

RS232:

RS232 was first introduced in 1962. This is the most common type of serial interface, it was the standard communication before the PS2 and USB became popular in the computer industry. RS232 is simple point to point arrangement which will send one bit at a time therefore it is known as serial communication. If we need to connect only one device to another and the distance between the two devices is less than 50 feet or 15 meters, then RS232 will do the job. RS232 is a specific serial protocol that defines the wiring and flow control used to send the data bits. It was originally designed to send data between computer using modems and telephone line. Let us consider two types of equipment’s that need to communicate with each other to keep it simple one device will only sends the data while other device will receive the data. In reality both sides send and receive data but if we want to understand how the communication works in one direction we can apply it to both. Data is generated by the sender which puts it in a buffer that temporarily holds the data. To get the data from the sender buffer through the buffer on the receiver we need a wire on the sender side. This wire will transmit data or the TX wire; on the receiver side receiver end it will receive data Rx wire. Once these are connected together the data can flow from the sender to the receiver. This is the simplest case but the designer of the RS-232 specification asked a few basic questions.

Arduino Modbus


What if the receiver is missing?

The sender would continue to transmit the data to nowhere so the RS232 designer added a control wire which will tells the sender that the receiver is there. On the sender side this wire is called the data terminal ready or DTR on the receivers end this is called the data set ready or DSR. When these are connected and the proper voltage is set by the receiver. The sender knows the receiver is there and once again data can safely flow but what if the sender transmit the data too fast how fast this data is sent is called the baud rate.

If the baud rate is so high that the receiver cannot get the data out of its buffer fast enough the buffer will overflows and once again the data is lost to counter this the designer added another control wire so the sender and the receiver can coordinate when to send the data on the sender side this is the request to send RTS on the receiver side this is clear to send or CTS with this additional line the receiver tells the sender when it needs to pause and buffer overruns are avoided using this stop signal is known as hardware flow control.

The RTS CTS wire is the hardware that controls when data flows between the device for completeness sake there are two more wires defined in the RS232. Both of them are related to the modems so seldom use anymore. These are the data carrier detect or DCD which tells the modem when an analogue signal is being received from another modem and the ring indicator that tells the modem that the phone is ringing since we are talking about modem. We also need ground wire to establish where the signal voltages are referenced from this third wire is called the signal ground or SG with these wires we can communicate between two RS232 devices an arrangement is descriptively called a three wire null modem.

The control wires handle making sure there is a device on both ends and that the receiver is ready to receive the data sent are no longer being done. We can perform the same flow control function by sending special packets of data over the transmit and receive lines using special packets to control when the data is sent is called software flow control and the packets themselves are called X on and X off. When the receiver is ready for the data it sends an X on when it needs the sender to stop it send an X bar. This is less reliable than hardware flow control since the flow control packets themselves are data but it often works well enough to be worthwhile.

Pin 1 DCD
Pin 2 RXD
Pin 3 TXD
Pin 4 DTR
Pin 5 GND
Pin 6 DSR
Pin 7 RTS
Pin 8 CTS
Pin 9 RI

 

We can connect mouse, printer and modem with RS232. RS232 also uses a duplex transmission method. RS232 can transmit up to 1 Mbs with maximum distance up to 30 feet. The normal speed of the RS232 is 19.2 Kbs.


RS422:

RS422 is an improved version of RS232 which used twisted pair cables to reduce the noise and interference; it uses signalling balancing to transmit data.

The signal becomes balanced it uses a voltage difference between the two lines as an indication of the signal value, with this method the data is able to transmit for a longer distance with faster data rates.

With RS422 the data can transmit up to 10 Mbps at 50 feet or 100Kbps or 4000 feet. RS422 is capable of multiple drop capability which it limits up to 10 slaves in the data line.

Pin 1 TXD –
Pin 2 TXD +
Pin 3 RTS –
Pin 4 RTS +
Pin 5 GND
Pin 6 RXD –
Pin 7 RXD +
Pin 8 CTS
Pin 9 CTS +

 

RS485:

RS485 is used to send data and receive serial data. If more than two devices connected serially then we can communicate devices with one another. To connect more than two devices on the same line and have a distance greater than 50 feet than we will use RS485 or RS422. RS485 can travel signal up to 1 KM. For master communicating with multiple slave devices RS485 is by far the most popular method. RS485 was developed by as a replacement or upgrade of RS432 as well as RS422.

It is widely used in the many applications ranging from business industrial communication to business and home device connection. This standard can support up to 32 nodes over a range up to 4000 feet which are roughly equal to 1200 meters without a repeater. The speed that Modbus massages are sent at is referred to as the baud rate or bits per second. All devices on an RTU network must use the same baud rate. Different devices support different transmission speed but between 9600 and 19200 bit per second is typical range. Modbus module can be configured from as low as 300 to as high as over 100000.

Arduino Modbus


RS485 connection with Arduino:

  • R0 (Receiver output) pin of RS485 is connected to controller receiver pin
  • RE (Receiver enable) and DE (driver enable) pin of RS485 are joined together, this pin is decide whether we want to transmit or receive signal.
  • When RE, DE=0 Receive signal mode
  • When RE, DE=1 transmit signal mode
  • DI (driver input) pin of RS485 is connected to another RS485 pin A and pin B.

RS485 sensor shield with Arduino:

In this project we are using two Arduino boards in which one will act as master and other will act as slave. Both boards will be connected with RS485 sensor shield. Connect the Vcc of the sensor shield with the 5V of the Arduino. Connect the ground of the sensor shield with ground of the Arduino. Connect the DE and RE pins of the sensor shield and then connect it with the digital pin 8 of the Arduino. Connect the DI pin of the RS485 sensor shield with the transmitter of the Arduino and connect the R0 pin of the RS485 shield with the receiver pin of the Arduino. Connect the pin B of the sensor shield 1 with the pin B of the sensor shield 1, connect the Pin A of the sensor shield 1 with the pin A of the sensor shield 2. Connect a push button with the Arduino such that one terminal of the push button is connected with the ground and other terminal of the push button is connected with the digital pin 3 of the Arduino.

Arduino Modbus

Arduino Modbus RS485, Code for the master Arduino:

DE and RE will act as enable which will be connected with digital pin 8, we define this pin in code as master pin and pin 3 will be declare as switch. The code is very simple. All the instructions are well commented. No header files are used.

#define MASTER_EN   8                 // connected to RS485 Enable pin
#define SWITCH      3                 // Declare LED pin

void setup() {
  pinMode(SWITCH , INPUT_PULLUP);     // Declare LED pin as output
  pinMode(MASTER_EN , OUTPUT);        // Declare Enable pin as output
  Serial.begin(9600);                 // set serial communication baudrate 
  digitalWrite(MASTER_EN , LOW);      // Make Enable pin low, Receiving mode ON
}

void loop() {
  if(digitalRead(SWITCH) == 0)
  {
    delay(1000);                        // Debouncing for switch
    digitalWrite(MASTER_EN , HIGH);     // Make Enable pin high to send Data
    delay(5);                           // required minimum delay of 5ms
    Serial.print("EMBEDDED GATE*");     // Send String serially, End String with *
    Serial.flush();                     // wait for transmission of data
    digitalWrite(MASTER_EN , LOW);      // Receiving mode ON
  }
}



Arduino Modbus RS485, Code for the Slave Arduino:

#define SLAVE_EN  8
#define LED       13
String recInput;                                // Variable to store Receive string 

void setup() {
  pinMode(LED , OUTPUT);                        // Declare LED pin as output
  pinMode(SLAVE_EN , OUTPUT);                   // Declare Enable pin as output
  Serial.begin(9600);                           // set serial communication baudrate 
  digitalWrite(SLAVE_EN , LOW);                 // Make Enable pin low, Receiving mode ON 
}

void loop() {
  while(Serial.available())                     // If serial data is available then enter into while loop
  {
    recInput = Serial.readStringUntil('*');     // Receive Serial data in Variable
    Serial.print(recInput);                     // Print Data
    
    if(recInput == "EMBEDDED GATE")             // Compare Data
    {
      digitalWrite( LED , HIGH);    // LED ON
      delay(1000);
      digitalWrite( LED , LOW);     // LED OFF
    }
  }
}


Arduino Modbus RS485 based Project Working:

After you upload the Master and slave codes then the final step is to open the serial monitor for the Slave Arduino. On the Master side when you press the button and led on the Slave side Arduino will start blinking.

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

2 Comments

  1. Can teach how to send data from slave to master? Or add sensors on slave and send data to master which has some LEDs

Leave a Reply

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

Back to top button