#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change 0x27 to your LCD I2C address
const int ldrPin = A0; // LDR sensor pin
const int buzzerPin = 2; // Buzzer pin
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight(); // Turn on the LCD backlight
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
noTone(buzzerPin); // Ensure the buzzer is initially off
}
void loop() {
// Read the value from the LDR sensor
int ldrValue = analogRead(ldrPin);
// Map the LDR sensor value to a range suitable for the buzzer frequency (modify as needed)
int frequency = map(ldrValue, 0, 1023, 200, 2000);
// Display the LDR sensor value on the LCD
lcd.clear();
lcd.print("LDR Value:");
lcd.setCursor(0, 1);
lcd.print(ldrValue);
// Generate a tone on the buzzer with the mapped frequency
tone(buzzerPin, frequency, 100); // The second argument is the duration of the tone in milliseconds
delay(100); // Add a small delay for stability (adjust as needed)
}