Arduino Serial Read String Until with ReadStringUntil Function Example

Ad Banner Image

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
    http://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
    http://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.
Sponsor Banner
You can leave a response, or trackback from your own site.
Leave a Reply to the article



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

Valid CSS! Valid HTML5!

Powered by Wordpress