MAKE AN LDR LASER TRIPWIRE

By Russell Barnes. Posted

Learn how to use an LDR to detect a laser pointer beam

The Raspberry Pi can easily detect a digital input via its GPIO pins: any input that’s approximately below 1.8V is considered off, and anything above 1.8V is considered on.

An analogue input can have a range of voltages from 0V up to 3.3V, however, and the Raspberry Pi is unable to detect exactly what that voltage is.

If you enjoyed making this project, then you can find many more GPIO Zero guides in our GPIO Zero Essentials book!

One way of getting around this is by using a capacitor, and timing how long it takes to charge up above 1.8V.

By placing a capacitor in series with a light-dependent resistor (LDR), thecapacitor will charge at different speeds depending on whether it is light or dark. We can use this to create a laser tripwire!

You'll need

  • 1× solderless breadboard
  • 1× lightdependent resistor (LDR)
  • 1× 1μF capacitor
  • 1× laser pointer
  • 5× male-tofemale jumper wires
  • 5× female-tofemale jumper
  • wires (optional)
  • 1× drinking straw
  • 1× plastic box

STEP-01 Connect the LDR

An LDR (also known as a photocell) is a special type of electrical resistor whose resistance is very high when it’s dark, but reduced when light is shining on it.

With the Raspberry Pi turned off, place your LDR into the breadboard, then add the capacitor. It’s essential to get the correct polarity for the latter component: its longer (positive) leg should be in the same breadboard column as one leg of the LDR.

Now connect this column (with a leg of both components) to GPIO 4. Connect the other leg of the LDR to a 3V3 pin, and the other leg of the capacitor to a GND pin. Your circuit should now resemble the circuit diagram below.

 Your circuit diagram should look like this in the end

STEP-02 Test the LDR

On the Pi, open IDLE from the Main Menu: Menu > Programming > Python 3 (IDLE). Create a new file by clicking File > New File. Enter the code below:

from gpiozero import LightSensor

ldr = LightSensor(4)

while True:
  print(ldr.value)

Save the code. At the start, we import the LightSensor class from GPIO Zero. We then assign the variable ldr to the LDR input on the GPIO 4 pin.

Finally, we use a never-ending while True: loop to continually display the current value of the light sensed by the LDR, which ranges from 0 to 1.

Try running the code and then shining your laser pointer on it to vary the light level.

STEP-03 Enclose the LDR

Unless you’re working in a darkened room, you’ll probably notice little difference between the measured light level when the laser pointer is directed onto the LDR and when it’s not.

This can be fixed by reducing the amount of light that the LDR receives from other light sources in the room, which will be essential for our laser tripwire device to work effectively. We’ll achieve this by cutting off a short section – between 2cm and 5cm – of an opaque drinking straw, and inserting the head of the LDR into one end.

Now try the test code again and see how the measured light level changes when you shine the laser pointer into the other end of the straw. You should notice a larger difference in values.

STEP-04 Wire up the buzzer

To create an audible alarm for our laser tripwire, we’ll add a piezo buzzer to the circuit.

Again, the polarity has to be correct: connect the column of the buzzer’s longer leg to GPIO 17, and the shorter leg to a GND pin. Let’s test whether it is working. In IDLE, create a new file, enter the code below and save it:

from gpiozero import Buzzer

buzzer = Buzzer(17)
buzzer.beep()

At the top, we import the Buzzer class from GPIO Zero. Next, we assign the buzzer variable to the buzzer output on GPIO 17. Finally, we use buzzer.beep to make the buzzer turn on and off repeatedly at the default length of one second.

To stop it, close the Python shell window while it is off.

STEP-05 Test the tripwire

We’ll now put it all together so that laser pointer shines at the LDR through the straw, and whenever the beam is broken, the buzzer sounds the alarm.

In IDLE, create a new file, enter the final code below, and save it:

from gpiozero import LightSensor, Buzzer
from time import sleep

ldr = LightSensor(4)

buzzer = Buzzer(17)

while True:
  sleep(0.1)
  if ldr.value < 0.5:
    buzzer.beep(0.5, 0.5, 8)
    sleep(8)
  else:
    buzzer.off()

At the start, we import the Buzzer and LightSensor classes from GPIO Zero. We also import the sleep function from time; we’ll need this to slow the script down a little to give the capacitor time to charge.

As before, we assign variables for the buzzer and LDR to the respective devices on GPIO pins 4 and 17. We then use a while True: loop to continually check the light level on the LDR; if it falls below 0.5, we make the buzzer beep.

You can change this number to adjust the sensitivity; a higher value will make it more sensitive. Try running the code. If you break the laser beam, the buzzer should beep for eight seconds. You can adjust this by altering the buzzer.beep parameters and sleep time.

STEP-06 Package it up

Once everything is working well, you can enclose your Raspberry Pi and breadboard in a plastic box (such as an old ice cream tub), with the drinking straw poking through a hole in the side.

If you prefer, you can remove the breadboard and instead connect the circuit up directly by poking the legs of the components into female-to-female jumper wires, with the long capacitor leg and an LDR leg together in one wire end, connected to the relevant pins.

Place the tub near a doorway, and place the laser pointer on the other side, with its beam shining into the straw. Run your code and try walking through the doorway: the alarm should go off!

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.