Arduino Serial Read String or Line from Serial Monitor with ReadString Function

Arduino Serial Read String or Line – Serial.readString()

  • Arduino Serial.readString() Function reads the multiple bytes from the Serial Port received buffer into a String variable.
  • The readString function will read all the data received until the timeout.
  • The function return the String data type.
  • The readString() function will complete by timeout time, by default it is one second.

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

Arduino Serial Read String From Serial Port Monitor

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

Syntax of readString function

String s1 = Serial.readString(); //read all the data in s1 string variable.

The readString function return the String data type. The function terminate after timeout, which is one second by default.

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 ReadString Function Example

In this small project code we will send some data from serial monitor and return back the same data with Serial.println() function.

Example 1 – Serial.readString() function code to loop-back from PC (serial monitor).

/*
    Serial.readString() test program.
	Author: Nilesh Chaurasia
    https://elextutorial.com
*/
void setup() 
{
	Serial.begin(9600); // Set the baud rate to 9600
}
void loop() 
{
	String s1 = Serial.readString();// 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.

Note: if you send no character the null string will be shown in the serial monitor.

Arduino Serial Read String or Line from Serial Monitor with ReadString Function
Fig.1 – Arduino Serial Read String or Line from Serial Monitor.

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

/*
    Serial.readString() program to device control from serial 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.readString();// 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 example just send “on” and “off” from serial monitor.

Control LED on pin 13 from Serial Monitor command with Serial.ReadString Function
Fig.1 – Serial Port LED control with Serial Monitor and PC.
You can leave a response, or trackback from your own site.

One Response to “Arduino Serial Read String or Line from Serial Monitor with ReadString Function”

  1. Mao Novack says:

    I am not sure where you’re getting your info, but great topic. I needs to spend some time learning much more or understanding more. Thanks for great info I was looking for this info for my mission.

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