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

. . .

  • 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
  • 2
    Why are you using software serial on a mega...? – Majenko Dec 14 '17 at 9:18
4

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

  • 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 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 6:34
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.