MAX485 Problem

Hi !

I have the following configuration:
Atmega328pb with MAX485 module, on the other side a usb RS485 adapter (on pc)

When I send data via the arduino or via the PC I receive anything, example:

Arduino send '1'
PC receive: 00 67 00

PC sending 0x01
Arduino get 67

Except that the 67 does not correspond to anything (ASCII HEX DEC)

void setup() {
  delay(1000);
  delay(1000);
  delay(1000);
  
  // initialize serial:
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(10, OUTPUT);
}

void loop() {

    const int BUFFER_SIZE = 50;
    char buf[BUFFER_SIZE];

  while (Serial1.available() > 0) {

    // read the incoming bytes:
    int rlen = Serial1.readBytes(buf, BUFFER_SIZE);
    Serial.print( rlen );
    Serial.print( " --- " );
    // prints the received data
    for(int i = 0; i < rlen; i++)
      Serial.print(buf[i], HEX);
  }
  

  if(Serial.available() > 0) {
    
    byte ch = Serial.read();

    if (ch != '\n') {
      Serial.print( " --- char : " );
      Serial.println( ch, HEX );
  
      digitalWrite(10, HIGH); // Transmit
      delay(100);
      Serial1.write( ch );
      delay(100);
      digitalWrite(10, LOW); // Receive
    }
  }
 delay(100);
}

How have you connected the MAX485 to the 328?

You shouldn't need those delays in the 485 Tx section. Just switch the MAX485 into Tx mode and write the data. The MAX485 will go into Tx mode way before the UART starts transmitting. Once you've written your data, you can call Serial1.flush() which will block until the last byte has been clocked out. Then switch the MAX485 back into Rx mode.

I received same data (67 for 1, B3 for 2, ...)

This is my custom board schematic

Problem solved !

I see a picture on internet with data - = A, but my usb converter is a Data - for B

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