In this Arduino electronics tutorial, we show how Light Dependent Resistor(LDR) can be used as a light detector alarm with Arduino and a buzzer. LDR is a sensor whose resistance varies with amount of light falling onto it. So it can be used as a simple light detector. When ambient amount of light falls onto the LDR, it resistance decreases which can be detected using the Arduino ADC(Analog to Digital Converter). For this we use as the LDR as a resistor in a voltage divider configuration. The middle of the resistor voltage divider is connected to the ADC input of the Arduino. The voltage at this point varies with light amount falling onto the LDR and because of this when we sample the voltage divider voltage we get varying voltage. We can then write program such that when certain voltage threshold is exceeded we take action such as turning on alarm using a buzzer. In this example application of LDR we will make an light detection based alarm with LDR and Arduino.
Video demonstration of LDR as light detector alarm with Arduino and a buzzer is below.
Interfacing LDR(Light Dependent Resistor) and Arduino Nano
The following picture shows interfacing of LDR with Arduino Nano and piezo buzzer.
Circuit schematic diagram of LDR, Arduino, Buzzer is shown below.
Arduino Code for LDR and Buzzer Alarm
The following is Arduino code for making light detection based alarm using LDR(Light Dependent Resistor) and Piezo Buzzer.
//Program for ight detection based Alarm using LDR, Arduino, Buzzer
//Source: https://ee-diary.blogspot.com
const int ldrPin = A3;
const int buzzerPin = 7;
void setup(){
Serial.begin(9600); //set Serial output baud rate
pinMode(buzzerPin, OUTPUT);
//For output format
Serial.println("\nVoltage(V):");
Serial.print("--------------------------------------------------\n");
}
void loop() {
int ldrValue = analogRead(ldrPin);
float ldrVoltage = (ldrValue/1024.0)*5;
if(ldrValue < 700){
digitalWrite(buzzerPin, HIGH);
}
else{
digitalWrite(buzzerPin, LOW);
}
Serial.println(String(ldrVoltage)+"V");
delay(2000); //delay 2000ms to prevent any duplicates
}
In the code, the LDR pin is declared as A3 analog input pin and the buzzer pin is declared as pin 7 of Arduino nano. The buzzer pin is declared as output in the setup() function. The analog input A3 is not required to be declared as input because the analogRead() function used later down the code will make it as input automatically. In the main loop() function we read in the analog value from the A3 pin and convert it to voltage. The analog input voltage in the range of 0 to 5V voltage is converted to 0 to 1023. The voltage in the 0 to 1023 range is converted to voltage in the range 0 to 5V. This voltage is displayed as output on the serial monitor. Using the if else statement we check whether the input voltage in the digital level(0 to 1023) form is below 700. If it is low then turn on the buzzer. This happen when the light is hit on the LDR. When light hits the LDR, the resistance decreases and the digital level representation falls below 700. The corresponding voltage also decreases.
We can also use Photodiode to make such alarm. See the Photodiode Light Detector with Arduino where we measure the light intensity. The LDR can also similarly be used as light intensity measurement sensor. Also see IR sensor with Arduino, LCD, Buzzer and LED where IR sensor based alarm system is illustrated similar to this light detector alarm illustrated here.