with the following sketch I should be able to read and store what is entered in the Serial monitor

void setup() {

void loop() {

  Serial.println("Enter Zone Number: ");
   int zone = Serial.parseInt();


if I enter "1" I would except this:

Enter Zone Number: 

but unfortunately I have this:

Enter Zone Number: 
Enter Zone Number: 

What am I missing?

2 Answers 2


.parseInt() reads incoming text up until either it times out or until it reads something that isn't a number.

You are sending a number, and most likely a line-ending. If that line-ending is a simple \n then that will trigger the "end of number" and will be discarded and the number returned. However, if you are sending \r\n (i.e., CRLF) then you effectively have two line endings there:


Parsed as:

1\r => return 1
\n => return 0

Relying on the (poorly written) Arduino stream parsing routines is not good. Not only are they blocking, but often they just don't work right.

Instead you should be reading the serial properly, taking account of line endings, and then converting the string you have read into a number using the likes of atoi().

Tutorial on reading serial:


Here is a really nice and simple solution (More robust also concerning line breaks): https://stackoverflow.com/a/20962137/2714285

int x;
String str;

void loop() {
    if (Serial.available() > 0) {
        x = Serial.parseInt();
        str = Serial.readStringUntil('\n');
  • it is a too heavy solution to get rid of one \n
    – Juraj
    Commented Feb 26, 2023 at 9:47
  • If your goal is speed I understand your point but I first tried the solution of the article of Majenko and it just didn't work on linux (the \n char was just not recognized). And it's, btw, much more heavy in terms of readability / code / docs to maintain if your work in a team Commented Feb 26, 2023 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.