Reads a line from serial until newline, finds a comma, and splits the string into two parts.
Execution Table
Step
Action
Serial Buffer
Variable Values
Output/Result
1
Check Serial.available()
Data: '23,45\n'
None
True, data available
2
Read input with readStringUntil('\n')
Data: '23,45\n'
input = '23,45'
String '23,45' stored
3
Find comma index with indexOf(',')
input = '23,45'
commaIndex = 2
Comma found at position 2
4
Extract firstPart substring(0,2)
input = '23,45'
firstPart = '23'
First part is '23'
5
Extract secondPart substring(3)
input = '23,45'
secondPart = '45'
Second part is '45'
6
End of loop iteration
Buffer cleared or new data waits
Variables hold parsed parts
Ready for next input
💡 Loop repeats waiting for new serial data
Variable Tracker
Variable
Start
After Step 2
After Step 3
After Step 4
After Step 5
Final
input
""
'23,45'
'23,45'
'23,45'
'23,45'
'23,45'
commaIndex
N/A
N/A
2
2
2
2
firstPart
""
""
""
'23'
'23'
'23'
secondPart
""
""
""
""
'45'
'45'
Key Moments - 3 Insights
Why do we use readStringUntil('\n') instead of just readString()?
readStringUntil('\n') reads characters until it finds a newline, ensuring we get one complete line of input. This matches the execution_table step 2 where input ends at newline.
What happens if the comma is not found in the input string?
indexOf(',') returns -1 if comma is missing. Then substring calls may behave unexpectedly or cause errors. The execution_table step 3 shows commaIndex = 2, but if -1, parsing needs extra checks.
Why do we add 1 to commaIndex when extracting secondPart?
Because substring(start) includes the character at start index. Adding 1 skips the comma itself, so secondPart starts right after the comma, as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the value of commaIndex?
A0
B-1
C2
D5
💡 Hint
Check the 'Variable Values' column at step 3 in execution_table.
At which step does the program extract the first part of the input string?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for substring extraction in execution_table rows.
If the input string was '100;200\n' instead of '23,45\n', what would commaIndex be?
A2
B-1
C3
D0
💡 Hint
indexOf(',') returns -1 if comma is not found, see key_moments explanation.
Concept Snapshot
String parsing from serial input in Arduino:
- Use Serial.available() to check data
- Use Serial.readStringUntil('\n') to read a line
- Use indexOf() to find delimiter position
- Use substring() to split string parts
- Always check if delimiter exists before parsing
Full Transcript
This example shows how Arduino reads a line of text from the serial port, waits until a newline character is received, then stores the input in a string variable. It finds the position of a comma in the string to split it into two parts. The first part is the substring before the comma, and the second part is after the comma. Variables update step-by-step as the program runs. Beginners often wonder why readStringUntil is used and how substring indexes work. This trace clarifies those points by showing each step and variable value clearly.
Practice
(1/5)
1. What does Serial.readStringUntil('\n') do in Arduino programming?
easy
A. Reads characters from serial input until a newline character is found
B. Sends a newline character over serial
C. Clears the serial buffer
D. Reads only one character from serial input
Solution
Step 1: Understand the function purpose
Serial.readStringUntil('\n') reads characters from the serial buffer until it finds the newline character '\n'.
Step 2: Compare with other options
It does not send data, clear buffer, or read only one character; it reads a full line until newline.
Final Answer:
Reads characters from serial input until a newline character is found -> Option A
Quick Check:
Read until '\n' means read full line [OK]
Hint: Remember '\n' means newline, so it reads until line ends [OK]
Common Mistakes:
Thinking it reads only one character
Confusing reading with sending data
Assuming it clears the buffer
2. Which of the following is the correct way to read a full line from serial input in Arduino?
easy
A. Serial.read()
B. Serial.available()
C. Serial.readStringUntil('\n')
D. Serial.write()
Solution
Step 1: Identify function to read full line
Serial.readStringUntil('\n') reads characters until newline, capturing a full line.
Step 2: Understand other functions
Serial.read() reads one byte, Serial.write() sends data, and Serial.available() checks bytes available.
Final Answer:
Serial.readStringUntil('\n') -> Option C
Quick Check:
Read full line = readStringUntil('\n') [OK]
Hint: Use readStringUntil('\n') to get whole line input [OK]
Common Mistakes:
Using Serial.read() to get full line
Confusing read and write functions
Using Serial.available() to read data
3. What will be the output of this Arduino code if the serial input is "TEMP:25\n"?
String input = Serial.readStringUntil('\n');
int value = input.substring(5).toInt();
Serial.println(value);
medium
A. 25
B. TEMP:25
C. 0
D. Error
Solution
Step 1: Read the input string
The input string is "TEMP:25" (newline removed by readStringUntil).
Step 2: Extract substring and convert to integer
input.substring(5) takes characters from index 5 onward, which is "25". Then toInt() converts "25" to integer 25.
Final Answer:
25 -> Option A
Quick Check:
Substring from 5 = "25", toInt() = 25 [OK]
Hint: Use substring index to isolate number, then toInt() converts [OK]
Common Mistakes:
Forgetting substring index starts at 0
Expecting full string printed
Not converting substring to int
4. Identify the error in this Arduino code snippet for parsing serial input:
String data = Serial.readStringUntil('\n');
int num = data.toInt();
if(num = 10) {
Serial.println("Number is 10");
}
medium
A. toInt() cannot convert string to int
B. Missing semicolon after Serial.println
C. Serial.readStringUntil('\n') does not read input
D. Using assignment '=' instead of comparison '==' in if condition
Solution
Step 1: Check if condition syntax
The condition if(num = 10) uses assignment '=' instead of comparison '=='. This causes a bug.
Step 2: Verify other parts
Semicolon is present, toInt() works correctly, and readStringUntil('\n') reads input properly.
Final Answer:
Using assignment '=' instead of comparison '==' in if condition -> Option D
Quick Check:
Use '==' to compare values in if [OK]
Hint: Use '==' for comparison, not '=' [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Assuming toInt() fails on valid numbers
Thinking readStringUntil doesn't read input
5. You receive serial input in the format "CMD:VALUE\n", for example "LED:1\n". How can you parse the command and value separately in Arduino?
hard
A. Use String input = Serial.readStringUntil('\n'); int val = input.substring(0,3).toInt(); String cmd = input.substring(4);
B. Use String input = Serial.readStringUntil('\n'); String cmd = input.substring(0,3); int val = input.substring(4).toInt();
C. Use String input = Serial.readString(); String cmd = input.split(':')[0]; int val = input.split(':')[1].toInt();
D. Use String input = Serial.read(); String cmd = input.substring(0,3); int val = input.substring(4).toInt();
Solution
Step 1: Read full line input
Serial.readStringUntil('\n') reads the entire line including command and value.
Step 2: Extract command and value
input.substring(0,3) extracts the first 3 characters as command (e.g., "LED"), and input.substring(4).toInt() converts the value part after ':' to integer.
Final Answer:
Use String input = Serial.readStringUntil('\n'); String cmd = input.substring(0,3); int val = input.substring(4).toInt(); -> Option B
Quick Check:
Substring command and value parsing works [OK]
Hint: Use substring with indexes to split command and value [OK]
Common Mistakes:
Using read() instead of readStringUntil
Trying to split string with split() which is not available