Sub topic 4
Printing Text and Values with `Serial.print()` and `Serial.println()`
Assume you can create, upload, and run an Arduino sketch. You should also know setup(), loop(), variables, and basic C/C++ statements. This chapter focuses on building readable messages from text and changing values.
Two functions, two line behaviors
Serial.print() sends data without ending the current line. Serial.println() sends data and then adds a line ending. The Serial Monitor displays the next output on a new line.
| Function | Result |
|---|---|
| `Serial.print(value)` | Writes `value` and stays on the same line |
| `Serial.println(value)` | Writes `value` and moves to the next line |
Call several functions in sequence when you want labels and values on one line:
const int sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
Serial.print("Sensor reading: ");
Serial.println(reading);
delay(500);
}The first print() writes the label. The println() writes the integer and ends the line. The Serial Monitor might show:
Sensor reading: 618
Sensor reading: 621The reading changes because analogRead() measures the voltage on the analog pin.
Printing different data types
The Serial object provides different versions of print() and println() for common data types.
- A quoted string prints text.
- An integer prints a whole number.
- A
floatprints a decimal value. - A
charprints one character. - A Boolean value prints
1for true or0for false.
int count = 7;
float temperature = 23.5;
char grade = 'A';
bool active = true;
void setup() {
Serial.begin(9600);
Serial.print("Count: ");
Serial.println(count);
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Grade: ");
Serial.println(grade);
Serial.print("Active: ");
Serial.println(active);
}
void loop() {
}A print() call does not automatically insert spaces. Add spaces inside the text, or print a separate space:
Serial.print("x=");
Serial.print(10);
Serial.print(" y=");
Serial.println(20);This produces:
x=10 y=20Build output that helps debugging
A useful debug message identifies both the value and the point in the sketch where it was printed.
const int LED_PIN = 13;
int cycles = 0;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
Serial.println("Sketch started");
}
void loop() {
digitalWrite(LED_PIN, HIGH);
Serial.print("Cycle ");
Serial.print(cycles);
Serial.println(": LED on");
delay(250);
digitalWrite(LED_PIN, LOW);
Serial.println("LED off");
cycles++;
delay(250);
}The counter shows whether loop() continues to run. A message such as Sketch started also confirms that the board reset and reached setup().
Common mistakes
- Calling
Serial.print()beforeSerial.begin()can produce no useful output. - Printing every variable with
println()can scatter one event across many lines. - Forgetting a space in a label makes output difficult to read.
- Printing a value without its label makes it hard to identify during debugging.
- A float may show fewer decimal places than expected. Number formatting is covered separately.
Exercise
Rewrite the following output code so it displays left=12 right=34 on one line: Serial.println("left="); Serial.println(12); Serial.println("right="); Serial.println(34);
Show answer
Use Serial.print("left="); Serial.print(12); Serial.print(" right="); Serial.println(34);