Hello Friends, in this blog I will share with you how to control Arduino Digital Output with Serial Monitor.
I am using One Arduino Uno, One 330 ohm resistor, One LED, 2 connecting cable, and one small Bread board.
LED is connected to pin number 12, with the help of resistor 330 ohm.
Below is the sketch:
// Code by Deepak Sharma
// The code is to control one digital output by serial monitor
const int LED = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
input.trim();
if (input.equalsIgnoreCase("ON")) {
digitalWrite(LED, HIGH);
Serial.println("LED ON");
}
else if (input.equalsIgnoreCase("OFF")) {
digitalWrite(LED, LOW);
Serial.println("LED OFF");
}
else {
Serial.println("Invalid Value");
}
}
}
Testing:
Open the Serial monitor,
Check the baud rate, it should be 9600.
Enter "ON" in serial monitor input, LED should be ON.
Enter "OFF" in serial monitor input, LED should be OFF.
Comments
Post a Comment