LASER ki लक्ष्मण रेखा -ESP32 | LASER DIODE | BUZZER | LDR Full Code




LASER ki लक्ष्मण रेखा -ESP32 | LASER DIODE | BUZZER | LDR



Full Code with Connections:

// Define pin numbers
const int ldrPin = 34;     // LDR connected to GPIO 34 (Analog input)
const int buzzerPin = 12;  // Buzzer connected to GPIO 12 (D12)

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

  // Set buzzer pin as output
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  // Read the voltage across the LDR (assuming it's connected in a voltage divider configuration)
  int ldrValue = analogRead(ldrPin);

  // Print LDR value (for debugging)
  Serial.print("LDR value: ");
  Serial.println(ldrValue);

  // Define a threshold value to determine when to activate the buzzer
  int threshold = 4000; // Adjust this threshold based on your LDR and lighting conditions

  // Control the buzzer based on LDR value
  if (ldrValue < threshold) {
    // Light intensity is low (LDR value is high), turn off the buzzer
    digitalWrite(buzzerPin, LOW);
  } else {
    // Light intensity is high (LDR value is low), turn on the buzzer
    digitalWrite(buzzerPin, HIGH);
  }

  // Add a short delay to avoid rapid changes
  delay(50);
}

 

Comments

Popular Posts