Arduino Tutorials:Turning on and off an LED from Serial Monitor

Published by sandesh on

The following is a simple arduino code, which is to control an LED using input of the serial monitor.If we enter 1 in the serial monitor, the LED will glow. If we enter 0 in the serial monitor, the LED doesn’t glow. Connect an LED to 5th analog pin and close the circuit by connecting the LED pin to ground.

Code:

void setup() {
Serial.begin(9600);
pinMode(5,OUTPUT);
}
void loop() {
if (Serial.available()) {
int val = Serial.parseInt();
if (val == 1) {
digitalWrite(5,HIGH);
}
else{
digitalWrite(5,LOW)  ;
  }
 }
}

Code explaination:

In this code, we set pin 5 as output and begin serial monitor. We take in input from serial monitor.If input is 1, we get output as high, and if we enter any other number, we get output as low. We can check this by connecting Led to pin 5. If we enter 1, Led will glow, else, Led won’t glow.

LED in OFF state:

If 0 is entered in serial monitor

LED in ON state:

If 1 is entered in serial monitor

Next Post:Reading analog data from temperature sensor (LM35)

Previous Post:Blink code in Arduino IDE


1 Comment

Arduino Tutorials:Blink code in Arduino IDE - projectsflix · January 3, 2021 at 6:09 pm

[…] Next Post:Turning on and off an LED from Serial Monitor […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.