Arduino Serial Read String Until with ReadStringUntil Function Example

Arduino Serial Read String Until Function – Serial.readStringUntil

  • Arduino Serial readStringUntil Function reads the serial port into the string variable, until it receive the terminating character.
  • By default the readStringUntil function wait for terminating character or timeout to complete.
  • The Serial.readStringUntil() function wait for timeout, which is one second by default.
  • If the terminating character is not received within timeout time, the readStringUntil function terminate because of timeout event also.
  • You can consider the terminating character as command end symbol, like in c language compiler look for “;” for end of statement.

Serial.readStringUntil() function is useful in copy all the received data in the String variable, for further analysis or testing the command received.

Arduino Serial readStringUntil Code Syntax

Ones the data is available in the string variable we can use functions for further analysis, like indexOf() function to search the particular word is exist or not.

Syntax of readStringUntil function

String s1 = Serial.readStringUntil(‘\n’); //read all the data in s1 string variable until receive Newline ( “\n” ).

The readString function return the String data type. The function terminate after timeout, or by receiving Newline.

Caution: The default wait time or timeout is one second, which can be modified with the help of function Serial.setTimeout().

We will use serial monitor tool available in the Arduino IDE to send the string to the Arduino board.

Arduino Serial Read String Until Example – ReadStringUntil

In the project program we will send some characters from serial monitor terminal and all are return back with Serial.println() function.

Example 1 – Serial.readStringUntil() function program to loop-back from PC.

/*
    Serial.readStringUntil() function test code.
	Author: Nilesh Chaurasia
    https://elextutorial.com
*/
void setup() 
{
	Serial.begin(9600); // Set the baud rate to 9600
	
}
void loop() 
{
	String s1 = Serial.readStringUntil('\n');// s1 is String type variable.
	Serial.print("Received Data => ");
	Serial.println(s1);//display same received Data back in serial monitor.
	delay(500);
}   
				 

In the example below we have send three characters “xyz” first and than we have sent 5 characters “12345”. Otherwise it will show nothing.

In this example you have to set line ending character as Newline in the serial monitor terminal application.

Note: if you not send any character then Arduino send null string back to PC.

Arduino Serial Read String Until from Serial Monitor with readStringUntil Function
Fig.1 – Serial Read String Until test from Serial Monitor.

Application of ReadStringUntil in Project

In this example project we will control the LED connected on pin 13, with “on” and “off” command from serial monitor.

Example 2 – Serial.readString() function to on the LED on pin 13 when you send “on” and turn off the LED when you send “off”.

/*
    Serial.readString() program to control the LED from command "on" / "off".
	Author: Nilesh Chaurasia
    https://elextutorial.com
*/
void setup() 
{
	Serial.begin(9600); // Set the baud rate to 9600
	pinMode(13,OUTPUT);
}
void loop() 
{
	String s1 = Serial.readStringUntil('\n');// s1 is String type variable.
	Serial.print("Received Data => ");
	Serial.println(s1);//display same received Data back in serial monitor.
	if(s1.indexOf("on")>=0)
	{
		digitalWrite(13,HIGH);
	}
	if(s1.indexOf("off")>=0){
		digitalWrite(13,LOW);
	}
	delay(100);
}   
				 

To test this project try to send “on” and “off” from serial monitor, after set the Newline as line ending character.

PC based LED control on pin number 13 with readStringUntil
Fig.1 – PC based LED control on pin number 13 with readStringUntil.
You can leave a response, or trackback from your own site.

6 Responses to “Arduino Serial Read String Until with ReadStringUntil Function Example”

  1. Hi there everybody, here every person is sharing these kinds of knowledge, therefore it’s pleasant to read this web site, and I used to go to see this web site every day.|

  2. I was very happy to search out this internet-site.I wanted to thanks to your time for this excellent learn!! I undoubtedly enjoying every little bit of it and I have you bookmarked to take a look at new stuff you blog post.

  3. bedding set says:

    Everything is very open with a very clear description of the challenges.
    It was definitely informative. Your site is
    extremely helpful. Many thanks for sharing!

  4. caliper says:

    I just likе the helpfսl info yօu provide to your articlеs.

    I wіll bookmark your blog ɑnd take a look at once more right here
    regularly. I am relatively sure I will be informeɗ a lot
    оf new stuff proper herе! Good luck for the following!

  5. Wow, this article is pleasant, my younger sister is analyzing these kinds of things, so I am going to tell her.

  6. Google says:

    Every once in a though we opt for blogs that we read. Listed beneath are the latest web-sites that we select.

Leave a Reply to the article



Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape