Lesson 74: Using Print Class in Arduino
Learn how the Print class powers print()/println() output and how to format readable debug data.
Progress indicator
Lesson 74 of 74
Learning Objectives
- Understand what the Arduino Print class is and why Serial uses it.
- Learn the exact difference between print() and println() in real output logs.
- Print int, float, bool, and text values with readable labels and separators.
- Format output using decimal precision and base formats (DEC, HEX, BIN).
- Avoid common beginner mistakes that make Serial Monitor output confusing.
Concept Explanation
What is the Print Class
The Print class is a core Arduino base class that provides print() and println() methods.
Think of Print as a shared output engine. Many Arduino objects use it: Serial, network clients, some display libraries, and file streams. This is why the printing style feels consistent across different outputs.
Print Class Purpose
It gives a common way to output text/data to Serial Monitor and other stream-like outputs.
- Debugging: quickly check variable values while your code runs.
- Monitoring: observe sensor values over time.
- Validation: confirm that conditions and loops are behaving correctly.
How Print Works
- You call
print()orprintln(). - Value is converted into text representation.
- Text is sent byte-by-byte to output stream.
- The serial port transmits bytes at the selected baud rate.
- Serial Monitor reads those bytes and renders readable characters.
print() vs println()
print()keeps cursor on same line.println()appends newline at end.- Use multiple
print()calls to build one structured line, then finish withprintln().
Label: value | Label: value | Label: valueSupported Data Types
- Integer types (int, long, byte)
- Floating point values (float, double)
- Text literals / String / char arrays
- Boolean and custom formatted expressions
- Numbers in different bases:
DEC,HEX,BIN
Formatting Output
You can combine multiple print calls and control decimal places for float: Serial.print(value, 2).
Serial.print(255, HEX)printsFF.Serial.print(10, BIN)prints1010.Serial.print(3.14159, 2)prints3.14.
When to Use Print
- Debugging variable values.
- Monitoring sensor and state changes.
- Creating readable runtime logs.
- Explaining logic flow while learning (for example: IF branch / ELSE branch).
Example Code
This sketch prints mixed data types using both print() and println().
const int LED_PIN = 2;
int counter = 0;
float voltage = 3.27;
bool ledState = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
ledState = !ledState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
Serial.print("Counter: ");
Serial.print(counter);
Serial.print(" | Voltage: ");
Serial.print(voltage, 2);
Serial.print(" V | LED: ");
Serial.println(ledState ? "ON" : "OFF");
counter++;
voltage = voltage + 0.01;
delay(500);
}Example Code Explanation
Serial.print("Counter: ")starts the line with a human-readable label.Serial.print(counter)prints the current integer counter value.Serial.print(" | Voltage: ")adds a separator so the line stays easy to scan.Serial.print(voltage, 2)prints voltage with exactly 2 decimal places.Serial.print(" V | LED: ")adds unit text and the next field label.Serial.println(ledState ? "ON" : "OFF")prints LED state and then moves to a new line.counter++andvoltage = voltage + 0.01update values, so each next line changes.
What Happens Inside
- Print class converts each value into text bytes (ASCII characters).
- Those bytes are placed in a serial transmit buffer.
- UART hardware sends buffer bytes at the selected baud speed (for example 115200).
- On your computer, Serial Monitor receives the bytes and displays text lines.
- If you print too frequently, the buffer can become a bottleneck and your loop appears slower.
Common Mistakes with Print
- Forgetting
Serial.begin()before printing. - Using only print() and missing line breaks, causing unreadable logs.
- Printing too much too fast and slowing loop behavior.
- Comparing string output manually instead of printing labels and values clearly.
- Using very low baud rate with very high print frequency.
Best Practices for Print
- Use clear labels and separators for each value.
- Use decimal precision only where needed.
- Throttle print frequency using delays or timed intervals.
- Use one structured line format so logs remain readable over long runs.
- Print only data you need during debugging, then reduce output for production behavior.
Try it now
Open the simulator workspace and observe how structured print output appears line by line.