Break function apparently not working in switch case - please help


I'm new to Arduino, just started couple days ago and trying to find my way through so thanks in advance for any help and sorry if this question was asked before, I googled a lot a couldn't find an answer.

So I'm following one video tutorial, where the guy is switching led on and off using Swith case.

the code looks like this:

"int led = 7;
char ch;

void setup()
pinMode(led, OUTPUT);

void loop()
if (Serial.available())
ch =;
switch (ch)
case 'a':
digitalWrite(led, HIGH);
Serial.println("LED is on");

case 'b':
digitalWrite(led, LOW);
Serial.println("LED is off");

digitalWrite(led, LOW);
Serial.println("Invalid input by user");
The idea is that when I type "a" the led should turn on, when I type "b" it should turn off, and when I type anything else it should stay off and display the message.

It works fine in video tutorial, but the problem I have is that when I type "a", the led turns on for a fraction of a second, then it turns off and displays the message "Invalid input by user" two times, like this:

"16:42:33.450 -> LED is on
16:42:33.450 -> Invalid input by user
16:42:33.483 -> Invalid input by user".

When I type anything else, it just displays the error message three times, like this:
"16:51:59.016 -> Invalid input by user
16:51:59.016 -> Invalid input by user
16:51:59.049 -> Invalid input by user".

With my very limited knowledge, it seems like the "break" is not doing its job, and the code keeps looping. Although I couldn't find any solution to fix this.

Please help!!!

Thank you very much in advance!!!

In the serial monitor, change to "No line ending".

What is line ending set to?

Set it to "none" and retest.

Please remember to use code tags when posting code

First, read. For example How to use the forum :wink:

Next, set line ending to none in Serial monitor :wink:

For info. Key 'a' will turn the led on. Specifically key 'b' will turn it off. But, any key will turn off the led when it's on using 'default'.

If you comment out default it should only use 'a' and 'b' for led control, ignoring other keys.