Problems with serial communication

hey guys :slight_smile:
I am currently working on an ESP32 project.
When I upload my code via the Arduino IDE, the serial communication with another microcontroller works as I want, but when I upload the same code via the PlatformIO extension in VSCode, it outputs random values instead of the value I set.
As I am new to PlatformIO, my configuration file is set to default except for monitor_speed, but this should have no effect as I understand it. Does anyone know what I am missing?

Can you show your platformio.ini and code you’re running on it?

Thank you for your quick reply!

This is my platformio.ini:
platform = espressif32
board = esp32dev
framework = arduino
lib_extra_dirs = ~/Documents/Arduino/libraries
monitor_speed = 4800

This is the code at which the message is sent via the serial interface:
void getInfoFromAT89()
commFlag = true;
Serial.println(“Sending request”);
Serial2.begin(4800, SERIAL_8N1, RXD2, TXD2);

What value does RXD2 and TXD2 have?

I have this board (NodeMCU ESP32 | Joy-IT) and use pins 16 (RX2) and 17 (TX2).

Okay and you do have an extra USB-serial connector connected to those serial pins, yeah? Your sketch seems to be using two Serials. The default Serial will be connected to the USB-UART converted on the board, while you seem to be using Serial2 to communicate with a AT89 chip.

You need to take care which serial port you are connecting to and at which baud rate they run. If you are on the serial port for the dev board (Serial), you need to set monitor_speed to the baud rate set in Serial.begin(), or 115200 (default value for Arduino-ESP32) and you should see all output printed by Serial.println() etc.

You can switch between the monitor ports by using monitor_port. If you have two USB-UART adapters in your setup (one on-board and one probing IO 17), you should be able to see the output for both of them correctly, given that monitor_port and monitor_speed match.

I don’t use an USB-serial connector. My RXD2 and TXD2 pins are connected directly to the TX and RX pins of the AT89-Board.

Yes, Serial is for the serial monitor and Serial2 is for communication with the AT89.

My serial monitor does exactly what it is supposed to do and it shows me things the way I want it to, I only set the baud rate of 4800 to see if it also affects Serial2.
The only problem I have is that when I flash the ESP with PlatformIO, Serial2 outputs random values instead of the ones I want.
When I do it with the ArduinoIDE everything works…

I can’t reproduce the issue.

I have the sketch

#include <Arduino.h>

#define RXD2 16 
#define TXD2 17

void setup()
  Serial.println("Sending request");
  Serial2.begin(4800, SERIAL_8N1, RXD2, TXD2);

void loop()

and platformio.ini

platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 4800 ; observe TXD2 output
monitor_port = COM5 ; port of USB-UART connector connected to TXD2
upload_port = COM40 ; port for ESP32 upload

and for testing I’ve hooked up a USB-UART adapter to the TXD2 pin (and common GND of course).


You can see how the PlatformIO serial monitor, which is instructed to connect to the serial port connecte to TXD2, is correctly printing 2 at 4800 baud. So the ESP32 could send it correctly and it was also correctly received.

You can also see that the “regular” Serial output of the dev board at 115200 is also working and showing sending request.

Can you verify my example test setup above?

If that is not working, there’s a fundamental problem. Maybe outdated platform. (CLIpio platform update espressif32)

Also, what version of the ESP32 core are you seeing in the Arduno IDE board manager? 1.0.6 or 2.0.0-alpha?

Thank you for the time and effort you invest in helping me!

That’s right, that’s exactly how I want it.
PlatformIO is up-to-date and I use the ESP32 core version 1.0.6

I’ll try your sketch later, if it runs I’ll have to have another look at my code.

First, your esp32 has 3UART port which u are using one(serial) to upload your code, and you want to use the other (serial2) to communicate… Great!
I recommend using you use two serial monitors on same PC’s to do this test.

First, your platformio ide which will have its “monitor_speed = baud1” and on your sketch do “Serial.begin(baud1);”… where baud1 can be 115200…

Secondly, another serial monitor which will have its baud rate set at baud2 and on your sketch do “Serial2.begin(baud2, SERIAL_8N1, RXD2, TXD2);” where baud1 can be 4800…

Be sure ur wiring are correct for serial 2, Then upload your code and test.

You can do this with same vscode by opening 2 different workspace, start one as serial2 terminal and the other as your working environment.
Good luck!