Arduino Serial find Function Syntax And Example Code of Serial.find(), timeout

What is Arduino Serial find Function

  • Arduino Serial find Function reads the received buffer and test for specified string or word present or not.
  • This function returns a boolean value.
  • If specified string is present in the buffer than the function return true, otherwise return false.
  • By default the find function wait for one second before return false, if string is not found.
  • The function read one character at a time whereas the find able to test string or word.

To test for word with the help of is really complex, and find function is a easy solution for testing multiple character response from devices.

Syntax of find function

int x = Serial.find(“on”);

If “on” received within one second of time the function return true, otherwise return false.

The find function is mostly used with the if logic, to perform some task in case target string received.

How find Function Works

The function continuously read the received data from the buffer and if target string found than return true.

And if the target string is not received until one second than it will return false, it means the function will wait for one second to receive the target word or string.

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

Arduino Serial.find Example

In this small project code we will on the LED on pin number 13 when we receive the “on”. And turn off the LED if received anything else.

Because the “on” is a multiple characters word, the function not able to perform this task easily.

Example 1 – Serial.find() function code to control the LED from PC.

    Control the LED on pin 13 with PC from serial monitor.
    Author: Nilesh Chaurasia
void setup() 
	Serial.begin(9600); // Set the baud rate to 9600
void loop() 
	if(Serial.available()) // Check if received
		if(Serial.find("on"))// test the received buffer for "on"

In this program we have used Serial.available() function to check first something is received. The find function is used to directly test the “on” string received in the serial buffer or not.

If received “on” we have used digitalWrite() function to turn ON the LED.

Arduino Example for Serial find function Serial.find()
Fig.1 – Arduino Example for Serial find function to test “on”.

Arduino Serial timeout Function ( Serial.setTimeout() )

The default wait time of the Serial.find() function is one second, with the help of Serial.setTimeout() you can modify the timeout of serial functions.

Syntax of Serial.setTimeout() function

Serial.setTimeout(5000);// Set the timeout to 5 second.

In the set timeout function you have to specify the time in milli-seconds ( ms ). The default value is 1000 for timeout.

You can leave a response, or trackback from your own site.

3 Responses to “Arduino Serial find Function Syntax And Example Code of Serial.find(), timeout”

  1. Warren Achor says:

    I feel that is one of the so much important info for me. And i’m happy studying your article. However wanna remark on few normal things, The website style is ideal, the articles is truly great : D. Good job, cheers|

  2. Thankyou for this wonderful post, I am glad I discovered this site on yahoo.

  3. Anton Shores says:

    Good day! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a outstanding job!

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