Avoid the laser beam.....


Have you ever watched an action movie when the star actor tries to avoid the laser beams into a room, in order not to activate the alarm and get caught?

That's what we 'll try to build in this lesson. An alarm system made of a laser pointer, a photoresistor and a buzzer which will be activated at certain circumstances.

In a few words, the laser beam will be active all the time and it will be pointed to the photoresistor from a distant point. As long as nothing interrupts the invisible beam, the light that passes through the photoresistor gives high values measured in Arduino IDE as discussed earlier.
But when something blocks the beam (for example someone who stands in the middle), then the light that passes through the photoresistor becomes from HIGH to LOW and that activates the buzzer which is making an annoying sound like a siren.

The code to make the above circuit work is below:

int rcvpin=A0;
int buzzpin=14;
int limitval=300;
boolean alarmon=false;

void setup()




void loop()
int ldrval=analogRead(rcvpin);


if (ldrval<=limitval)

if (alarmon==true)







So, copy the code above, paste it in Arduino IDE and upload it to the NodeMCU.

Also, let's try to explain some details about the code:

First, we declare some variables to use. rcvpin is the variable for the LDR pin. buzzpin is the variable for the buzzerpin. limitval is the variable for the amount of ambient light of the room that the LDR is placed. It may need adjustment, (the Serial Monitor will help on that), as the ambient light is changing, depending on the environment at any time. We must have in mind that the alarm will be triggered if the LDR senses less light than the limit. alarmon is a boolean variable which means that it may have one of the two values at any time: true or false. the initial value is false because the alarm is off when the circuit powers on.

After the setup() section of input-output pins, there is the loop() section. There, we check if the LDR light is less or equal to the limit which means that something blocks the light from the laser pointer to the LDR, so the alarmon variable takes the value true. Then the buzzer is activated and the only way to stop it is by reseting the NodeMCU, pressing the RESET button on the corner.

So, play with the laser trap. Try to check how fast you can pass through the trap without being caught. You can split the circuit between the laser pointer on one breadboard and the speaker and the LDR on another breadboard. In this way, you can set the trap length even to many meters away!