-1

I'm trying to achieve RS485 communication between Arduino Mega and Arduino UNO as specified in following link:

https://www.engineersgarage.com/arduino/rs485-communication-between-arduino-mega-and-arduino-pro-mini

But now my problem is The master i.e. Arduino Mega doesn't print anything on Serial Monitor.

Please help me out to solve this issue, trying from week still no success.


Sir Peter Paul Kiefer,

Im attaching my circuit setup

enter image description here

Master Code/Mega code

void setup() 
{  
  //Using Serial1 Port
  Serial1.begin(9600);
  Serial.begin(9600);

  //DE/RE Controling pin of RS-485
  pinMode(8, OUTPUT);
}

void loop() 
{
  char getdata='m';

  //DE/RE=HIGH Transmit Enabled M1
  digitalWrite(8,HIGH);

  //Write '9' and Fetch Data From Pro Mini
  Serial1.print('9');

  //DE/RE=LOW Receive Enabled M1
  digitalWrite(8,LOW);
  delay(1000);

  //If Serial Data is available
  if(Serial1.available())
  { 
    while( Serial1.available() && getdata!='d' )
    { 
      getdata=Serial1.read();
      Serial.print(getdata);
    }

    Serial.println("");
  }
}

Slave code

void setup() 
{
  //Serial1.begin(9600);//Uncomment for Arduino Lenardo
  Serial.begin(9600);
  //while(!Serial1);//Uncomment for Arduino Lenardo

  //Led Connected
  pinMode(13, OUTPUT);

  //DE/RE Controling pin of RS-485
  pinMode(8, OUTPUT);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  char getdata='c';

  //Led OFF
  digitalWrite(13,LOW);
  //DE/RE=LOW Receive Enabled
  digitalWrite(8,LOW);

  if(Serial.available())
  {
    getdata=Serial.read();
  }

  if(getdata=='9')
  {
     //DE/RE=HIGH Transmit Enabled 
     digitalWrite(8,HIGH);

     Serial.print("AcruxTek");
     Serial.print("Isld");
  }

  delay(2000);
  //Led ON
  digitalWrite(13,HIGH);
  delay(2000);
}

I connected gnd of RS485 converter to arduino gnd and also i did common gnd between circuits but still no output. I used same code nothing changed.. except one thing the example uses arduino pro mini but i used arduino uno as slave

  • Sorry. The link doesn't work. And you did not specify your problem in any way. We can not help you with knowing your setup. Just showing like others have done, gives us no hint of what you have done. So how can we help? I downvoted you, because it seems as a commercial for an diy page and of cause, I may be wrong. Edit your question, give us information about your own setup, perhaps a schematic (even hand drawn) and your code you've written so far. Then I will remove my downvote and probably I can even help you. – Peter Paul Kiefer Oct 25 '19 at 15:10
  • 1
    Oh, I've seen you have included the closing braket in the url. If I remove it the link works. I'll draw my down vote back. But you should give us an overview of your own project, we can not find out out what's your problem if we study a working project from someone other. – Peter Paul Kiefer Oct 25 '19 at 15:16
  • Im trying to read energy meter holding register data through RS485. As given in my previous link im using RS485 to TTL converter for that purpose. I'm not success in getting data from energy meter. so i thought of trying basic example of RS485 commuication between ARduino Mega and Arduino Uno using this converter. AS I said serial monitor doesn't print anything – priya Oct 26 '19 at 6:03
  • Yes I understood that, you have problems with connecting two Arduinos over a RS485 link using the mentioned Interfaces/adapters. But how did you do that? Please give a description of what you did. The project from the link should work, so what did you different. What about your program code. Have you a typo in it. How did you connect the parts? I do not know because I can not have a look into it. I would really like to help you, but I can't without information. HINT: It would be better to edit your question with the new information. – Peter Paul Kiefer Oct 26 '19 at 9:03
  • 3
    Please put additional information in your question, you can edit it! Don't write an answer, I just found it by accident. And as PeterPaulKiefer said, we need to see your source code or a note that you use the linked code unchanged. Did you connect GND between the Arduinos and the RS485 modules? – the busybee Oct 28 '19 at 12:26
0

I think there might be two issues in the engineering garage code. delay() function parameter in both codes is not matched. And another thing might cause problem is"Arduino Serial monitor hardware serial is already connected with another usb-serial converter ic which you are using for code uploading, so you cannot use this serial port for rs-485. So using software serial of Arduino pro-mini is a safe option. Finally you have to connect the software Serial pins with rs-485 converter. I have modified the code a little bit and attached the code herewith.

Arduino Mega code

void setup() 
{ 
  Serial1.begin(9600);//Using Serial1 Port
  Serial.begin(9600);
  pinMode(8, OUTPUT);//DE/RE Controling pin of RS-485
}

