Untitled

 avatar
unknown
plain_text
5 months ago
1.0 kB
4
Indexable
// Define the pin connected to the SW-520D sensor
#define SENSOR_PIN 4  // Use GPIO 4 or another available GPIO

// LED indicator pin (optional)
#define LED_PIN 2     // Built-in LED on most ESP32 boards

void setup() {
  // Initialize serial communication for debugging
  Serial.begin(115200);

  // Configure SENSOR_PIN as input
  pinMode(SENSOR_PIN, INPUT);

  // Configure LED_PIN as output
  pinMode(LED_PIN, OUTPUT);

  // Initial state of LED
  digitalWrite(LED_PIN, LOW);

  Serial.println("SW-520D and ESP32 setup complete!");
}

void loop() {
  // Read the sensor state
  int sensorState = digitalRead(SENSOR_PIN);

  // Print the sensor state to the Serial Monitor
  if (sensorState == HIGH) {
    Serial.println("No Vibration Detected");
    digitalWrite(LED_PIN, LOW);  // Turn off LED
  } else {
    Serial.println("Vibration Detected!");
    digitalWrite(LED_PIN, HIGH); // Turn on LED
  }

  // Add a small delay to avoid spamming the Serial Monitor
  delay(100);
}
Editor is loading...
Leave a Comment