2

I'm building a Master-Slave system using some arduino pro mini as slaves and a Mega 2560 as Master to communicate over RS485 config via MAX485. Since i'm newbie in RS485 i know that i need to put some termination resistor somewhere is the line but i don't know where and how, also i don't know how much ohm should it be. My Baud rate is 9600 and i'm using twisted pair cable (CAT5) and it's 120 meters long.

UPDATE 1: My goal is to get data from each slave time to time, every slave is connected to a temperature sensor and sends temperature value to master on demand (when master asks for it.) The master asks for temperature value from all slaves separately (of course!) and repeatedly every 500 ms. First it asks slave 1 to send data and then slave 1 sends data to master, then master asks slave 2 to send data and slave 2 sends data to master and so on. after 500 ms, it does this again.

please see the schematic photo

UPDATE 2:

Master side code (Mega 2560):

#include <AltSoftSerial.h>
#define DE 2 //RS485 Data enable pin. HIGH=Transmit, LOW=Receive
String inputS = "";
boolean stringComplete = false;
AltSoftSerial altSerial;

void setup() {
 altSerial.begin(9600);
 Serial.begin(9600);
 pinMode(DE, OUTPUT);
}

void loop() {
 digitalWrite(DE, HIGH); //Transmit
 altSerial.println("MEGA:ProMini1");
 altSerial.flush();
 digitalWrite(DE, LOW); //Receive
 CheckAndReceiveFromMax(); //Check if data received
 if(stringComplete){
   inputS.trim();
   if(inputS.equals("ProMini1:ACK"))
    Serial.println("MEGA: ProMini1 Acknowledged: " + inputS);
   else
    Serial.println("MEGA: Not valid On ProMini1: " + inputS);
   stringComplete = false;
   inputS = "";
 }
delay(100);
digitalWrite(DE, HIGH); //Transmit
altSerial.println("MEGA:ProMini2");
altSerial.flush();
digitalWrite(DE, LOW); //Receive
CheckAndReceiveFromMax();
if(stringComplete){
  inputS.trim();
  if(inputS.equals("ProMini2:ACK"))
    Serial.println("MEGA: ProMini2 Acknowleged: " + inputS);
  else
    Serial.println("MEGA: Not valid On ProMini2: " + inputS);
  stringComplete = false;
  inputS = "";
}
delay(500);
}

void CheckAndReceiveFromMax()
{
 while (altSerial.available()) {
 char inChar = (char)altSerial.read();
 inputS1 += inChar;
 if (inChar == '\n')
  stringComplete1 = true;
 }
}

Slave side code (Pro Mini1):

#include <AltSoftSerial.h>
#define DE 7
String inputS = "";
boolean stringComplete = false;
AltSoftSerial altSerial;

void setup() {
 altSerial.begin(9600);
 Serial.begin(9600);
 pinMode(DE, OUTPUT);
}

void loop() {
 digitalWrite(DE, LOW); //Receive
 CheckAndReceiveFromMax(); //Check if data received
 if(stringComplete){
  Serial.print("String received: ");
  inputS.trim();
  Serial.println(inputS);
  if(inputS.equals("MEGA:ProMini1")){
   Serial.println("ProMini1: Mega called me!");
   digitalWrite(DE, HIGH); //Transmit
   altSerial.println("ProMini1:ACK");
   altSerial.flush();
   digitalWrite(DE, LOW);
  }
 stringComplete = false;
 inputS = "";
 }
}

void CheckAndReceiveFromMax()
{
 while (altSerial.available()) {
 char inChar = (char)altSerial.read();
 inputS += inChar;
 if (inChar == '\n')
  stringComplete1 = true;
 }
}

And the same code for ProMini2, ProMini3 and so on.

OUTPUTS: When master calls ProMini1, the ProMini2 answers, and when master calls ProMini2, ProMini1 aswers. Also, received data is not always in correct format.

Server Serial Monitor:

 MEGA: Not valid On ProMini1:  roMini1:ACK
  :
