ESP32 C3 Supermini Serial data scrambled

I’ve recently bought an ESP32 C3 SuperMini from AliExpress to read RS232 data from an AIS transponder (it’s a ship position device). I set it up and got scrambled data from the serial. I then tried to connect the ESP32 directly to the COM-port of my computer and it seems like I get scrambled data regardless of my setup.

I’ve made sure that the RS232 connection is grounded.
I use the GPIO pin 20 as RX and GPIO pin 21 as TX. This is defined as "UART"-pins by the seller.
I have made sure that I use the same settings on both devices (Baud rate, parity, data bits, stop bits)
I have tested other pins with no luck.
I have tested choosing different boards like "Nologo ESP32C3" and "XIAO_ESP32C3" with no luck. "ESP32C3 Dev Module" was recommended by the seller.

My code:

void setup() { 
  Serial.begin(115200); 
  Serial1.begin(38400, SERIAL_8N1, 20, 21); 
} 

void loop() { 
  if (Serial1.available() > 0) { 
    Serial.println("Serial data is available!"); 
    String str = Serial1.readString(); 
    str.trim(); 
    Serial.println(str); 
  } 
}

If I send “test”, I get “QSt?” from the ESP32.

I’m a newbie at microcontroller programming and it feels like I’m doing some stupid mistake. Any help is appreciated.

Maybe look at this: ESP32 UART - Serial Communication, Send and Receive Data (Arduino IDE) | Random Nerd Tutorials and see the example code under: Serial Communication with a GPS Module

Did you use an RS232 to TTL converter?

No, I did not....

Actually, the AIS device has two different ports, one RS232 and one RS422 for NMEA output. It's the RS422 port that i actually wanted to use, so I have a RS422 to TTL module. When testing with the module and the RS422 port i got scrambled data so I thought I would "simplify" the setup and test the RS232 port instead. As I could connect it directly to my computer, I just assumed that I could connect it directly to the ESP32 :person_facepalming:

But even with using the TTL-module I get data like this:
Expected output:
!AIVDO,1,1,,,B0000003wk?8mP=18D3Q3wv5sP06,0*02
Output from ESP32:
o_[�W�������{�������)��%_���w�]��_���������

This is the RS422 to TTL module I use: "YL-128 RS422"
https://vi.aliexpress.com/item/32727645991.html

EDIT: Sorry, I replied the wrong post, This is an answer to jim-p's post.

Returning to the basic test which you conducted using "open serial port monitor", which you described in post #1, can you explain what you connected to ESP32 C3 pins 20 and 21 and what device created port COM1 on your PC ?

XIAO_ESP32C3 has only 1 uart

You may have damaged your ESP32 by doing that.
I suggest you da a loopback test with the ESP32 to see if the RX/TX pins still work

It's the COM-port on my motherboard (GA-Z77M-D3H):


I connected NSIN to TX (PIN 21) AND NSOUT to RX (PIN 20) and GND to Ground.

Are you sure ?

a) He is using a SuperMini and b) the XIAO_ESP32C3 has at least 2 UARTS according to this: How to use Serial1 with XIAO ESP32C3 - XIAO - Seeed Studio Forum

thats the way i read it, right down the bottom of the doco

Did a test like this:

  delay(5000);
  Serial1.print("TEST");
  Serial.println(Serial1.readString());

Output:
11:14:43.872 -> TEST
11:14:49.847 -> TEST
11:14:55.852 -> TEST

So I guess it's still OK?

looks like you are correct , i will have to try it out

Looks OK but that does not mean it will work with RS232.
I would go back to using the RS422 converter board.

I've not seen this method of integrating a PC directly with an ESP32 device and I can't comment on the validity of the choice of pins. I can just make the general comments that (a) it is usual to use a USB/UART adapter for such an integration and (b) the ESP32 pins are 3.3 volts (with a small tolerance).

I tested to use the SoftwareSerial library (https://docs.arduino.cc/libraries/espsoftwareserial/) and I get the exact same results:

#include <SoftwareSerial.h>

EspSoftwareSerial::UART AISDeviceSerial;

void setup() {
  Serial.begin(38400);
  AISDeviceSerial.begin(38400, SWSERIAL_8N1, 20, 21, false);
}

void loop() {
  if (AISDeviceSerial.available() > 0) {
    Serial.println("Serial data is available!");
    String str = AISDeviceSerial.readString();
    str.trim();
    Serial.println(str);
  }
}

Output:
11:38:55.690 -> Serial data is available!
11:38:56.699 -> o_[�������{�������)��%_���w�]��_���������

Wait!
That RS422 board will not operate with a Vcc of 3.3V!

Search for a RS232 converter that will work at 3.3V

Try a different set of pins for UART1 (Serial1). 20 and 21 appear to be dedicated to UART0 (Serial) as far as I can see which you are already using for the serial console.
Try pins 3 and 4 for example.

That make a lot of sense!

Found this thread:

Turns out NMEA is 3,3V from the beginning. I tested to connect the NMEA negative output to the RX PIN 20 on the ESP32 and finally got this output:
12:00:45.388 -> Serial data is available!
12:00:46.403 -> !AIVDO,1,1,,,B0000003wk?8mP=18D3Q3wv5sP06,0*02

Thank you very much for pointing me in the right direction!!

But now I'm not sure if I still should get a 3,3V RS422 module? May I damage the ESP32 or the AIS device without it?

If it says RS232 and not 5V or 3V or TTL then it can be positive and negative voltages as high as 25V and that will certainly damage the ESP. I don't think NMEA specifies a voltage.

Can you post a datasheet or operators manual for your AIS device. Maybe we can see what it actually outputs.

Full manual:

Can't find any references to voltages for the interfaces at all in the manual.