const int switchPin = 12; // Pin where the switch is connected
const int relayPin = 25; // Pin where the relay is connected
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Set the switch pin as input with internal pull-up resistor
pinMode(switchPin, INPUT_PULLUP);
// Set the relay pin as output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is off
Serial.println("Relay control with switch setup complete");
}
void loop() {
// Read the state of the switch
int switchState = digitalRead(switchPin);
// Check if the switch is pressed
if (switchState == LOW) { // Assuming the switch is active-low
Serial.println("Switch pressed. Relay ON.");
digitalWrite(relayPin, HIGH); // Activate relay
} else {
Serial.println("Switch not pressed. Relay OFF.");
digitalWrite(relayPin, LOW); // Deactivate relay
}
// Small delay to debounce the switch
delay(50);
}