3.Light-Controlled Buzzer Using an LDR and Arduino
Light-Controlled Buzzer Using an LDR and Arduino
Components Needed:
- Arduino board
- LDR (photoresistor)
- 10kΩ resistor
- Buzzer (active or passive)
- Breadboard and jumper wires
Circuit Connections:
LDR Circuit:
- Connect one leg of the LDR to 5V on the Arduino.
- Connect the other leg of the LDR to an analog input pin (e.g., A0) and a 10kΩ resistor.
- Connect the other end of the 10kΩ resistor to GND.
Buzzer Circuit:
- Connect the positive pin of the buzzer to a digital pin (e.g., 9).
- Connect the negative pin of the buzzer to GND.
Example Code:
C++ Code
int ldrPin = A0; // Analog pin for LDR
int buzzerPin = 9; // Digital pin for buzzer
int threshold = 500; // Threshold value for light level
void setup() {
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
Serial.begin(9600); // Start serial communication
}
void loop() {
int ldrValue = analogRead(ldrPin); // Read LDR value
Serial.println(ldrValue); // Print LDR value to Serial Monitor
if (ldrValue < threshold) {
digitalWrite(buzzerPin, HIGH); // Turn on buzzer when it is dark
} else {
digitalWrite(buzzerPin, LOW); // Turn off buzzer when it is bright
}
delay(500); // Wait for half a second
}
Explanation of Code:
- Threshold: Set the threshold value to determine when the buzzer should be activated. Adjust this value according to your ambient light level.
- analogRead(ldrPin) reads the current light level, and if the value is below the threshold, it activates the buzzer.
- digitalWrite(buzzerPin, HIGH) turns on the buzzer, while digitalWrite(buzzerPin, LOW) turns it off.
- The Serial Monitor can be used to view the light readings to help determine a suitable threshold value.
Applications:
- This project can be used as a light-activated alarm, for example, to detect when someone blocks a light source.
- It can also be used in projects where an alert is needed based on light levels, such as a warning when the lights go out.
Comments
Post a Comment