2
\$\begingroup\$

I'm learning how to use a MAX485 or MAX487 IC to create a half-duplex RS-85 communication between two Arduino s.

Each Arduino has an LED that turns ON/OFF by receiving a command from the other Arduino. When one of the Arduino's LED is ON, the other LED for the second one is supposed to be OFF.

The time for blinking LED s is 1 second (1 second ON and 1 second OFF).

Now, the problem is that one of the Arduino's LEDs (D2) is constantly ON while the second Arduino's LED (D1) is completely OFF and it seems that there is no communication between them. I think there is an issue with controlling the RE and DE pins for each of the MAX485 ICs.

I am new to the communication protocol over RS-485K. Kindly please help me. enter image description here

First Arduino Code:

#define EXE_INTERVAL 1000
#define EXE_INTERVAL_2 2000

int readData= 0;
int LEDpin= 13;
int TXon= 2;

unsigned long lastExecutedMillis = 0; // vairable to save the last executed time
unsigned long lastExecutedMillis_2 = 0; // vairable to save the last executed time for code block 2

void setup() {
  Serial.begin(9600);
  pinMode(LEDpin, OUTPUT);
  digitalWrite(LEDpin, HIGH);  
  pinMode(TXon, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis(); 
  digitalWrite(TXon, LOW);
  
  if (Serial.available() > 0) {
    readData= Serial.read();
    if(readData== 'L' ){
      digitalWrite(LEDpin, LOW);
    }
    if(readData== 'H' ){
      digitalWrite(LEDpin, HIGH);
    }
  }  

  if (currentMillis - lastExecutedMillis >= EXE_INTERVAL) {
    lastExecutedMillis = currentMillis; // save the last executed time
    Serial.write('H');
    digitalWrite(TXon, HIGH);
  }
  if (currentMillis - lastExecutedMillis_2 >= EXE_INTERVAL_2) {  
  lastExecutedMillis_2 = currentMillis; // save the last executed time
  Serial.write('L');
  digitalWrite(TXon, HIGH);
  }
}

Second Arduino Code:

int readData= 0;
int LEDpin= 13;
int TXon= 2;

void setup() {
  Serial.begin(9600);
  pinMode(LEDpin, OUTPUT);
  digitalWrite(LEDpin, LOW);  
  pinMode(TXon, OUTPUT);
}

void loop() {
  digitalWrite(TXon, LOW);
  if (Serial.available() > 0) {
    readData= Serial.read();

    if(readData== 'H' ){
      digitalWrite(LEDpin, HIGH);
      Serial.write('L');
    }
  digitalWrite(TXon, HIGH);
    if(readData== 'L'){
      digitalWrite(LEDpin, LOW);
      Serial.write('H');
    }
  digitalWrite(TXon, HIGH);
  }  
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

The first AVR keeps TXON always high so it can never receive anything.

It also means the second AVR can receive what the first AVR sends. But since always one transmitter is already enabled, it can try to transmit but there will just be two tranceivers driving the bus and none listening.

EDIT: After the latest code update, it's clear that the transmitter is not enabled before byte transmission starts, and it is not kept enabled for the duration of one byte being transmitted, so the bus never has a single valid byte on it.

\$\endgroup\$
7
  • \$\begingroup\$ Thank you, I changed the "digitalWrite(TXon, HIGH)" to "digitalWrite(TXon, LOW)" in the line number 20. But now D2 is constantly ON and D1 is constantly OFF. What is the problem ? \$\endgroup\$
    – a2640
    Jul 21, 2022 at 19:31
  • 1
    \$\begingroup\$ The transmitter is not enabled when you transmit the byte. \$\endgroup\$
    – Justme
    Jul 21, 2022 at 19:36
  • \$\begingroup\$ But I am trying to enable it with "digitalWrite(TXon, HIGH)" in the line 28 and 40. Then with "digitalWrite(TXon, LOW)" in the line number 20 disable it to receive data from the other Arduino. Can you please let me what is the problem in this between and how can I fix it? \$\endgroup\$
    – a2640
    Jul 21, 2022 at 19:44
  • \$\begingroup\$ Perhaps you should draw the signals with pen and paper, or use the Proteus oscilloscope to see what goes on in the system? \$\endgroup\$
    – Justme
    Jul 21, 2022 at 19:46
  • 3
    \$\begingroup\$ If a data byte takes a millisecond to send, enabling the trasmitter for a fraction of this time is not enough to send the data because it needs to be enabled for the whole duration of the data byte. I am no Arduino expert how it's libraries work, but if the UART libraries do not support RS-485 transceiver enabling/disabling for you, at least it should provide you delays you can use. \$\endgroup\$
    – Justme
    Jul 21, 2022 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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