ESP32 sending serial data with RS485

Minimal programs:

//sender

#define RXD2 16 // does nothing on sending ESP32
#define TXD2 17

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, RXD2, TXD2);
  pinMode(RXD2, INPUT);
  pinMode(TXD2, OUTPUT);
}

void loop() {
  Serial2.write(1);
  delay(1000);
  Serial2.write(2);
  delay(1000);
  Serial2.write(3);
  delay(1000);
}

//receiver

#define RXD2 34
#define TXD2 16 // does nothing on receiving ESP32

char receivedInt;
int receivedIntA = 0;

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, RXD2, TXD2);
  pinMode(34, INPUT);
  pinMode(16, OUTPUT);  
}

void loop() {

  if (Serial2.available() > 0) {
    receivedIntA = Serial2.read();
    Serial.print("Received Byte: ");
    Serial.println(receivedIntA);
    
  }
}

I set the baud rate to 115200. Nothing prints. Should something be done to convert the incoming int to char or? Any ideas?

Connection diagram:

Serial.println(receivedIntA,HEX);`

Try print HEX value an see what print?

You have both RE pins tied low, that makes both devices receivers.

1 Like

Did you do a pre-test by uploading a testcode that waits for a button to be pressed and if button is pressed send something on the standard-IO-pins.?
I mean the IO-pins that are normally connected through the USB-to-TTL-converter

Did you do a loop-back-test where you connected the Tx and Rx-pin of one single ESP32 with each other?

did you do a loop-back-test by connecting the two RS485-chips with each other and by looping back through connecting Tx with Rx?

Did you do a pre-test if serial send / receive works through connecting the two ESP32's by directly connecting the Tx and Rx-pins?

Thanks. The tranceiving circuit is correct. I just didn't create it and thought it was the same. (It's got the RE pin HIGH or whatever it needs to be.)

I connected the TX pin (17) of an ESP32 with the RX pin (34) of another ESP32. (Also connected grounds together.) Nothing results.

I'll try the one ESP32 loop-back-test...

Why on the receiver you are using pin 34 if RX2 is pin 16.
and TX2 is pin 17?

See the image.
Pin 34 has no UART.

On other projects, the default RX pin was changed to 34. For the larger circuit, either I don't have access to pin 16 or it's being used for something else. For texting purposes, I'm down to no other circuits, just ESP32 Dev modules.

I ran your two codes on 2 ESP2 (mine is DOIT ESP32 DEV KIT v1) and they didn't work.

So I modified the two codes as follows and they worked
Connect 1 sender TX2 (17) with receiver RX2 (16).

see the printout.

Sender

//sender

#define RXD2 16 // does nothing on sending ESP32
#define TXD2 17
//---------------------------------------------------
void setup() {
  Serial.begin(115200);
  Serial2.begin(9600);
  //pinMode(RXD2, INPUT);
  //pinMode(TXD2, OUTPUT);
  Serial.println("Start");
}
//---------------------------------------------------
void loop() {
  Serial2.write(1);
  delay(1000);
  Serial2.write(2);
  delay(1000);
  Serial2.write(3);
  delay(1000);
}

receiver

//receiver

#define RXD2 16
#define TXD2 17 // does nothing on receiving ESP32

char receivedInt;
int receivedIntA = 0;
//---------------------------------------------------
void setup() {
  Serial.begin(115200);
  Serial2.begin(9600);
//  pinMode(34, INPUT);
//  pinMode(16, OUTPUT);  
  Serial.print("Start: ");
}
//---------------------------------------------------
void loop() {
  if (Serial2.available() > 0) {
    receivedIntA = Serial2.read();
    Serial.print("Received Byte: ");
    Serial.println(receivedIntA,HEX);
  }
}

Printout

Received Byte: 1
Received Byte: 2
Received Byte: 3
Received Byte: 1
Received Byte: 2
Received Byte: 3
Received Byte: 1
Received Byte: 2
Received Byte: 3
Received Byte: 1
Received Byte: 2
Received Byte: 3
Received Byte: 1
Received Byte: 2
Received Byte: 3

I guess pin 34 can't be used as the RX then. Thanks.

I swear it worked for another project though...

OK, now I will test 34 pin.
PS:
Not work.
Maybe in another project you used softserial?

What model is your ESP32?

Are you referring to your diagram in post #1?

If so, then it is most definitely not correct. As @camsysca already pointed out, you have RE & DE tied low on both of your MAX485s. They will both be in permanent receive mode.

In addition, the output of your sending ESP32 should go to the DI pin of the MAX485 - NOT the RO pin.

Which line driver chip are you using? Your drawing shows MAX485 and a link to the MAX485 datasheet, but you also included a reference to the MAX488/490.

1 Like

The type is WROOM 32-D.

The transceiving circuit is correct in real life and not correct in the diagram.

So what does this mean?

You should always post precise information
what IO-pins of what device are connected to what other device and what IO-pins?

Such talking / writing of "this", "them" and "that" is a main source for trouble

It's a part of a much larger circuit. I made the mistake of having the diagram wrong. I didn't build the transmitting circuit.

whatever this exactly means. (Just unprecise inforrmation again)

If you do have posted precise information maybe some more help can be offered

In the meantime: If it does not work "this" way. Try "that" (maybe it does not work too)

for more example code for ESP32/RS485 have a look at esp32-readings-with-wind-direction-sensor-via-rs485

1 Like

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