// Define the pins
const int ledPin = 32;
const int relayPin = 4;
// Variable to store the last time we updated the outputs
unsigned long previousMillis = 0;
// Interval at which to blink (1000 milliseconds = 1 second)
const long interval = 1000;
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// Initialize the relay pin as an output
pinMode(relayPin, OUTPUT);
// You can optionally start with both off
digitalWrite(ledPin, LOW);
digitalWrite(relayPin, LOW);
// Start serial communication for debugging (optional)
Serial.begin(115200);
Serial.println("ESP32 LED and Relay Blinker Started!");
}
void loop() {
// Get the current time
unsigned long currentMillis = millis();
// Check if the interval has passed
if (currentMillis - previousMillis >= interval) {
// Save the last time you blinked the outputs
previousMillis = currentMillis;
// Toggle the LED state
if (digitalRead(ledPin) == LOW) {
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
} else {
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
// Toggle the relay state
if (digitalRead(relayPin) == LOW) {
digitalWrite(relayPin, HIGH);
Serial.println("Relay ON");
} else {
digitalWrite(relayPin, LOW);
Serial.println("Relay OFF");
}
}
}