Breadboard
docs
Example project · Displays & sensors

Laser tripwire alarm

A laser aimed at a photoresistor; the ESP32 reads the light on its ADC and sounds the buzzer when the beam is broken. Drop the photoresistor’s Light-level slider to break the beam.

The Laser tripwire alarm circuit as rendered by the simulator

What's on the bench

  • Battery
  • Buzzer
  • ESP32-C3
  • Laser (KY-008)
  • Photoresistor
  • Resistor

The code

This MicroPython script runs on the emulated board every boot; edit it in the Code tab.

from machine import Pin, ADC
import time
adc = ADC(Pin(0))
adc.atten(ADC.ATTN_11DB)
buzz = Pin(1, Pin.OUT, value=0)
# Bright beam on the photoresistor => high reading. Break it => alarm.
while True:
    light = adc.read_u16() / 65535
    broken = light < 0.3
    buzz(1 if broken else 0)
    print('ALARM' if broken else 'armed', round(light, 2))
    time.sleep_ms(300)