# While (Serial.available() ==0) {} issue, Please HELP !

**URL:** <https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590>\
**Category:** Programming\
**Created:** [June 8, 2026, 1:18pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590 "2026-06-08T13:18:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![WooleyPig](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@WooleyPig](https://forum.arduino.cc/u/WooleyPig)\
**Post date:** [June 8, 2026, 1:19pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/1 "2026-06-08T13:19:00Z")

</div>

Hey all,

Really hope someone can help me out here.....

I Have not got my UNO program to hand until I return to work next week but I have a glitch I need help with if someone can ?

Program Purpose:

My program, uses PWM output to an external module which converts PWM 0 - 100% to 0 - 5v, it's controlling the speed of a Grundfos industrial centrifugal water pump. I have x5 INT variables which hold PWM values to represent flow rate set points which produces a 32 step pattern over a 6 hr period of varying flow rates and holds. This 6 hour sequence then repeats over and over while a logger records water pressure and flow rate at 10 sec intervals.

Apart from the 'main Loop()' I have 3 functions. One deals with the pump speed when it needs to increase from it's current flow rate, another which deals with pump speed when it needs to decrease from it's current flow rate and a third which deals with a situation where the flow rate needs to hold for a set amount of time.

Eg: START - Hold at zero for 10 mins - increase from zero to 1000 over 2 mins - Hold at 1000 for 20 mins - Increase from 1000 to 2000 over 2 mins - Hold at 2000 for 32 mins etc etc

In the main Loop(), I have a sequence of x32 'code blocks' which call either of these functions depending on the flow sequence required and passes the required PWM variable for the function to use when changing pump speed, all the functions use a basic loop which changes speed over a 2 min period but the Holding function uses different amounts of time to hold at certain flow rates.

The water being pumped is going through a line of brand new domestic water meters with the intention of getting them to a value of 1000 cubic meters on their registers which concludes the test sequence.

Every week of this rig running I need to completely stop the flow to take readings from the registers of the water meters on test and continue the sequence after. I created a forth function to handle this. I have a simple 'Pause switch' hooked up to one of the UNO's pins which is coninuously monitored while the program runs. Once this switch is clicked it enters a pause state where the current 'step' completes and the Pause function is then called. Once in the Pause function, the 'Completed step' is recorded to a variable and the pump is set to zero speed which stops the flow. I then print up a message to the Serial monitor asking the user to type the reason for the pause and hit enter.

while (Serial.Available() ==0) {} Holds this situation until a user typed input is there

The users typed input is assigned to a string variable and printed to the serial monitor. At this point the state of the 'Pause Switch' is monitored via another 'while loop' and nothing happens, I take my meter readings, once done, pause switch is turned off. Once this happens, the pause function reads the last completed 'step' flow rate and it restarts the pump to reach that speed over 2 mins before continuing with the sequence.

Hope the above makes basic sense and I really hope I have not gone on too long, sorry if I have ?

MY PROBLEM:

I send the Program to the UNO and it begins to run.....All works fine at this point and also when I activate the very first 'Pause' action, all perfect. The problem comes later when I try to activate the 'Pause' again.....It completes the current Step and brings the flow rate down to zero as expected along with the message to enter the reason for the pause in the serial monitor, but this time, instead of waiting at that endless While loop for the user input is shoots straight passed this as if the user has already typed and hit enter. ? My assumption is that 'Serial.Available() == 0' is no longer empty after the first 'Pause' action and user input but googling is not giving me a result of how to clear that buffer, Or, maybe it is something else.

Many Thanks for any help if anyone would be so kind.

Regards  
Paul

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 8, 2026, 1:21pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/2 "2026-06-08T13:21:47Z")

</div>

Post full code of your sketch.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [June 8, 2026, 1:26pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/3 "2026-06-08T13:26:53Z")

</div>

@WooleyPig  
Too many words rather than just a code....

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [June 8, 2026, 1:44pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/4 "2026-06-08T13:44:05Z")

</div>

you have to call Serial.read() otherwise the character stays available.

(but that is just a guess based upon my errors in the past)

---

<div class="post-metadata">

