0

Friends, I am trying to achieve half duplex communication between two Arduinos using RS485 Protocol. The hardware I am using is a MAX485 module and an Arduino Uno. I am using the SoftwareSerial library for Arduino, but I am not be able to transmit data from sender to receiver. The length of the wire between MAX485 modules is 1 meter.

My question is, what wrong I am doing here, please guide me.

The code:

SenderDevice

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7);

String strCmd;
int counter;

void setup()
{

 Serial.begin(9600);
 mySerial.begin(9600);

 pinMode(5, OUTPUT);
 digitalWrite(5, HIGH);
 counter = 0;

}

void loop()
{

  counter++;
  strCmd = "Counter" + String(counter) + "$";
  Serial.println("Sending " + strCmd);
  mySerial.print(strCmd);

  delay(10);
  mySerial.flush();
  Serial.flush();
  memset(&strCmd, 0, sizeof(strCmd));
  delay(2000);

}

ReceiverDevice

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7);

String strCmd;

void setup()
{

 Serial.begin(9600);
 Serial.println("ready");

 pinMode(5, OUTPUT);
 digitalWrite(5, LOW);
 mySerial.begin(9600);

}

void loop()
{

   while (mySerial.available() > 0) {

        char c = mySerial.read();
        if (c == '$') {

           Serial.println("Received " + strCmd);
           memset(&strCmd, 0, sizeof(strCmd));

        } else {

          strCmd = strCmd + c;

        }

  }

}

My Wire Connections are :

(Both Sender & Receiver Device)
Arduino Uno 6 -> Max485 RO
Arduino Uno 7 -> Max485 DI
Arduino Uno 5 -> Max485 DE
Arduino Uno 5 -> Max485 RE
Arduino Uno 5V -> Max485 VCC
Arduino Uno GND -> Max485 GND

Sender Max485 A -> Receiver Max485 A
Sender Max485 B -> Receiver Max485 B
  • I have quite bad experience with using the software library using MIDI which has a very low baudrate; I advise to at least try with the hardware UART (RX/TX) to see if that solves the problem. – Michel Keijzers Mar 13 at 20:21
  • Do you have GND connected between sender and receiver? – Matej Mar 13 at 22:20
  • @MichelKeijzers : I am already using the hardware UART for someother reasons. – java bee Mar 13 at 23:29
  • @Matej : I have tried connection the ground of both devices too, But it did not work. In real time my application is going to be 15 meters away. – java bee Mar 13 at 23:30
  • I only said to try it, so you know if the problem is in the library/software; if it works with a hardware serial, than at least you know your code is ok. – Michel Keijzers Mar 13 at 23:35

Your Answer

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

Browse other questions tagged or ask your own question.