ESP32 UART receiving data while not physically connected

I'm using ESP32 for UART comm with a device that may be physically disconnected. It's all good while it's connected, but when I take out the UART wires ESP32 continues to receive some random data.

Is this expected (maybe non-terminated pins have random noise on them?)
How can I detect that UART is properly connected?

HardwareSerial uart(1);

void setup() {
  uart.begin(57600, SERIAL_8N1, 2, 4); // remapping rx/tx to pins 2/4
}

void loop() {
  uint8_t buf[32];
  while (uart.available()) {
    uint8_t buf_size = uart.readBytes(buf, 32);
    some_logging_function_that_prints_buffer(buf, buf_size);
  }
}

read 32 bytes: fc3f00f200fc00fe00fc01fc00f81f00f800f0010078015c0078001000e00f00
read 32 bytes: 00f00200200000000008000000f80300000000f80000fc1f00f801f800fc00fe
read 32 bytes: 0f00fc00f400f800fe00fe00fc00fe00fc00fc00fc00fc00f0000062007c0300
read 32 bytes: 00fc03f60000ff3100fe0000f00800fc2d00ff1100f00100fe0000f800000000
read 32 bytes: 00fc1f00fc0000fc00fe04fc0a00f80000fc7f00fc3d000000001000f4020000
read 32 bytes: 00007400f807000000200008009000700c00f43c00fc00fc01fc00f43f009002
read 32 bytes: 0a00fe00fc00fc0100f83000f87e00f01d00fc0000f85f008c008000fc00fc01

Serial is itself an "open ended" protocol when it comes to connection status. Monitoring the state of that is the responsibility of the transport layer software that uses it.

In other words, valid input is defined by what you expect vs. what you receive at the input.

What is this?

Very definitely. What you experience is noise from an unterminated or floating pin. A simple pull down say 10K to ground will cause it to cease. This can happen at any baud, the band rate determines to some extent what it will decode it as.

You need a bit to lock on to then after a predetermined time it will look for another bit clocking whatever value it sees on the pin as data. Remember the noise although in analog format will be come 1 or zero depending on the signal level at the processor port. What is in between will be interpreted as 1's or zero's. I see it a lot because I am about a long drive with a #1 wood away from a communication tower. I have used UARTs as tone generators, with a bit of care they will work nicely.

Try it with maybe a 2' piece of wire then terminate it and see the difference.

Is it actually possible for random noise to produce data at 57600 baud in 8N1 format

So I called the below, random noisy data disappeared when un-terminated, and it works while connected as well. Is it the right thing to do? Are there any unexpected consequences? I'm quite new to this game :grimacing:

gpio_pullup_en(pinRx);
gpio_pullup_en(pinTx);

well simply put since the parity is 'None', all it needs is a start-bit, so basically the pin going from HIGH to LOW, after that the pin-state will be read to fill FIFO, and after elapsed time again the pin needs to go from the stop-bit HIGH (or '0') to LOW for the start-bit again.

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