**Author:** ![qubits-us](https://avatars.discourse-cdn.com/v4/letter/q/5daacb/32.png) [@qubits-us](https://forum.arduino.cc/u/qubits-us)\
**Post date:** [June 8, 2026, 1:44pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/5 "2026-06-08T13:44:46Z")

</div>

> [@WooleyPig](#):
>
> how to clear that buffer

read it and throw it away, something like..

```arduino
while (Serial.available() >0) {Serial.read();}

```

could also print it and see what it is..

good luck.. ~q

---

<div class="post-metadata">

**Author:** ![MicroBahner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/microbahner/32/238923_2.png) [@MicroBahner](https://forum.arduino.cc/u/MicroBahner)\
**Post date:** [June 8, 2026, 1:48pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/6 "2026-06-08T13:48:40Z")

</div>

> [@WooleyPig](#):
>
> googling is not giving me a result of how to clear that buffer,

Simply read the characters in the buffer and ignore them until the buffer is empty. Presumably a CR or LF is still there from users previous input..

... ok, too late 😂

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [June 8, 2026, 1:54pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/7 "2026-06-08T13:54:34Z")

</div>

First, read the pinned post re 'How to get the most from the forum'.  
You are starting by trying to explain your non-working solution. To assist, we need to know the requirements.  
IMPORTANT: We can not see your screen or desk.  
Pictures of hand-drawn wires going from a lanelled pin to a device like a resistor are king.  
Obviously, post ALL your code in a code block. If there is any Serial output to look at, post it in its own code block as well.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 8, 2026, 2:58pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/8 "2026-06-08T14:58:04Z")

</div>

> [@WooleyPig](#):
>
> instead of waiting at that endless While loop for the user input is shoots straight passed this

Check your IDE for using NO LINE ENDING. [While (Serial.available()==0) { } KEEPS falling trough - #2 by PaulRB](https://forum.arduino.cc/t/while-serial-available-0-keeps-falling-trough/1203521/2)

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/2/9/b/29be4e2eee3a7bea5368f37bf09836db219f89c9.png)

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 8, 2026, 3:24pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/9 "2026-06-08T15:24:14Z")

</div>

> [@qubits-us](#):
>
> > [@WooleyPig](#):
> >
> > how to clear that buffer
> 
> read it and throw it away, something like..
> 
> ```arduino
> while (Serial.available() >0) {Serial.read();}
> 
> ```
> 
> could also print it and see what it is..
> 
> good luck.. ~q

We can upload the following sketch to see that Serial buffer gets empty one-by-one as `Seria.read()` is executed. ~~However `Line ending` character remains in buffer as `Serial.avaiable()` does not count it.~~ Edit:

```arduino
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while(Serial.available()>0)
  {
    Serial.read();
    Serial.println(Serial.available());
    delay(3000);
  }
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [June 8, 2026, 4:17pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/10 "2026-06-08T16:17:43Z")

</div>

> [@GolamMostafa](#):
>
> However `Line ending` character remains in buffer as `Serial.avaiable()` does not count it.

That surprises me. Characters are characters, they are not examined for purpose neither when they arrive and are buffered nor when they are removed for consideration.

a7

---

<div class="post-metadata">

**Author:** ![qubits-us](https://avatars.discourse-cdn.com/v4/letter/q/5daacb/32.png) [@qubits-us](https://forum.arduino.cc/u/qubits-us)\
**Post date:** [June 8, 2026, 5:03pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/11 "2026-06-08T17:03:39Z")

</div>

> [@GolamMostafa](#):
>
> However `Line ending` character remains in buffer as `Serial.avaiable()` does not count it.

well I seriously hope that is not true or it would be a very nasty bug..  
if a byte has decimal value 10 then Serial.available() will not count it??  
guess that throws multiple byte variables out the window..

strange days.. ~q

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 8, 2026, 5:15pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/12 "2026-06-08T17:15:55Z")

</div>

> [@qubits-us](#):
>
> well I seriously hope that is not true or it would be a very nasty bug..  
> if a byte has decimal value 10 then Serial.available() will not count it??  
> guess that throws multiple byte variables out the window..
> 
> strange days.. ~q

I did send `Hello` with Newline option. I am gving you the print out. You please evaluate. It could be that my observation is wrong!

```arduino
23:20:57.472 -> 0
23:21:00.494 -> 4
23:21:03.472 -> 3
23:21:06.463 -> 2
23:21:09.501 -> 1
23:21:12.488 -> 0

```

This is the sketch I uploaded:

```arduino
void setup() 
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available() > 0)
  {
    Serial.read();
    Serial.println(Serial.available());
    delay(3000);
  }
}

```

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [June 8, 2026, 5:26pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/13 "2026-06-08T17:26:45Z")

</div>

Print the decimal value of each character actually received at the same time you print the number available.

With all three possible line ending settings.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 8, 2026, 5:32pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/14 "2026-06-08T17:32:17Z")

</div>

This is another printout:

```arduino
23:30:57.429 -> H0
23:31:00.408 -> e4
23:31:03.435 -> l3
23:31:06.396 -> l2
23:31:09.417 -> o1
23:31:12.421 -> 
23:31:12.421 -> 0

```

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [June 8, 2026, 5:34pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/15 "2026-06-08T17:34:07Z")

</div>

I said "decimal value" for a reason...

And, please, for maximum clarity, actually include code and output. I can't see the sequence of actions in your newest code.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 8, 2026, 5:39pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/16 "2026-06-08T17:39:56Z")

</div>

> [@camsysca](#):
>
> I said "decimal value" for a reason...

I have the hex print out with input string 12345 and Newline

```arduino
23:35:56.998 -> 31 0
23:35:59.994 -> 32 4
23:36:02.982 -> 33 3
23:36:05.986 -> 34 2
23:36:08.983 -> 35 1
23:36:12.024 -> A 0

```

Decimal printout: Input string 12345 and Newline

```arduino
23:39:24.900 -> 49 0
23:39:27.886 -> 50 4
23:39:30.913 -> 51 3
23:39:33.897 -> 52 2
23:39:36.886 -> 53 1
23:39:39.914 -> 10 0

```

I have expected a print out like below: Why does print out show 0 first and not 5 as I have sent 5 + 1 = 6 characters

```arduino
49 5     
50 4     
51 3
52 2
53 1
10 0

```

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [June 8, 2026, 5:44pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/17 "2026-06-08T17:44:50Z")

</div>

So clearly, newline counts. All is well. You're printing how many are available after you've removed a character. The first zero is because you're removing the H, or 1, which is the only thing that's arrived at that point. Characters arrive one at a time, not as a block.

Also do the other two line ending options.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 8, 2026, 5:45pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/18 "2026-06-08T17:45:52Z")

</div>

Thank you. I have got it. I have corrected my post. Sorry to all for the inconvenience.

> [@camsysca](#):
>
> The first zero is because you're removing the H, or 1, which is the only thing that's arrived at that point.

After reading the first arrived character( H or 1), there is 3 sec delay during which the remaining 5 (ello Newline) characters have arrved. So, after next read, the count becomes 4 and then 3 and so on.

```arduino
01:04:38.850 -> 49 5
01:04:41.881 -> 50 4
01:04:44.869 -> 51 3
01:04:47.876 -> 52 2
01:04:50.883 -> 53 1
01:04:53.885 -> 10 0

```

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [June 8, 2026, 10:18pm UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/19 "2026-06-08T22:18:21Z")

</div>

FYI, had you simply applied the delay immediately after entering the while loop in order to allow the characters time to accumulate, you'd not have been fooled.

Yet again, I must emphasise. It is clear you've changed your code in order to produce this last printout, but it isn't obvious to anyone what it really looks like now. If the purpose of this forum is to educate, surely the code that produced that output should be presented with it, no?

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 9, 2026, 2:23am UTC](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590/20 "2026-06-09T02:23:25Z")

</div>

> [@camsysca](#):
>
> If the purpose of this forum is to educate, surely the code that produced that output should be presented with it, no?

```arduino
void setup() 
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available() > 0)
  {
     delay(3000); //to accomodate all the charcaters in buffer 
     Serial.read();
     Serial.println(Serial.available());
  }
}

```

**Input:** string: 12345 and Newline

```arduino
01:04:38.850 -> 49 5 //arrived character: 1 char remain in buffer: 5          
01:04:41.881 -> 50 4
01:04:44.869 -> 51 3
01:04:47.876 -> 52 2
01:04:50.883 -> 53 1
01:04:53.885 -> 10 0

```

[Next page](https://forum.arduino.cc/t/while-serial-available-0-issue-please-help/1447590.md?page=2)
