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.

If 0 is entered:

If 1 is entered:

Categories: ArduinoTutorials

0 Comments

Leave a Reply