/* ESP32 WiFi Scanning example */
// https://www.circuits-diy.com/relay-control-with-button-arduino-tutorial/
// https://randomnerdtutorials.com/guide-for-relay-module-with-arduino/
void setup() {
pinMode(12, INPUT); // Set digital pin 12 as an input for pushbutton
pinMode(14, OUTPUT); // Set digital pin 14 as an output for relay
Serial.begin(9600); // Open serial communication at 9600 baud
}
void loop() {
int buttonState = digitalRead(12); // Read digital pin 12 for pushbutton
if (buttonState == LOW) {
// pushbutton is pressed
digitalWrite(14, HIGH); // turn on the relay (pin 14)
Serial.println("Relay is ON");
delay(10); // add a short delay for stability
} else {
digitalWrite(14, LOW); // turn off the relay (pin 14)
Serial.println("Relay is OFF");
delay(10); // add a short delay for stability
}
}
// Connect the pushbutton and relay to the Arduino board.
// The pushbutton should be connected to a digital input pin,
// and the relay should be connected to a digital
// output pin.
// Also, you can connect the VCC of the relay to the 5V of Arduino,
// and the GND to the GND of the
// Arduino.
// In the “setup()” function, set the digital input pin for the pushbutton and digital
// output pin for the relay
// using the “pinMode()” function and open the serial communication with the computer
// for monitoring the state
// of the relay. For example, you could connect the pushbutton to digital pin 2 and
// the relay to digital pin 3.