Control LED Using Serial Monitor

43,985

10

12

Introduction: Control LED Using Serial Monitor

Hello everyone welcome to my first instructable.

In this instructables we are going to learn how to turn LEDs on and off using serial monitor.

Step 1: Gather the Parts

  • A breadboard
  • A LED
  • A 220 ohm resistor
  • An Arduino

Step 2: The Code

Upload the following code to your arduino:

void setup()

{

pinMode(13, OUTPUT);

Serial.begin(9600);

while (!Serial);

Serial.println("Input 1 to Turn LED on and 2 to off");

}

void loop() {

if (Serial.available())

{

int state = Serial.parseInt();

if (state == 1)

{

digitalWrite(13, HIGH);

Serial.println("Command completed LED turned ON");

}

if (state == 2)

{

digitalWrite(13, LOW);

Serial.println("Command completed LED turned OFF");

}

}

}

Step 3: Wiring

Hookup all the components according to the circuit diagram above.

Step 4: Done

Now just connect the arduino to your PC now open the serial monitor (To do so navigate through tools>Serial monitor).

Now when you send 1 the LED turns on and turns off when you send 2.

Thanks for viewing. Please write your questions or suggestions below.

Makerspace Contest

Participated in the
Makerspace Contest

1 Person Made This Project!

Recommendations

  • Pocket-Sized Speed Challenge

    Pocket-Sized Speed Challenge
  • Super-Size Speed Challenge

    Super-Size Speed Challenge
  • Audio Challenge 2020

    Audio Challenge 2020

12 Discussions

0
akash811998
akash811998

3 months ago

how does while(!serial) work here. i am not understanding

0
Anshu AR
Anshu AR

Reply 3 months ago

Probably not useful for UNO.
But for some devices like Leonardo, the sketch waits at "while(!Serial);" unless Serial communication has established.

0
abinaschopdar
abinaschopdar

1 year ago

I am working on the same kind of program in place of using number I am using strings the problem I am facing is my code reads the first Char then rest of the char.

0
saad-a
saad-a

1 year ago

Thank you very much that helped a lot.
but if you can explain what is the role of these two commands and why do we need to write them?

while (!Serial);
if (Serial.available())

0
ĐứcT23
ĐứcT23

1 year ago

excuse me, sir, why my Serial automagically returns to 0 while i was coding like you. thanks

0
Anshu AR
Anshu AR

Reply 1 year ago

Can you be more specific?
Does the serial prints "0"?
Can you include a screenshot?
I am assuming you need to set the baud rate on the serial monitor to 9600 as the sketch defines Serial communication at 9600 bauds.
(You can adjust the baud rate at the bottom right corner.)

0
ArjitS1
ArjitS1

Question 2 years ago

plz explain this code , I replaced code state==1 to state 'a' but it not work with alfa a key ? how to change num to alfa keys ????

0
Anshu AR
Anshu AR

Answer 2 years ago

//Here try this code:

void setup(){

pinMode(13, OUTPUT);

Serial.begin(9600);

while (!Serial);

Serial.println("Input A to Turn LED on and B to off");

}

void loop() {

if (Serial.available()){

char state = (char) Serial.read();

if (state == 'A'){

digitalWrite(13, HIGH);

Serial.println("Command completed LED turned ON");

}

if (state == 'B'){

digitalWrite(13, LOW);

Serial.println("Command completed LED turned OFF");

}

}

delay(100);

}

Capture.PNG
0
Anshu AR
Anshu AR

Reply 4 years ago

Thanks.

0
khurram125
khurram125

Reply 3 years ago

good job boy