Error Serial Communication Arduino

Hi all..
I'm trying to test my RS485 module. When I tried with simple sending, using Arduino Nano as Slave and Arduino Mega as master, the message "GET%" could not be read by slave. Meanwhile, when I swapped, arduino nano became Master and arduino mega became Slave, the command "GET%" can be read, but sending data from slave can only be read half by arduino Nano. Is the cause in my RS485 module or in the code or what?

Please answer, I know this is a silly question but I really need help.
MASTER

#include <SoftwareSerial.h>
#define enTxPin 4   // HIGH: TX and LOW: RX 
SoftwareSerial mySerial(2, 3); // RX, TX

void setup() {
  Serial.begin(9600);
  Serial.println("Starting System");

  mySerial.begin(9600);

  pinMode(enTxPin, OUTPUT);
  digitalWrite(enTxPin, HIGH);       // default TX
}

void loop() {
  mySerial.print("GET%");
  digitalWrite(enTxPin, LOW);       // change pin to RX
  unsigned long timeLimit = millis();
  Serial.println("waiting data");
  while (millis() - timeLimit < 3000) {
    if (mySerial.available()) {
      String dataIN = "";
  
      dataIN = mySerial.readStringUntil('%');
      Serial.println(dataIN);
      digitalWrite(enTxPin, HIGH);       // default TX
      break;
    }
  }
  Serial.println("waiting loop");
  Serial.println();
  delay(5000);
}

SLAVE

#include <SoftwareSerial.h>
#define enTxPin 2   // HIGH: TX and LOW: RX 
SoftwareSerial mySerial(11, 12); // RX, TX

void setup() {
  Serial.begin(9600);
  Serial.println("Starting System");

  mySerial.begin(9600);

  pinMode(enTxPin, OUTPUT);
  digitalWrite(enTxPin, LOW);       // default RX
}

void loop() {
  if (mySerial.available()) {
    String dataIN = "";

    dataIN = mySerial.readStringUntil('%');
    Serial.println(dataIN);

    if (dataIN.substring(0,3) == "GET"){
      Serial.println("kirim data ke master");
      digitalWrite(enTxPin, HIGH);       // change pin to HIGH for TX
      String dataOUT = "jarak1#35%";
      mySerial.print(dataOUT);
      digitalWrite(enTxPin, LOW);       // default RX
    }
  }

  delay(10);
}

Result when Arduino Nano becomes Master and Arduino Mega becomes Slave.

Note: Please don't ask why I'm using Serial Software on Arduino Mega, I know I can use Serial1, I'm just trying to copy and paste the code I got on YouTube.

Thanks

Why are you using a RS485 module between a Nano and a Mega ?

I'm planning to make a Server Room Monitoring System project with 1 Master and 2 Slaves. Slave 1 sends temperature, humidity (with DHT11) and Vibration data (with SW420). Slave 2 sends electric current data from the sensor. All slaves send data to Master via RS485 module. So before that, I tried to test the module first whether it can work or not.

Actually I've tried coding using the Modbus RTU library to send temperature data from the DHT sensor. But I can't send it even though I have followed the reference from the following website: .: Tutorial Modbus RS485 Arduino : Nurse Call

I tried the code on the website exactly the same, only adjusting the data sent by the sensor. But I can't and I think it's because of rs485, that's why I tried simple data first.

You mention only one RS485 module but to send data between 2 Arduinos you need one at each end of the comms link. Is that what you have ?

Sorry I mean every microcontroller has its own rs485 connected on pins A B.

It worked, I tried using arduino nano on slave and master. I don't know what caused it, but now I can send string data via rs485.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.