void loop()
{
  digitalWrite(8,HIGH);//DE/RE=HIGH Transmit Enabled M1
  Serial1.print('9');//Write '9' and Fetch Data From Pro Mini




  int timeOut = 0;
  digitalWrite(8,LOW);//DE/RE=LOW Receive Enable
  while(!Serial1.available() && timeOut < 5000)
  {
    delay(1);
    timeOut++; // wait 5s to get response from slave
  }
  String response = Serial1.readString();
  Serial.println(response);
}

Arduino Pro-Mini Code

#include <SoftwareSerial.h>
#define RS485_RX 2
#define RS485_TX 3
SoftwareSerial mySerial(RS485_RX,RS485_TX);//Creating object

void setup() 
{ 
  //Serial1.begin(9600);//Uncomment for Arduino Lenardo
  Serial.begin(9600);
  mySerial.begin(9600);
  //while(!Serial1);//Uncomment for Arduino Lenardo

  pinMode(13, OUTPUT);//Led Connected
  pinMode(8, OUTPUT);//DE/RE Controling pin of RS-485
}

void loop() 
{
  digitalWrite(13,LOW);//Led OFF
  digitalWrite(8,LOW);//DE/RE=LOW Receive Enable

  while(!mySerial.available())
  {
    delay(1);
  }

  String res = mySerial.readString();
  if (res == "9")
  {
     digitalWrite(8,HIGH);//DE/RE=HIGH Transmit Enabled 
     mySerial.print("AcruxTek");
     mySerial.print("Isld");
  }
  digitalWrite(13,HIGH);//Led OFF
}
| improve this answer | |
  • Thanks for your reply sir, Ill definitely try your suggestions and let you know the results – priya Oct 26 '19 at 6:08
  • Sir, I tried your code still there is not output coming in master i.e. mega serial monitor – priya Oct 28 '19 at 9:31
0

It's not really clear from the picture, but it seems that, the two adapter have no common ground. i.e you have to connect the ground (GND) pin of one the adapter to the GND pin of the other one. But do not remove the connection between the GND pins and the arduino GND pins.

I also assume that the mega-adapter is powered with 3.3 V and the UNO-adapter is powered with 5 V (or 3.3V I can not see). This might cause a problem, because the MAX485 chip needs a 5V power supply.

EDIT:

Sorry by a second view a saw, the mega delivers 5 V. So possibly you only have to connect the GNDs of the adapters to solve your problem.

EDIT 2:

I guess the little yellow wire in front of of both adapters connects the DE pins of them. The DE pin of each adapter is also connected to the respective Arduino PIN 8. So it doesn't matter which Arduino is requesting to switch the adapter into the send mode. Both adapters are sending. As far as I know, the adapter only work in Half Duplex you mus define the direction of the data flow. If one adapter is in send mode the other must be in receive mode.

It might also be possible that the DE and RE PIN of each adapter is connected. But this seams strange to me also. With that I would say you setup up one adapter in both receive and send mode and the other in none of them.

EDIT 3:

By looking into the MAX485 Spec. I saw that RE is LOW active but labeled as RE (which I understood as HIGH active), so sorry the last paragraph of my EDIT 2 is nonsense. So, perhaps it's a matter of timing or (more likely) there is still something wrong with the wiring.

EDIT 4:

In the picture I see that you connect the hardware tx and rx pins to the adapter. But Software Serial needs its own pins. Pin0 and Pin1 are connected with the PC Serial Terminal over the built in USB to Serial adapter of the arduino UNO. That's the reason why you need the software serial solution on an arduino. You need a second serial interface and UNO has only one of them which is already used for the connection to the PC (Programming and serial monitor).

In the code I don't see the initialization of the software serial port. This is also necessary:

#include <SoftwareSerial.h>

const byte rxPin = 2;
const byte txPin = 3;

// set up a new serial object
SoftwareSerial mySerial (rxPin, txPin);
| improve this answer | |
  • @priya I rejected your edits of my Answer because I think, it might confuse the readers. I appended the text to this comment: "As per your suggestion I connected RE pin to pin 9 of Arduino for both MEGA and UNO boards and set the logic level. But still no output. Can you suggest any test that can be done to ensure that RS485 converters are in working condition or damaged?" – Peter Paul Kiefer Nov 7 '19 at 8:57
  • I formatted you code, so I can read it better. Parts of the Code are missing there should be a line SoftwareSerial Serial1( rxPin, txPin ); Oh wait... In the picture I see that you connect the hardware tx and rx pins to the adapter. But Software Serial needs its own pins. The other are connected with the PC Serial Terminal. I'll edit my answer. – Peter Paul Kiefer Nov 7 '19 at 9:18
  • Sir I tried the code provided by Shuvangkar Shuvo which uses software serial still no success – priya Nov 7 '19 at 9:37
  • Did you reconnect the DI and RO pins of the adapter to Pin2 and Pin3 of the UNO? In the picture you used the wrong pins (pin0 and pin1) of the UNO. – Peter Paul Kiefer Nov 7 '19 at 10:39
  • Yes Sir, I connected DI pin to pin 3 and RO pin to pin 2 of arduino UNO – priya Nov 7 '19 at 13:52

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.