// In this procedure, we will control a relay using
// commands from the Serial Monitor (1 = ON, 0 = OFF)
int relayPin = 8; // Relay signal pin connected to D8
char command; // Variable to store the Serial Monitor input
void setup() {
Serial.begin(9600); // Start Serial communication
pinMode(relayPin, OUTPUT); // Set relay pin as output
digitalWrite(relayPin, LOW); // Start with relay OFF
Serial.println("=== Relay Control System ===");
Serial.println("Type 1 to TURN ON the relay");
Serial.println("Type 0 to TURN OFF the relay");
}
void loop() {
// Check if data is available from Serial Monitor
if (Serial.available() > 0) {
command = Serial.read(); // Read one character from Serial input
if (command == '1') {
digitalWrite(relayPin, HIGH); // Turn relay ON
Serial.println("Relay is ON 🔛");
}
else if (command == '0') {
digitalWrite(relayPin, LOW); // Turn relay OFF
Serial.println("Relay is OFF 🔴");
}
else {
Serial.println("Invalid command! Type 1 or 0 only.");
}
}
}