Arduino

Serial.setTimeout() and Serial.getTimeout() Arduino Functions

One of the essential components of Arduino is the Serial communication, which enables the exchange of data between the Arduino board and a computer. In this article, we will discuss two Arduino functions, Serial.setTimeout() and Serial.getTimeout(). We will explain the syntax, parameters, and return values of these functions and provide examples of their usage.

Serial.setTimeout() – Arduino

The Serial.setTimeout() function in Arduino is used to set the maximum time in milliseconds that the program will wait for serial data. By default, the set timeout is 1000 milliseconds. The Serial.setTimeout() function comes from the Stream class in Arduino, which provides a set of common ways to work with inputs/outputs.

The Serial.setTimeout() function is useful when working with slow or unreliable connections.

Syntax
The syntax for Serial.setTimeout() function is:

Serial.setTimeout(time)

Parameters

To use Serial.setTimeout(), you need to provide the serial port object as the first parameter, and the duration of the timeout in milliseconds as the second parameter. The duration can be expressed as a long data type.

The time-out period value is in milliseconds (ms). This timeout value is unsigned long that starts from 1 and goes up to 4294967295 milliseconds.

Returns

This function does not return any value.

Serial.getTimeout() – Arduino

The Serial.getTimeout() function returns the current time-out period for the Serial communication. It can be used to determine the current time-out period or to restore the default time-out period.

Syntax
The syntax for Serial.getTimeout() function is:

Serial.getTimeout(time);

Parameters

This function does not take any parameters.

Returns

This function returns the set timeout value which is by default 1000ms and can be changed using the Serial.getTimeout(). The data type of return value is unsigned long.

Example Code

In the following example, we get the current time-out period using the Serial.getTimeout() function. We then print the time-out period to the serial monitor. Next, we changed the time-out period to 5 sec and then printed it again on the serial monitor.

void setup() {
  Serial.begin(9600); // initialize serial communication at 9600 bits per second
  unsigned long defaultTimeout = Serial.getTimeout(); // get the default time-out period
  Serial.print("Default time-out period is: ");
  Serial.println(defaultTimeout); // print the default time-out period to the serial monitor
  Serial.setTimeout(5000); // set the time-out period to 5000 milliseconds (5 seconds)
  unsigned long timeOut = Serial.getTimeout(); // get the current time-out period
  Serial.print("Current time-out period is: ");
  Serial.println(timeOut); // print the current time-out period to the serial monitor
}

void loop() {
}

In the above code the setup() function initializes the serial communication at 9600 bits per second and gets the default timeout period using the Serial.getTimeout() function. The default timeout period is then displayed on the serial monitor.

Next, the Serial.setTimeout() function is used to set the new timeout period to 5 seconds (5000 milliseconds).

The loop() function then retrieves the current timeout period using the Serial.getTimeout() function and displays it on the serial monitor. A delay of 1 second is added before printing the next value to prevent the serial monitor from being flooded with data. This code can be used to verify that the timeout period is set correctly and that the Serial.getTimeout() function is working as expected.

Output
In the output we can see both the default timeout and new timeout is printed on the serial monitor.

Conclusion

The Serial.setTimeout() and Serial.getTimeout() functions are important utility functions in the Arduino language. Serial.setTimeout() sets the maximum time in milliseconds that the program will wait for serial data, while Serial.getTimeout() retrieves the current timeout period. These functions are useful for working with external devices that may not always provide data within a specific time frame.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.