HOWTO · Arduino
How to Print Multiple Variables in Arduino Serial
Display Arduino variable values in the Serial Monitor with Serial.print() and Serial.println().
On this page
To display several variable values in the Arduino Serial Monitor, initialize serial communication with Serial.begin() and send each value with Serial.print() or Serial.println(). Successive Serial.print() calls keep writing on the current line; Serial.println() ends its value with a line break. Add text between values to make the result easier to read. Set the Serial Monitor to the same baud rate used by Serial.begin().
The complete example below shows separated values on one line, floating-point precision, separate-line values, and a changing pair. Its small host shim lets the same source run as standard C++: on an Arduino, the board core supplies Serial and delay(); on a computer, the shim writes to standard output and runs two sample iterations. Set the Serial Monitor to the same baud rate as Serial.begin() when using a board.
Print multiple values on one line with Serial.print()
Serial.print() converts a value to text and writes it without adding a line break. Call it successively for each field, and print a separator between fields. The example uses a tab ("\t") between a temperature and humidity reading, so the output contains a tab character rather than the two visible characters backslash and t.
For floating-point values, the optional second argument specifies how many digits to show after the decimal point. Here, Serial.print(temperature, 2) requests two fractional digits. It changes the displayed formatting, not the value stored in the variable. A final Serial.println() finishes the row with a line ending. The optional precision argument applies to floating-point formatting; integer values are printed as whole numbers.
Print values on separate lines with Serial.println()
Use Serial.println(value) when each value should start on its own line. It writes the value and then a line ending, so successive calls make a vertical list. In the sketch, the label and its value stay together because each label uses Serial.print() and its corresponding value uses Serial.println().
Open the Serial Monitor at 9600 baud to match Serial.begin(9600). If the monitor uses a different rate, the received characters can look garbled even when the sketch is sending the intended values. Each Serial.println() writes its value followed by a line ending, so the next call starts a new row.
Combine both functions to format changing values
For repeated readings, use Serial.print() for the first value and a separator, then Serial.println() for the final value in the row. The loop below prints a changing pair, with a tab between the values and a line ending after each pair. It increments the humidity value before the next iteration; the second value is always five greater. On a board, the loop repeats and pauses briefly between rows. The host shim runs two iterations to make the changing output visible. The same-line and separate-line sections above refer to this single complete example, keeping the variable declarations and serial setup in one place.
When adapting the sketch, keep a consistent field order and delimiter if another program will parse the rows; changing either can affect that program. The host shim demonstrates formatting only; it does not emulate a board or Serial Monitor.
Run this example. The host shim prints to standard output; on a board, the Arduino core provides the same Serial calls and repeating loop().
#ifndef ARDUINO
#include <iostream>
class HostSerial {
public:
void begin(unsigned long) {}
void print(const char* value) { std::cout << value; }
void print(int value) { std::cout << value; }
void print(double value, int digits = 2) {
std::cout.setf(std::ios::fixed);
std::cout.precision(digits);
std::cout << value;
std::cout.unsetf(std::ios::fixed);
}
void println(const char* value) { print(value); std::cout << '\n'; }
void println(int value) { print(value); std::cout << '\n'; }
void println(double value, int digits = 2) { print(value, digits); std::cout << '\n'; }
};
HostSerial Serial;
void delay(unsigned long) {}
#endif
int humidity = 40;
void setup() {
Serial.begin(9600);
}
void loop() {
float temperature = 23.456;
Serial.print("Temperature: ");
Serial.print(temperature, 2);
Serial.print("\t");
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("Temperature on its own line: ");
Serial.println(temperature, 2);
Serial.print("Humidity on its own line: ");
Serial.println(humidity);
int first = humidity;
int second = humidity + 5;
Serial.print(first);
Serial.print("\t");
Serial.println(second);
humidity++;
delay(1000);
}
#ifndef ARDUINO
int main() {
setup();
loop();
loop();
}
#endif
Temperature: 23.46 Humidity: 40
Temperature on its own line: 23.46
Humidity on its own line: 40
40 45
Temperature: 23.46 Humidity: 41
Temperature on its own line: 23.46
Humidity on its own line: 41
41 46