Newbie question: Using Serial Monitor to control

First off I would like to say hello as Im new to this forum and to the Arduino uC.

I did a search both on the forum and online to find out if and how to control the uC using the serial monitor and USB.

Basically i would like to be able to press a key on my keyboard to turn on an LED and another key to turn it off. I tried using the Serial.begin/ combination but no response from the arduino. I can see it receives the data since the RX flashes but it doesn't respond to the specific keys.

Any ideas or where I could find some more info

Hey matey, welcome to the forums. Good to see you did you research first, I hope others learn from your good example :wink:

Couple of things you should know when dealing with the Serial functionality.

1# When you type within the IDE, each character is sent one at a time (a Byte is actually a Char behind the scenes. A char has a range of 0-254). When that data is received by your program, make sure you account for the delays between chars…

For example: if you send “hello” to the Arduino and output that to an LCD you may recieve a bunch of gargbled characters. ie: H<E…LLO.

2# When you type a character in the Arduino IDE it is sent a Char (a Byte).

To help you out a bit. try and structure your commands.

if(Serial.available()>4){ // If there are five or more bytes in the arduino buffer

byte buff[4];
for(int i=0;i<4;i++) buff =;
case ‘R’:
//Turn on red LED
case ‘B’:
Feel free to post your code

here is my code feel free to modit as you please

int LED= 13; // LED connected to digital pin 13 int Data = 0; // sets Data as container for

void setup() { pinMode(LED, OUTPUT); Serial.begin(300);


void loop() // run over and over again {;

if (Data==0x4F) //if char in serial monitor is O LED is On { digitalWrite(LED,HIGH); delay(300); } else if (Data==0x53) //if char in serial monitor is S LED is Off { digitalWrite(LED, LOW); delay(300); } else //else LED blinks { digitalWrite(LED,LOW); delay(300); digitalWrite(LED, HIGH); delay(300); } }

If you look in the examples menu of the Arduino application (version 16) there is a program that does exactly what you want. That is turns on the LED when you type H and off when you type L.

i shall check it out thanks.

Hi PK.

Your sketch will probably only work intemittently.

1# Consider changing your chain of 'if' statements with a switch. you can use the "default" keyword when none of the cases are suitable.

2# You need to wait until you have sufficient data to do your comparison. Check Serial.available().

if (Data==0x4F)
if (Data==0x53)

There is no reason not to write:

if (Data=='O')
if (Data=='S')

It makes the code more readable and intuitive.

could you clarify what you mean by changing the "if" statements ? im not sure if i understand what you mean.

also i now have changed the 0x4F to 'O' thanks for that

I'd suggest a long hard read of the Arduino reference before you delve much further into the Arduino world:

For information on an IF statement, refer to

AHHHH it's alive it works. i found my error my baud rate was too high and i wan't able to SEE what was going on lol. thank you all for your help

TeamMCS i actually have the reference set as my favourites and also i have the Arduino Handbook by my side. But i wasn't aware of the SWITCH statement, thank you for pointing me in the right direction

Good stuff. "Getting started with Arduino" is a good book to readup on. I think there's even a beta PDF knocking around somewhere on the internet

could you clarify what you mean by changing the "if" statements ?

Instead of :

if (Data == 'S')
...else if (Data == 'O')

You can write:

switch (Data)
  case 'O':
    // stuff to do in case of Off

  case 'S':
     // stuff to do in case of Set

  case 'E':
     // some other action like End 

    // stuff to do if none of the above
    // (can be empty)


NB if you don't put the "break" in, your code will "fall through" to the next case. You actually may want to do this, if two commands are equivalent,e.g.

  case 'O':  // command On
  case 'S':  // command Set
    // code here

Ah sorry PK, I've not had much sleep! Roasting here in England.

As posted by AWOL, a switch is actually a IF "behind the scenes" but it makes things more readable if you have mutiple outcomes from a single expression.

When [or if] you get deeper into the world of programming, a switch is sometimes a sign of overcomplication. It's worth noting that as programs become more complex care should be taken over the design of the code and how it can be solved more elegantly. Dont worry to much about this now but I thought it should be poitned out ;)

[edit] Need Coffee

no worries Team. I have some programming background although i never fully embraced it. My background is telecommunications as i'm more comfortable with hardware but I realize fully that in these days programming is about 80% and actual hardware 20%. I usually program in either assembler and VHDL but I would classify myself as a beginner to intermediate. However i believe i found a great resources in this website.

Yeah? As a software engineer by trade I actually find the hardware aspect of the Arduino far more interesting. It’s great to see you’ve got some low-level experience. I’ve dabbled in ASM but I’ve always ended up reverting to HLLs instead - I think it’s the feeling of so little progress for so much effort. Understanding the technology is really valuable though.

Grumpy Mike said:
“If you look in the examples menu of the Arduino application (version 16) there is a program that does exactly what you want. That is turns on the LED when you type H and off when you type L.”

Sorry, I’m a newbie too, and when I look at:

I don’t see a version number (mentioned above) and I don’t see anything that mentions the serial monitor. Could you please give a specific webpage reference for the program that you mentioned above that turns the LED off/on? Thanks !!