How to controll LED with Serial com. - Help needed

I want to control leds with the use of serial command.

Like when you write "turn on led 1" LED 1 turns on and when you write " turn off led 1" the LED 1 goes back off. How do I do that?

Whant to controll 3 LEDs by doing this.

LED 1 = pin 8 LED 2 = pin 9 LED 3 = pin 10

I just wrote this example code, I haven't tried it yet with my hardware but it's simple so you should be able to just copy, paste and go.

//Connect module inputs to Arduino pins D1, D2, D3,
//D4. Open serial console at 38400 baud, and send
//value "0" to reset all LEDs, or a specific number to
//activate that LED.

#define LED1 1
#define LED2 2
#define LED3 3
#define LED4 4

byte command = 0;

void setup()
  Serial.println("LED4 demonstration starting up");
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  Serial.println("Ready. Type 0 to reset all LEDs, 1 - 4 to activate a LED.");

void loop() {
  if (Serial.available()) {
    command =;
    if( command == '0' )
      Serial.println("Resetting all channels");
    if( command == '1' )
      digitalWrite(LED1, HIGH);
      Serial.println("Activating LED 1");
    if( command == '2' )
      digitalWrite(LED2, HIGH);
      Serial.println("Activating LED 2");
    if( command == '3' )
      digitalWrite(LED3, HIGH);
      Serial.println("Activating LED 3");
    if( command == '4' )
      digitalWrite(LED4, HIGH);
      Serial.println("Activating LED 4");

void resetAllChannels()
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);

Thanks for your help. Some new things to write down from this code. But what if I wanted to use a sentence and not numbers, such as "Led1on"

Kimlorentz: But what if I wanted to use a sentence and not numbers, such as "Led1on"

You could modify your Arduino code so it receives several characters and interprets what they mean.

But it would be a complete waste of computing resources. "Led1on" takes 6 characters but only conveys the same information as "1".

The code in this Demo shows (among other things) how to receive several characters. If I remember correctly it assumes you know how many characters will be sent. If you want to send a varying number of characters you might want to look at the code in Chapter 8 of this Demo.


You can label the LEDs and the commands however you want, just make sure if you change the name of one LED in one part of your code, you must change all the other labels in the program of that single LED to match it. Just keep it simple and short so it runs smoothly