Connect multiple Arduino using max487 and RS-485 bus/Mod-bus/Arduino ICSC library (explained with sample program and connection diagram)

Connect multiple Arduino using max487 and RS-485 bus/Mod-bus

In this post I would like to  explain the easiest way to implement the long distance wired communication between multiple Arduino's  by using  ICSC library and max487

"Arduino long distance - wired communication"

What is RS 485 /Modbus?

RS-485 is a multi-point communications standard set by the Electronics Industry Alliance (EIA) and Telecommunications Industry Association (TIA). RS-485 supports several connection types, including DB-9 and DB-37. Because of lower impedance receivers and drivers, RS-485 supports more nodes per line than RS-422.

max 487 with arduino UART

block diagram : multiple Arduino/PLC connection using modbus/RS485

          I would like to use ICSC library for this purpose - as it make my work more easier๐Ÿ’“ 

-Using Proteus for simulation

-MAX 487 as transceiver

-Arduino nano as controller unit


MAX XXX are  low-power  transceivers  for  RS-485  and  RS-422 communication. Each part contains one driver and one receiver.
The  MAX483MAX487MAX488,  and  MAX489 feature  reduced  slew-rate  drivers  that  minimize  EMI  and reduce reflections
caused by improperly terminated cables,thus  allowing  error-free  data  transmission  up  to  250kbps.

In ICSC library it is very easy to implement long distance communication , Without having the Headache over internal stuffs of RS485 protocol
All credits goes to the one who made this library !

1.Get the library from github
download -->extract -->paste it into library folder of Arduino

-I'm using Proteus for simulating the circuit/connection diagram

The picture below shows the

Simple connection diagram for one single Arduino /one node 

MAX487 single arduino node  connection diagram for modbus



In the above connection diagram there is a
switch -->which will send a message if pressed
Led -->For indicating incoming message
A & B Pins of MAX487 is the actual bus -->It will be connected to all the nodes/Arduino's in this network

 The communication network with multiple nodes are shown in below

multiple arduino modbus connection circuit diagram

       (A virtual terminal /serial monitor is connected to the station no.4 for debugging purpose)


 Arduino Code: 

#include <Arduino.h>
#include <ICSC.h>

//callbackFunctions

void pressed(unsigned char src, char command, unsigned char len, char *data)
{
  digitalWrite(10, HIGH);
}

void released(unsigned char src, char command, unsigned char len, char *data)
{
  digitalWrite(10, LOW);
}


void setup()
{
  ICSC.begin(3, 115200, 2);//my id, board-rate,de pin no
  ICSC.registerCommand('P', &pressed);
  ICSC.registerCommand('R', &released);
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);
  pinMode(9, INPUT_PULLUP);
}

void loop()
{
  static unsigned char lastPress = digitalRead(13);

  if (lastPress != digitalRead(9))
  {
    lastPress = digitalRead(9);

    if (lastPress == LOW)
    {
      ICSC.send(2, 'P', 20, "Test2");
      }
    else
    {
    ICSC.send(2, 'R', 20, "Test1");//destination id,event,length,msg
      }
  }
  ICSC.process();
} 

 

Code explanation:

void pressed and void released functions are event calls .

Those function will activated when it get a command  with ' P ' or ' R ' respectively.

i.e It is something which runs when the particular Node receive a command with respective character.--> here it is P or R -  which indicates press(P) and release(R) -- it is user defined character

In the above program we are just changing state of led while the function call happens(callbackFunction)

we are not doing anything with the incoming data !

if you want to any operation with the incomming data/message/command(whatever you call it๐Ÿ˜›) 

then the event call/ callback function will be:

 

void released(unsigned char src, char command, unsigned char len, char *data)
{

  if(data == "Test1") {  //or String(data)

   //do something

  }
 }

 

In setup() function - we have to initialise the node .


ICSC.begin(3, 115200, 2);//my id, board-rate,de pin no

here 3 is node number or personal ID of that particular Arduino.

115200 is the broard rate - at which it  sending messages and expecting the incoming message.

2 is the pin number of DE pin of MAX487

(As DE of MAX487 is connected to pin no. 2 of Arduino ) 


  ICSC.registerCommand('P', &pressed);
  ICSC.registerCommand('R', &released);

""register commands'"'

P/R is the char command and &released/&pressed are callbackFunction func for the respective events

i.e linking P with the function pressed

 In loop() function we are continusly checking the state of switch(pressed or released(0/1))

and send command to another node based on that , by using :

ICSC.send(2, 'P', 20, "Test2");

here 2 is the destination address -->it will send the message ("Test2") to another Arduino which has 2 as its ID.

P is the char command, 20 is length of message(just putting a random max length for  my message ! I knew its is grater than the actual size of the message ๐Ÿ˜›) , "Test2" is the message/data to be transmitted  

 

Okey, now what if you want to send a single/same message to all the nodes in the system! ?

yah brodcast message!

ICSC.broadcast('A',20,"for all");

This single line will do that work๐Ÿ’ฅ

                                                            ***

 

 
 

 

Read:

Locomotion in mobile robotics. 

What is mavros?.

Introduction to robotics. 

Different types of robots?

ROS : 5 Major advantages of using ROS.

Important branches of robotics?