MEGA: Not valid On ProMini2: ProMini1:ACK
MEGA: Not valid On ProMini1:  roMini1:ACK

MEGA: Not valid On ProMini2: ProMini1:ACK


MEGA: Not valid On ProMini2:  roMini1:ACK

. . .

Safa Seed
  • 81
  • 2
  • 9
  • 1
    are you supplying power through the cable? if yes, what is the power voltage at each arduino? – jsotola Dec 13 '17 at 21:32
  • in this schematic yes, but in test environment i used short wire for power supply for each node, but i considered common ground. 5v – Safa Seed Dec 13 '17 at 21:34
  • 1
    remove the ground connections. if the cable is shielded, then connect the shield to ground at one place only. .... have you tested your devices by connecting them a short distance apart? (all on the same bench, for example) – jsotola Dec 13 '17 at 21:40
  • i tested it with short wire just between 1 master and 1 slave and worked properly – Safa Seed Dec 13 '17 at 22:02
  • Please try it on short wires with all slaves. – AltAir Dec 13 '17 at 22:42
  • why would you not test multiple slave devices? ... how do you even know that your software works correctly? ... you are making assumptions. there is an old saying when you assume, you make an ass of u and me ... lol – jsotola Dec 13 '17 at 23:21
  • You should find your answer here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/763 – Majenko Dec 13 '17 at 23:53
  • @Majenko and dear jsotola: Question Updated – Safa Seed Dec 14 '17 at 06:33
  • 2
    Why are you using software serial on a mega...? – Majenko Dec 14 '17 at 09:18
  • @Majenko: Does it matter ? I can use hardware serial, but no difference – Safa Seed Dec 14 '17 at 12:13
  • Software serial (of any form) is frowned upon and only used as a last resort. – Majenko Dec 14 '17 at 12:14
  • You might want to take a look at my ICSC library. It's designed for just this kind of environment. https://github.com/MajenkoLibraries/ICSC – Majenko Dec 14 '17 at 12:15
  • @Majenko: your library seems great but there is no tutorial of how to use it and the function list. Can you give me a link of it if exists ? thanks. – Safa Seed Dec 14 '17 at 13:10
  • @SafaSeed The examples show the use of pretty much all the functions. There's not many. You can find them in the header file near the bottom. – Majenko Dec 14 '17 at 13:28
  • @Majenko if you mean "PingMaster", "PingSlave", "RemoteLEDReceiver" and "RemoteLEDSender" they don't give me of how can i use it in my rs485 bus. For example nowhere in the code DE pin of MAX485 is mentioned! Im totally confused – Safa Seed Dec 14 '17 at 13:41
  • @SafaSeed Here is an old wiki page - some things may have changed slightly, but not much: https://sourceforge.net/p/arduino-icsc/wiki/Home/ – Majenko Dec 14 '17 at 13:43
  • @Majenko: Please answer to this question: https://arduino.stackexchange.com/questions/47748/problem-using-icsc-by-majenkotech-in-rs485-bus – Safa Seed Dec 14 '17 at 15:50

2 Answers2

4

For low data speeds, like 9600bps, termination is usually not necessary.

jose can u c
  • 6,974
  • 2
  • 15
  • 27
  • But i cannot get correct data. it works properly when there is just one master and one slave, but as i add a new slave the data received by both master and slaves are incorrect – Safa Seed Dec 13 '17 at 21:30
  • 3
    Your problem may or may not be related to termination. If you revise your question to include more information about what you expect to see, and what you see instead, as well as more information about your setup, such as an example of your code for communication, someone may spot some other solution. See also https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – jose can u c Dec 13 '17 at 21:39
  • Question updated :) – Safa Seed Dec 13 '17 at 22:00
  • 1
    So what does the incorrect data look like? Do you get some answer with wrong values? What values do you send and expect to receive? – jose can u c Dec 13 '17 at 22:25
  • Question Updated – Safa Seed Dec 14 '17 at 06:34
2

try 120ohm carbon resistor, not wirewound, at each end of the line ( two resistors in total)

jsotola
  • 1,505
  • 2
  • 11
  • 19