While working with Arduino we must print multiple data on a serial monitor. Normally Serial.print function is used but today we will highlight another function named as sprintf and will look how this function helps in printing multiple variables in a single line on the serial monitor.
Using Serial.print()
Normally when we want to print a variable or an output result, we use Serial.print() or Serial.println() to print every result on the next line. Suppose if we have multiple variables then we must write numbers of lines for serial print depending upon the total variable count.
Here is the example which illustrate use of Serial.print():
In the above example we have initialized three variables’ integers a, b, and c. To print all three variables on a serial monitor we must separately write a code of serial print for each one. Here we used three variables which lead us to writing six lines of code just to show us on serial monitor. Now imagine writing a code for five or 10 variables and printing them over a serial monitor.
This is when the sprintf() function comes in, let’s discuss how to print all three variables in a single line of code.
Print Multiple Variables Using Arduino sprintf() Function
The sprintf() generally stands for “String print”. Unlike normal serial print it doesn’t directly show output of variables on serial monitor first it stores output inside a specified char variable buffer.
The sprintf() allows the user to send a formatted output to a char array where the result will be stored. To show the string where variables are formatted, the Serial.print() function is used.
How to Use Arduino sprintf()
The sprintf() comes handy when we have to print multiple variables in a single line, using the three lines of code we can write as many variables as we want. Here is the basic syntax of writing multiple variables using sprintf():
sprintf(buffer, "Sum of Number %d and Number %d is %d ", a, b, c);
Serial.println(buffer);
- First we need to initialize a character array where the output string will be stored.
- Then in the second step sprint() function will be used which will combine text and variables in one line.
- Finally in the last step Serial.print() function will display the formatted string on the serial monitor.
Example Code
Now we will print the above code using the sprintf() function.
Let’s examine each line of code in detail now.
First in code we initialize serial communication using the Serial.begin() and setting baud rate to 9600. Later the loop section initializes three variables a, b, and c. Sum of the first two variables a and b will be stored in the third variable c. Now come to the main part of the code.
The size of the character array needs to be as large as it can easily store output variables and text. Count the number of characters needed to store and make the buffer with the exact same size.
Next line in the code is the actual sprintf() function. It takes 2 arguments; the first argument will store the character inside the buffer array. Second argument is the string which we want to create. Here to display the variable in string we have used format specifiers.
Format specifier is the %sign followed by the letter known as format character. Character specifiers are the letter after % sign. Both these tell the sprint() what datatype will be used for the available data.
Some common character specifiers are:
Character | Data type |
---|---|
d or i | Signed decimal integer |
u | Unsigned decimal integer |
s | String of characters |
In the above example we have initialized three format specifiers using %d which means we want 3 variables to be stored in the output string as signed decimal integer. To get the values to these three variables, 3 arguments are added right after the string. For every format specifier we need to pass a separate value and each of them separated by comma.
Note: The sprintf() functions in Arduino cannot handle floating point values. So, if we need to print a decimal number something like 3.14 or 12.12, first we must convert that float value to a string, then print on the screen. A function dtostrf() is commonly used to do this.
Conclusion
While programming the Arduino board we need to look for all the techniques and functions available that can optimize our code. Having a minimalistic and efficient code helps to run Arduino fast. While printing data on the serial monitor, we use serial print function but here we have covered how sprint() function can help Arduino to print multiple variables over a single line.