Learning C with the Freenove WSP32-WROVER starter kit and there's an issue I cannot find an answer to. Using the provided code to input some data with UART, it would seem to have a strange behavior whenever I input string that is larger than 12 characters. Under 12, everything is fine.

  Filename    : SerialRW
  Description : Use UART read and write data between ESP32 and PC.
  Auther      : www.freenove.com
  Modification: 2020/07/11
String inputString = "";      //a String to hold incoming data
bool stringComplete = false;  // whether the string is complete

void setup() {
  Serial.println(String("\nESP32 initialization completed!\n")
                + String("Please input some characters,\n")
                + String("select \"Newline\" below and click send button. \n"));

void loop() {
  if (Serial.available()) {         // judge whether data has been received
    char inChar = Serial.read();         // read one character
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
  if (stringComplete) {
    Serial.printf("inputString: %s \n", inputString);
    inputString = "";
    stringComplete = false;

The output of any larger than 12 characters string is sometimes a little bit different, but always seems to follow a similar pattern. For any 13 characters long string, I get :


The ouput changes if I add characters, for example 20 characters is this :


I assume the unprintable character is a new line.

The baud rate is set at the same value both in the code and in the serial monitor and I get the same results if I change the baud rate.

Any explanation regarding this behaviour and how to avoid it would be most welcomed.

1 Answer 1



Serial.printf("inputString: %s \n", inputString);


Serial.printf("inputString: %s \n", inputString.c_str());

%s expects a const char * which is not what inputString yields.

What you're seeing in the difference between strings larger and smaller than 12 characters is down to whether or not small string optimization is in use and how structures are passed in the ESP32's calling convention.

  • 1
    Thanks a lot, this did solve the issue <3 Jul 28 at 22:03
  • As a rule, it is better to use char array thhen string. While string might be smaller then your maximum size of array, its variable nature makes it very hard for memory allocation to guess a right size. While it is not a problem in a simple program like this when you get to larger projects where your functions are used within libraries, variable nature of string makes memory alocation within gaps in memory fragmentation too unreliable. (I have solved many crashes simply by changing strings to char arrays)Its well worth getting into a habit of char arrays rather then strings
    – Tomas
    Jul 28 at 23:27
  • I have tried to remove "in fragmentation" unfortunately 5 minute edit rule for comment is too low for dissgraphic person"
    – Tomas
    Jul 28 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.