Bird
Raised Fist0
Arduinoprogramming~10 mins

Serial.print() and Serial.println() in Arduino - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Serial.print() and Serial.println()
Start Program
Setup Serial
Call Serial.print()
Send data without newline
Call Serial.println()
Send data with newline
Repeat or End
The program starts, sets up serial communication, then sends data using Serial.print() without moving to a new line, and Serial.println() which sends data and moves to a new line.
Execution Sample
Arduino
void setup() {
  Serial.begin(9600);
  Serial.print("Hello ");
  Serial.println("World!");
}

void loop() {}
This code initializes serial communication and prints 'Hello ' then 'World!' with a newline after.
Execution Table
StepFunction CalledData SentCursor PositionOutput on Serial Monitor
1Serial.begin(9600)NoneStart
2Serial.print("Hello ")"Hello "After 'Hello 'Hello
3Serial.println("World!")"World!\r\n"Start of new lineHello World!
4End of setupNoneStart of new lineHello World! (cursor at new line)
💡 Program waits in loop() doing nothing; serial output complete with 'Hello World!' and cursor at new line.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Serial Buffer"""Hello ""Hello World!\r\n""Hello World!\r\n"
Key Moments - 2 Insights
Why does Serial.print() not move to a new line after printing?
Serial.print() sends data but keeps the cursor on the same line, as shown in step 2 of the execution_table where the cursor stays after 'Hello '.
What does Serial.println() do differently than Serial.print()?
Serial.println() sends data and then adds a newline character (carriage return and line feed), moving the cursor to the next line, as seen in step 3 where the cursor moves to the start of a new line.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cursor position after Serial.print("Hello ") is called?
AAfter the text 'Hello ' on the same line
BAt the end of the program
CAt the start of a new line
DBefore any text is printed
💡 Hint
Check the 'Cursor Position' column in row 2 of the execution_table.
At which step does the output move to a new line on the Serial Monitor?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output on Serial Monitor' and 'Cursor Position' columns in the execution_table.
If we replace Serial.println("World!") with Serial.print("World!"), what changes in the output?
AThe output will be 'Hello' and 'World!' on separate lines
BThe output will be 'Hello World!' on the same line without moving to a new line
CThe program will not print anything
DThe output will have an extra blank line
💡 Hint
Compare the behavior of Serial.print() and Serial.println() from the key_moments and execution_table.
Concept Snapshot
Serial.print(data) sends data to the serial monitor without moving to a new line.
Serial.println(data) sends data and moves the cursor to the next line.
Use Serial.begin(baud_rate) in setup() to start serial communication.
Serial.println() adds a newline character automatically (carriage return and line feed).
Serial.print() keeps printing on the same line until a newline is sent.
Full Transcript
This example shows how Arduino's Serial.print() and Serial.println() work. First, Serial.begin(9600) starts the serial communication. Then Serial.print("Hello ") sends 'Hello ' to the serial monitor but keeps the cursor on the same line. Next, Serial.println("World!") sends 'World!' and moves the cursor to a new line. The output on the serial monitor becomes 'Hello World!' with the cursor ready on the next line. Serial.print() does not add a newline, while Serial.println() does. This difference is important when formatting output on the serial monitor.

Practice

(1/5)
1. What is the main difference between Serial.print() and Serial.println() in Arduino?
easy
A. Serial.print() prints data without moving to a new line, Serial.println() prints data and moves to a new line.
B. Serial.print() prints data twice, Serial.println() prints data once.
C. Serial.print() only prints numbers, Serial.println() only prints text.
D. Serial.print() clears the screen before printing, Serial.println() does not.

Solution

  1. Step 1: Understand Serial.print() behavior

    Serial.print() sends data to the serial monitor but stays on the same line.
  2. Step 2: Understand Serial.println() behavior

    Serial.println() sends data and then moves the cursor to the next line, so the next output starts on a new line.
  3. Final Answer:

    Serial.print() prints data without moving to a new line, Serial.println() prints data and moves to a new line. -> Option A
  4. Quick Check:

    print() no newline, println() adds newline [OK]
Hint: Remember: println adds a new line, print does not [OK]
Common Mistakes:
  • Thinking print adds a new line
  • Confusing print with println behavior
  • Assuming print clears the screen
2. Which of the following is the correct syntax to print the number 42 followed by a new line using Arduino Serial?
easy
A. Serial.print(42);
B. Serial.println(42);
C. Serial.printline(42);
D. Serial.println42();

Solution

  1. Step 1: Identify correct function name

    The correct function to print with a new line is Serial.println().
  2. Step 2: Check syntax correctness

    Serial.println(42); uses correct syntax: Serial.println(42);. Options C and D have incorrect function names or syntax.
  3. Final Answer:

    Serial.println(42); -> Option B
  4. Quick Check:

    Correct function name and syntax = B [OK]
Hint: Use println() to print with a new line [OK]
Common Mistakes:
  • Misspelling println as printline
  • Missing parentheses
  • Using print instead of println for new line
3. What will be the output on the Serial Monitor after running this Arduino code?
void setup() {
  Serial.begin(9600);
  Serial.print("Hello");
  Serial.print("World");
  Serial.println("!");
}
void loop() {}
medium
A. HelloWorld!
B. Hello World !
C. Hello World !
D. Hello World!

Solution

  1. Step 1: Analyze Serial.print() calls

    Serial.print("Hello") prints "Hello" without new line, then Serial.print("World") prints "World" immediately after.
  2. Step 2: Analyze Serial.println() call

    Serial.println("!") prints "!" and then moves to a new line.
  3. Final Answer:

    HelloWorld! -> Option A
  4. Quick Check:

    print() no newline, println() adds newline [OK]
Hint: print() joins text, println() ends line [OK]
Common Mistakes:
  • Assuming print adds spaces or new lines
  • Confusing print and println effects
  • Expecting spaces between printed strings
4. Identify the error in this Arduino code snippet:
void setup() {
  Serial.begin(9600);
  Serial.print("Count: ");
  Serial.println(10)
}
void loop() {}
medium
A. Serial.println() cannot print numbers
B. Serial.begin() must be called in loop()
C. Serial.print() cannot print strings
D. Missing semicolon after Serial.println(10)

Solution

  1. Step 1: Check syntax line by line

    The line Serial.println(10) is missing a semicolon at the end.
  2. Step 2: Verify other statements

    Serial.begin(9600); is correctly placed in setup(), and print/println can print strings and numbers.
  3. Final Answer:

    Missing semicolon after Serial.println(10) -> Option D
  4. Quick Check:

    Semicolon missing = A [OK]
Hint: Check for missing semicolons after print statements [OK]
Common Mistakes:
  • Forgetting semicolons
  • Thinking Serial.begin() must be in loop()
  • Believing print can't handle numbers
5. You want to print the numbers 1 to 3 on the Serial Monitor, each on its own line, using a loop. Which code snippet correctly does this?
hard
A. for (int i = 1; i <= 3; i++) { Serial.print(i); }
B. for (int i = 1; i <= 3; i++) { Serial.print(i); Serial.print("\n"); }
C. for (int i = 1; i <= 3; i++) { Serial.println(i); }
D. for (int i = 1; i <= 3; i++) { Serial.println(i + "\n"); }

Solution

  1. Step 1: Understand printing numbers on separate lines

    Using Serial.println() prints the number and moves to the next line automatically.
  2. Step 2: Analyze each option

    for (int i = 1; i <= 3; i++) { Serial.println(i); } uses Serial.println(i); inside the loop, correctly printing each number on its own line. for (int i = 1; i <= 3; i++) { Serial.print(i); Serial.print("\n"); } tries to add a newline character manually, which may not work as expected. for (int i = 1; i <= 3; i++) { Serial.print(i); } prints numbers without new lines. for (int i = 1; i <= 3; i++) { Serial.println(i + "\n"); } tries to add a newline inside println, which is redundant and incorrect syntax.
  3. Final Answer:

    for (int i = 1; i <= 3; i++) { Serial.println(i); } -> Option C
  4. Quick Check:

    Use println() in loop for new lines [OK]
Hint: Use println() inside loop to print lines separately [OK]
Common Mistakes:
  • Using print() without newline in loops
  • Adding manual \n inside println()
  • Incorrect string concatenation with numbers