Arduino – How to Get a String from Serial with readString()

In this tutorial I will show you how to use the Arduino readString() function, in order to receive a String (text) from Serial communication.

>> Watch this video as an additional resource to this tutorial:

After watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!

Code example with Arduino readString() function

For this example, we will create a very simple application. Here we just want to receive some text (a String), and print it back with Serial. Then, once you know how to do that, you will be able to process the String in your code and do whatever you want.

Here is the code.


You are learning how to use Arduino to build your own projects?

Check out Arduino For Beginners and learn step by step.


void setup() {
  Serial.begin(9600);
  Serial.setTimeout(10);
}
void loop() {
  if (Serial.available() > 0) {
    String str = Serial.readString();
    str.trim();
    Serial.println(str);
  }
}

Code explained

Let’s break down the code line by line to understand what it does.

Setup – Init Serial and reading timeout

void setup() {
  Serial.begin(9600);

First things first, we start Serial communication in the void setup(). We need that in order to be able to receive any data from Serial.

Here I use 9600 for the baud rate, which is quite common, and pretty low. You could choose something like 115200 for better performance.

  Serial.setTimeout(10);
}

It’s quite important to understand why we need this line. The thing is, when you use the Arduino readString() function, it will block for a duration of time (a timeout) to make sure it has received everything.

The default timeout for reading with readString() is 1000 ms, or 1 second. Yes, one full second, which means that anytime you use readString(), it’s just like if you did a delay(1000), which is definitely not optimal.

Hence, we reduce the Serial reading timeout, to 10 milliseconds in this case. If we go lower we might have some issues with missed characters when we read from Serial.

I suggest you start with 10 ms, and then increase it if you see that you’re missing some characters in your code.

Loop – use readString()

That’s it for the setup, now let’s analyse the code that will run indefinitely on the Arduino loop() function.

void loop() {
  if (Serial.available() > 0) {

First we check if we have received any data from Serial. This line is quite classic and you’ll use that for anything you want to read from Serial (int, String, etc).

Serial.available() will tell how many bytes are ready to be read in the Serial buffer, so basically, if the number is potitive, we know we have some available data.

    String str = Serial.readString();

We enter the if, and here we use the Arduino readString() function. This will read everything that’s in the Serial buffer and return it as a String.

For example, if you write “abc”, then you’ll have “abc” in the str variable (quite obvious). If you write “34”, then you’ll have the String “34” in str. The data will be converted and read as an Arduino String object.

And if you remember, we had set a Serial timeout in the setup() function. In this example the timeout is 10 ms, so the readString() function will block for 10 ms, which will give it enough time to read and process all the received bytes.

If you ever notice that you’re missing some data, you can increase the Serial baud rate to make communication faster, and/or also increase the Serial timeout.

    str.trim();

If you want to get rid of additional whitespace characters after the String, you can use the trim() function on the String object directly.

For example, if you have the “New Line” option in the Serial Monitor on your Arduino IDE, then a newline character (“\n”) will be added everytime. So, if you send “abc”, what will be received is “abc\n”. By using trim(), you remove this extra character.

    Serial.println(str)
  }
}

And finally, to finish our code example, we print the String back, just to make sure that we have received it correctly.

Recap and best practices with Arduino readString()

You know how to use readString() in your Arduino code.

What you can do now is to use this String you received in an if structure. For example you can ask for the user name, and if the user name is on a white list, you power on a green LED.

You could also print the text on an LCD screen, write the String in EEPROM, etc etc.

One thing to pay attention to (again) is to make sure the Serial timeout is big enough so that you can receive all the characters. Increasing the Serial baud rate can help with this, and if it’s still not enough, you’ll have to increase the Serial timeout.

This is an important trade off, as you want to balance between having a program that works, and having a program that runs fast.

Did you find this tutorial useful?

Do you want to learn Arduino from scratch?

If yes, this course is for you:

Arduino For Beginners - Complete Course

>> Arduino Programming For Beginners <<