RS485 read/write

wjquigs
Posts: 7
Joined: Fri Aug 11, 2023 9:41 pm

RS485 read/write

Postby wjquigs » Sun Oct 15, 2023 8:24 pm

I have two ESP32 development boards connected using RS485 MAX serial transceivers. Following other threads in this forum, I configured one as a sender and one as a receiver. They seem to be communicating but the data is gibberish. I'm sending ints starting at 65 which should show up as ASCII 'A', but the output on the receiver side is garbage. The RS485 transceivers are powered from VIN (5V) on the ESP32, and on the receiver side DE and RE are also connected to ground (on the sender side, they are connected to GPIO4). On both, RO connects to RX2 (pin 16) and DI connects to TX2 (pin 17).
The transceivers are connected A-A and B-B with a jumper. I removed the termination resistors on the transceivers since with the resistors in place, I wasn't seeing anything.
I've tried switching to transmitting chars, strings, etc but same results.

Here's my sender:

Code: Select all

#include <HardwareSerial.h>
HardwareSerial Serial485Max(2);
#define S2RX 16
#define S2TX 17
#define S2ENA 4
#define SerialDataBits 9600

void setup()
{
  Serial.begin(115200);
  Serial485Max.begin(SerialDataBits, SERIAL_8N1, S2RX, S2TX);            
  pinMode(S2ENA, OUTPUT);
  delay(100); 
  digitalWrite(S2ENA, HIGH); 
}

void loop()
{
    int i;
    for (i=0; i<1000; i++) {
        Serial485Max.print(i);
        Serial.print(i);
        Serial.print('.');
        delay(100);
    }
}
And here's my receiver:

Code: Select all

#include <HardwareSerial.h>
HardwareSerial Serial485Max(2);
#define S2RX 16
#define S2TX 17
#define S2ENA 4
#define SerialDataBits 9600

void setup()
{
  Serial.begin(115200);
  Serial485Max.begin(SerialDataBits, SERIAL_8N1, S2RX, -1, true);            

  delay(500);
  Serial.println("serial ready");
}

void loop()
{
  int sb;
  char oc;
  while (Serial485Max.available()) {
      oc = Serial485Max.read();
      Serial.print(oc);
      Serial.print('.');
    }
}

MicroController
Posts: 1696
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: RS485 read/write

Postby MicroController » Mon Oct 16, 2023 11:36 am

The RS485 transceivers are powered from VIN (5V)
Probably not a good idea. This may cause 5V to be sent from the MAX485 into the ESP's RX pins.
I removed the termination resistors on the transceivers
These 'termination' resistors are required. Without them, the signals may not be able to return to the idle state quickly enough.

Also, are sender and receiver connected to a common GND?

wjquigs
Posts: 7
Joined: Fri Aug 11, 2023 9:41 pm

Re: RS485 read/write

Postby wjquigs » Mon Oct 16, 2023 3:01 pm

I switched back to transceivers with termination resistors, changed power to the transceivers to 3.3V, connected the transceiver grounds to each other, and confirmed all my pins were correct. I also disconnected DI from GPIO 17 on the receiver side. Same result.

Who is online

Users browsing this forum: No registered users and 41 guests