Arduino Mega 2560 – Simple Alarm for Stealing Detection Using a Light Dependent Resistor (LDR)

In this tutorial, I will show you how to develop a simple alarm, in case someone steals something from you, using a Light Dependent Resistor (LDR), a laser sensor and an active buzzer.

Parts Required

  • Arduino Mega 2560;
  • USB 2.0 Cable Type A/B;
  • 1 Light Dependent Resistor (LDR);
  • 1 Laser Pointer Sensor;
  • 1 Active Buzzer;
  • 1 10kΩ resistor;
  • Male to male jumper wires;
  • 1 Breadboard.

Note: Since only one analogue pin is required for you to follow this tutorial, an ESP8266 NodeMCU board or any similar board can be used instead. Just be sure to change the pin numbers in the code if you use other boards. Also, there is no need for a laser, as long as you manage to have a device that is capable of applying a sufficiently intense beam of light to the LDR.

Light Dependent Resistor (LDR)

A Light Dependent Resistor (LDR), also known as a photoresistor, is a special device in which its resistance varies depending on the light falling on its sensitive surface (Figure 1).

Figure 1 – Light Dependent Resistor (LDR).

This device is composed of two visible metal contacts separated by a thin strip, exposing the semiconductor material which is inside the LDR to external light. This semiconductor material is characterized by having very few free electrons, meaning a very low conductivity and hence a high resistance. As the light reaches the semiconductor, the light photons are absorbed and some of their energy is transferred to the electrons [1]. This gives some of them sufficient energy to break free, and so facilitating electricity conduction between the metal plates. In short, as more light falls on the semiconductor, more electrons are released to conduct electricity and the overall resistance of the LDR falls further.

As you may have predicted by now, these devices are used when there is a need to sense the presence and absence of light in a specific location. The applications of these kind of resistors mainly include alarm clocks, street lights, light intensity meters and, as it is the case of this tutorial, burglar or stealing alarm circuits [2]. However, how can we measure the light intensity falling on the LDR’s surface? A very simple and practical way to do it is to connect it in series with a standard resistor (R1) and apply a DC voltage (Vin) to the circuit, as seen in Figure 2 [3]. In this case, Vin equals 5 Volts and R1 has a resistance os 10kΩ.

Figure 2 – Voltage divider circuit with an LDR.

This way, a different voltage will appear at the junction point (Vout) for different light intensities. The amount of voltage drop across the standard resistor is determined by the resistive value of the LDR. As mentioned previously, resistance decreases when the light intensity falling in the LDR’s surface increases and vice versa, meaning there is an inverse relationship between resistance and light intensity. Hence, Vout shall increase as light intensity increases. To demonstrate this effect, I have reproduced the circuit of Figure 2 in my board and measured the voltage drop between the terminals of the standard resistor with two different light intensities applied to the LDR: room light and focused laser light. The result is shown in Figure 3.

(a) (b)
Figure 3 – Value of Vout for (a) room light and (b) focused laser light.

As we can see, Vout is substantially higher when the light from the laser is falling directly in the LDR’s surface. In this situation, we can conclude that the resistance of the LDR is equal to 310Ω. In fact, the resistance of the LDR in a total absence of light can reach 10MΩ, which falls to about 100Ω when it is fully illuminated [3].

Circuit

Moving on to the circuit, presented in Figure 4, it is designed to detect whenever an object of your choosing is moved out of place. This way, the light from the laser, which was initially blocked by the object, reaches the LDR, activating the buzzer to warn everyone near that something is not right.

Figure 4 – Circuit’s schematic.

First, we have to recreate the voltage divider from Figure 2. To do so, after connecting the LDR and the 10kΩ resistor in series, connect the junction point to the analog pin A0 of the Arduino Mega, the other LDR terminal to 5 Volts and the remaining pin of the resistor to ground.

The laser sensor used in this tutorial is very simple and inexpensive, which makes it easy for you to get. If you intend to use a laser sensor, and since we want it to be on all the time, simply connect the positive pin to 5 Volts and the negative pin to ground. Then, make sure that the laser’s beam light is pointed to the LDR’s surface before placing your object in the middle. Just be careful when moving the laser while it is connected since you can hurt yourself and potentially others if its light is pointed directly to someone’s eyes.

Regarding the active buzzer, it is a very simple device that only requires a DC power source for a beep to be generated. If you want to know more about active and passive buzzers, their features and how to use them in your future projects, check our tutorial “ESP8266 NodeMCU – Make some noise with buzzers“. For this tutorial, connect the positive pin of the buzzer to the digital pin 2 of the Arduino and the negative pin to ground.

Code

Finally, it is coding time. From Figure 3, we have seen that when the laser is pointing directly to the LDR, the voltage at the terminals of the resistor is around 4.85V. For this reason, I have considered that when this voltage is greater than 4 Volts, the buzzer should be activated. In order for the analog pin to read this value properly, it necessary to convert it by dividing the signal voltage value we want (4Volts) by the total voltage value (5 Volts) and then multiply this result by the maximum analog value (1024). The result of this operation is approximately 820.

If, alternatively, you are using an ESP8266 NodeMCU board, you may need to change the pin numbers in the code below. In case you need help with that, you may found useful to check our tutorial “ESP8266 NodeMCU – Blinking a LED” to see the corresponding pin numbers for this board.

int ldr = A0;     //Analog channel 0 of Arduino. 
int buzzer = 2;   //Arduino buzzer interface pin
                                                                
void setup()                                                          
{
Serial.begin(9600);       //Begin Serial monitor of arduino ide
pinMode(ldr, INPUT);      //Arduino LDR pin as input 
pinMode(buzzer, OUTPUT);  //Arduino buzzer pin as output                                            
}

void loop()
{
//Read Intensity of light falling on the LDR and
//store the measured value in variable ldrValue
int ldrValue = analogRead(ldr);

delay(100);                      //Time for ADC to stable 
Serial.println(ldrValue);        //Print the read value on arduino serial monitor

// If the voltage at 10k resitor is 4 volts or ldrValue is greter than 820,
// activate de buzzer 
// analog value = (signal voltage value/total voltage value) * maximum analog value
// (4/5)*1024 = 820

if(ldrValue>820){           
    digitalWrite(buzzer, HIGH);
    delay(500);
    digitalWrite(buzzer, LOW);
    delay(500);
}
else
{
    digitalWrite(buzzer, LOW); 
}
}

Video

If you want to see the final result of this project, check what happened when my friend tried to take away my remote controller from its original place. Spoiler alert, he was busted! If you enjoyed this tutorial, you can visit our YouTube channel and watch this and many other videos. Thanks for following us and be sure to rate, comment and share our content. Have fun!!

References

[1] notes.com/articles/electronic_components/resistors/light-dependent-resistor-ldr.php

[2] https://www.electronics-https://www.elprocus.com/ldr-light-dependent-resistor-circuit-and-working/

[3] https://www.electronics-tutorials.ws/io/io_4.html

Leave a Reply

Your email address will not be published. Required fields are marked *