Français

Projects · 02

Laser alarm

Upgrade of mission 01. Same alarm, but with a visible laser beam and a sensor you tune yourself.

Guard a doorway with a laser line. When someone crosses the low laser beam, a buzzer sounds and your phone gets an alert.

To testNot built by us yet: code compiled, circuit and safety checked. Build at your own risk; younger makers with an adult.
Start with mission 01 Print the sheet

Do these lessons first

  1. Voltage, current, resistanceavailablethe voltage divider is the key new idea here: why the 10 kΩ, how a voltage is measured.
  2. Your first program: connect and upload code to your ESP32availableinstall the Arduino IDE, connect the board, upload a sketch.
  3. Inputs, outputs and ifavailableread a button, switch a buzzer, let the code decide.

Done mission 01? You already know the breadboard and if/then code. Not yet? The “Reminder” boxes give the idea in two lines.

Level
Level 2extra: laser, battery pack, calibration
Time
One afternoon3–4 h · estimate
Cost
About 30–60 CADestimate
Tools
Easyno soldering, no power tools
Ask for help
2 stepssteps 13 and 16
Lesson first
Voltage, current, resistanceavailable

Not built by us yet. Younger makers: an adult follows every step of this sheet.

Safety first

Read this box before you plug anything in.

  • Class 2 laser
  • Batteries
  • Small parts
  • Not built by us yet. Younger makers: an adult follows every step of this sheet.
  • Class 2 laser only (1 mW or less, class printed on the label or the listing). No class shown, or 5 mW: do not use it.
  • Beam at ankle height, pointing into the sensor tube. Never at eyes, people or pets. Never look into the beam. No mirrors.
  • Switch the pack off when not in use. Don't run it with pets or toddlers around. Indoors only, never toward windows, vehicles or aircraft.
  • Power: a 3×AA pack for the laser; a USB power bank or the computer for the board. Never a bare LiPo cell.
  • Unplug before you rewire. Keep small parts and wires away from pets and from anyone who might put them in their mouth.

Parts

Show the 14 parts · About 30–60 CAD
  • 1

    ESP32 board×1Already have it from mission 01?

    The brain

    Exact spec: ESP32-WROOM-32 DevKit, 38 pins, USB (micro-USB or USB-C), 3.3 V logic. Clones may have a different pin order: check the labels printed on your board.

  • 2

    Laser module×1

    Makes the invisible-line beam

    Exact spec: Red 650 nm dot laser module, 5 V. Buy only a module whose label or listing says Class 2 (≤ 1 mW). If the listing shows no class, or shows 5 mW, do not buy it: most cheap 3-pin modules are 5 mW (Class 3R) and unlabelled.

  • 3

    3×AA battery holder×1

    Powers the laser away from the board

    Exact spec: 3-cell AA holder with on/off switch and bare or JST wires; 3 AA alkaline batteries (not included). 3×AA gives about 4.5 V for a 5 V module: to confirm when we build it.

  • 4

    Photoresistor (LDR)×1

    Senses the laser light

    Exact spec: GL5528 type, 5 mm

  • 5

    10 kΩ resistor×1

    Pairs with the sensor (voltage divider)

    Exact spec: 10 kΩ, 1/4 W, through-hole, 5 % or better

  • 6

    220 Ω resistor×1Already have it from mission 01?

    Protects the LED

    Exact spec: 220 Ω, 1/4 W, through-hole

  • 7

    Green LED×1Already have it from mission 01?

    Shows the alarm is armed

    Exact spec: 5 mm green LED, through-hole

  • 8

    Active buzzer module×1Already have it from mission 01?

    Makes the alarm sound

    Exact spec: Active (built-in tone) buzzer module, 3 pins S / + / −, 3.3–5 V, sounds when S is HIGH. If it buzzes right at power-up, it is low-trigger: return it or swap HIGH/LOW in the code.

  • 9

    Push button×1Already have it from mission 01?

    Arms and disarms

    Exact spec: 6 mm tactile switch, 4 legs

  • 10

    Breadboard×1Already have it from mission 01?

    Connects parts without soldering

    Exact spec: Full-size 830 points. A 38-pin ESP32 leaves a free hole on one side only; board too wide? Use two breadboards side by side.

  • 11

    Jumper wires (male-to-male)1 setAlready have it from mission 01?

    Link the parts

    Exact spec: Male-to-male, about 10–20 cm, 20 wires or more

  • 12

    Jumper wires (female-to-male)×5

    Laser side (3), and a longer LDR if needed (2): a pack of 10 or more is common

    Exact spec: Female-to-male, about 20 cm or longer. The battery holder wires will not hold on the laser's male pins by themselves (a mini breadboard also works there).

  • 13

    USB cable×1Already have it from mission 01?

    Power and upload code

    Exact spec: Data cable (not charge-only) matching the board's port

  • 14

    USB power bank×1optionalAlready have it from mission 01?

    Runs the alarm without the computer

    Exact spec: Any 5 V power bank

The shopping list is at the bottom of the page.

Tools

Show the 5 tools (3 optional)
  • Computer with Arduino IDE

    Upload the code

    Exact spec: Arduino IDE 2.x + “esp32 by Espressif” 3.3.x (free download from arduino.cc)

  • Small screwdriver

    Open the battery holder if needed

    Exact spec: Small Phillips (or flat)

  • Multimeteroptional

    Check wires and voltages

    Exact spec: Any basic digital multimeter

  • 3D printeroptional

    Print the holders

    Exact spec: Black PLA. No printer: a black straw or rolled black paper works

  • Tapeoptional

    Hold the laser and tube in place

    Exact spec: Masking tape or blue tack

How it works

A photoresistor (LDR) changes resistance with light. The ESP32 reads voltage, not resistance, so the LDR is paired with a 10 kΩ resistor: a voltage divider. More light, higher voltage.

The ESP32 turns the voltage into a number from 0 to 4095. When someone breaks the beam, the number drops under a threshold and the alarm starts. A short black tube around the sensor keeps room light out, so only the laser counts.

The laser has its own battery pack because it sits on the other side of the doorway. Only light crosses the door, no wire.

Related lessons: Voltage, current, resistance · Your first program: connect and upload code to your ESP32 · Inputs, outputs and if

Wiring

Two views of the same circuit: the breadboard as you will build it, and the schematic. Tap a diagram to open it full screen.

Mission 01 wiring, breadboard view Full-size breadboard seen from above. The ESP32 straddles the middle gap at the left end, USB port to the left; only the top row of holes (j) stays free next to its pins. Twelve numbered wires: 1 red from 3V3 to the red plus rail, 2 black from GND to the blue minus rail, 3 yellow from GPIO34 to the sensor row, where the photoresistor and the 10 kilo-ohm resistor meet; the photoresistor's other leg goes into the plus rail and the resistor's other leg into the minus rail. 4 orange from GPIO25 to the buzzer S pin, 5 red from the plus rail to buzzer plus, 6 black from buzzer minus to the minus rail. 7 green from GPIO26 to the 220 ohm resistor, which leads to the LED long leg; 8 black from the LED short leg to the minus rail. 9 blue from GPIO27 to one button leg, 10 black from the diagonal button leg to the minus rail. The laser is not on the breadboard: on the other side of the door, wire 11 joins the battery pack red wire to laser pin S and wire 12 the black wire to laser pin minus; the middle pin stays empty. FIG. 1 — BREADBOARD VIEW (FROM ABOVE)+−−+jihgfedcba+−−+jihgfedcba25303540455055ESP325VCMDD3D213GND1214GPIO27GPIO26GPIO25333235GPIO34VNVPEN3V3USBS+−+AB3GPIO34 → sensor row4GPIO25 → buzzer S7GPIO26 → 220 Ω → LED +9GPIO27 → button A12568103V3 → +GND → −LDR10 kΩsensor rowbuzzer module220 Ωgreen LEDbuttonLED: long leg (+) toward the 220 Ω, flat side = short leg (−).Other side of the doorNot on the breadboard, not wired to the ESP32+−+OFF3×AA pack · 4.5 VCLASS 2laser moduleS−empty111211 red (+) → S · 12 black (−) → −Middle pin: empty.Keep the pack OFF until you aim (ask for help).Wire list#COLOURFROMTO1redESP32 3V3+ rail (red)2blackESP32 GND− rail (blue)3yellowESP32 GPIO34sensor row4orangeESP32 GPIO25buzzer S5red+ railbuzzer +6blackbuzzer −− rail7greenESP32 GPIO26220 Ω → LED long leg8blackLED short leg− rail9blueESP32 GPIO27button leg A10blackbutton leg B (diagonal)− rail11redbattery pack red (+)laser S12blackbattery pack black (−)laser −Direct, no wire: LDR leg → + rail · 10 kΩ leg → − rail.
Tap the diagram to open it full screen.
Mission 01 wiring, schematic IEC schematic. The ESP32's 3V3 pin feeds a supply line. The photoresistor goes from 3V3 to a node wired to GPIO34; a 10 kilo-ohm resistor goes from that node to ground, forming a voltage divider: more laser light, higher voltage on GPIO34. GPIO25 drives the S pin of the active buzzer module, whose plus pin is on 3V3 and minus pin on ground. GPIO26 goes through a 220 ohm resistor to the green LED, then to ground. GPIO27 goes to a push button whose other side is ground; the code turns on the internal pull-up. Across the doorway, separate from the board, a 3×AA pack (4.5 V) with its switch powers the laser module through pins S and minus; the middle pin is not connected. Only light crosses the door. FIG. 2 — SCHEMATIC (IEC SYMBOLS)ESP32DevKit3V3GPIO34GPIO25GPIO26GPIO27GND3V3 (+ rail)laser lightLDR10 kΩsensor rowS+−buzzer(active)220 Ωgreen LEDbuttonGPIO27 uses the internal pull-up: pressed = LOW.13479568102doorway: only light crossesswitch3×AA · 4.5 VS−emptybeamlaserClass 21112
Tap the diagram to open it full screen.
Show the 12 wires and the pin table

Wire list

#ColourFromTo
1 redESP32 3V3+ rail (red)
2 blackESP32 GND− rail (blue)
3 yellowESP32 GPIO34sensor row
4 orangeESP32 GPIO25buzzer S
5 red+ railbuzzer +
6 blackbuzzer −− rail
7 greenESP32 GPIO26220 Ω → LED long leg
8 blackLED short leg− rail
9 blueESP32 GPIO27button leg A
10 blackbutton leg B (diagonal)− rail
11 redbattery pack red (+)laser S (the + supply), NOT the ESP32
12 blackbattery pack black (−)laser −, NOT the ESP32

No wire needed: the LDR's other leg goes straight into the + rail, the 10 kΩ's other leg into the − rail. The laser's middle pin stays empty. Check your laser's pinout: on S / middle / − modules, S is the + supply.

ESP32 pins

PinGoes toRole
3V3+ rail: LDR, buzzer +Power out (3.3 V)
GND− rail: 10 kΩ, buzzer −, LED, buttonGround
GPIO34Sensor row (LDR + 10 kΩ)Input, analog (0–4095)
GPIO25Buzzer SOutput
GPIO26220 Ω → green LEDOutput
GPIO27Button (other leg to GND)Input with internal pull-up

Build steps

Do one step, check it, then go on.

Built mission 01? Reuse the same ESP32, rails, buzzer, LED and button. Skip steps 3, 4, 10, 11, 12 and 14. Remove the IR modules (they used GPIO32; the LDR uses GPIO34, the other pins are identical).

  1. Step 1 · Lay out your parts.

    Check: Every part in the list is on the table.

  2. Step 2 · Make the sensor tube.

    Roll black paper into a tube about 3 cm long and 6 mm wide inside (or print one in black PLA, or cut a black drinking straw). The LDR will sit at the bottom.

    Check: The LDR is at the end, and no side light gets in.

  3. Step 3 · Place the ESP32.

    Press the board across the middle gap of the breadboard, so each row of pins has its own holes. Put the side with 3V3 / GPIO34 / 25 / 26 / 27 on the side that keeps a free hole. Board too wide? Use two breadboards. Clones may have a different pin order: check the labels printed on your board. USB port at the end of the breadboard, no cable yet.

    Step 3: place the ESP32 The ESP32 is lowered onto the left end of the breadboard, USB port at the end, its two pin rows going into rows i and a on either side of the middle gap. A zoom shows the board's cross-section: pins in a and i, one free hole in j next to each pin. 3×1×1830+−aijOne free hole (j) beside each pin

    Check: The board sits flat and no pin is bent.

  4. Step 4 · Rails.

    Wire the ESP32 pin 3V3 to the red (+) rail and a GND pin to the blue (−) rail.

    Step 4: power rails Two jumpers: wire 1 red from the ESP32 3V3 pin to the red plus rail, wire 2 black from a GND pin to the blue minus rail. 4×2+−123V3GND+−

    Check: Red wire on red rail, blue/black wire on blue rail.

  5. Step 5 · Voltage divider, part 1.
  6. Step 6 · Voltage divider, part 2.

    Put one LDR leg in the sensor row and the other leg in the + (3V3) rail. LDR legs have no direction. The sensor has to reach the doorway: set the breadboard on the floor by the door frame (USB power bank there), or extend the LDR with two long female-to-male jumpers.

    Check: The sensor row now holds one resistor leg and one LDR leg.

  7. Step 7 · Connect to the board.

    Jumper from the sensor row to GPIO34.

    Check: Three things meet in the sensor row: resistor, LDR, wire to GPIO34.

  8. Step 8 · Upload the code.

    Install the board package, open the sketch, plug in the USB cable, choose “ESP32 Dev Module”, upload.

    Step 8: plug in USB and upload A USB data cable goes into the ESP32's port at the end of the breadboard; the other end goes to the computer running the Arduino IDE. 8×1data×1+−IDE

    Check: “Done uploading” appears.

  9. Step 9 · Test the sensor.

    Open the Serial Monitor at 115200 baud. Cover the LDR with a finger, then uncover it.

    Check: The number drops when covered and rises when uncovered.

  10. Step 10 · Add the buzzer.

    Unplug USB first. Buzzer S to GPIO25, + to the 3V3 rail, − to the GND rail. If it buzzes as soon as you power up, it is a low-trigger module: return it or swap HIGH/LOW in the code.

    Step 10: add the buzzer USB unplugged first. The buzzer module's three pins go into row f, columns 38 to 40. Wire 4 orange goes from GPIO25 to pin S, wire 5 red from the plus rail to pin plus, wire 6 black from pin minus to the minus rail. 10×1×3+−456S+−

    Check: Pins match the wiring table.

  11. Step 11 · Add the green LED.
  12. Step 12 · Add the arm button.
  13. Step 13 · Wire the laser. Ask for help

    Class 2 only: the label or listing must say Class 2 (≤ 1 mW); no class, or 5 mW, do not use it. Check your module's pinout: on S / middle / − modules, S is the + supply. Push a female-to-male jumper onto pin S and one onto pin −, then join S to the red wire of the battery pack and − to the black wire (or use a mini breadboard). The middle pin stays empty. Keep the pack switched off, and switch it off whenever you are not using it. Don't run it with pets or toddlers around.

    Step 13: wire the laser (ask for help) Close-up, away from the breadboard. The 3×AA pack has its switch on OFF. Wire 11 joins the pack's red wire to laser pin S; wire 12 joins the black wire to laser pin minus (a female-to-male jumper on each pin, or a mini breadboard, makes the joint hold). The middle pin stays empty. The module is labelled Class 2. 13×1Class 2×13×AAOFFCLASS 2S−1112+−

    Check: The pack is off. Someone experienced has checked the red/black wires and the Class 2 label.

  14. Step 14 · Set up the phone.

    Install the free ntfy app, subscribe to a topic you make up (random letters and numbers, no names). Anyone who guesses it can read it.

    Check: The topic appears in the app.

  15. Step 15 · Fill in the code.

    Type your Wi-Fi name, password and the same topic into the sketch. Use a 2.4 GHz Wi-Fi network (the ESP32 does not do 5 GHz). Upload again.

    Check: The topic in the code matches the app exactly.

  16. Step 16 · Aim the beam.Safety reminder in this step Ask for help

    Put the sensor tube on one side of the doorway and the laser on the other, both at ankle height, the laser pointing into the tube. Set the breadboard on the floor by the door frame (USB power bank there), or extend the LDR with two long female-to-male jumpers. Indoors only, never toward windows, vehicles or aircraft; no pets or toddlers around. Switch on the pack and watch the Serial Monitor: the number jumps up when the dot is in the tube. Write down this “lit” number (laser on target) and the “dark” number (hand in the beam). Switch the pack off when you are done.

    Step 16: aim the beam (ask for help) Seen from above: the laser on one side of the doorway and the sensor tube on the other, facing each other, the beam crossing the doorway. Side view: both at ankle height, about 10 centimetres above the floor, below knee height. A crossed-out view shows the beam at eye level: never do that. 16×1×1From abovelasertube + LDRbeamSide view≈ 10 cmknee: maxnever eye level

    Check: Number high with the beam, low when you pass a hand through.

  17. Step 17 · Set the threshold.

    Pick a number halfway between your “lit” and “dark” numbers (from step 16) and put it in threshold. Upload again.

    Check: The threshold is between the two numbers.

  18. Step 18 · Test like a spy.

    Press the button, leave the room within 5 seconds, walk back in.

    Step 18: test like a spy Three moments. 1: press the button. 2: the green LED blinks for 5 seconds while you leave, then stays on. 3: walking back in, a foot breaks the beam; the buzzer sounds and the phone gets an alert. Press the button again to silence it. 18125 s3

    Check: LED blinks, then stays on; the buzzer sounds and the phone alert arrives. Press the button to silence and disarm.

Code walkthrough

Board: ESP32 Dev Module (ESP32-WROOM-32 DevKit, 38 pins). The code below compiles in our CI with the ESP32 board package 3.3.12.

Nothing to install: WiFi and HTTPClient come with the ESP32 board package.

Download the .ino file Download the sketch folder (.zip)

  1. Chunk 1 Header and libraries

    Notes plus three libraries for Wi-Fi and web requests. What to change: nothing

    Show the code (10 lines)
    // Tinker Mission — Project 02: Laser alarm
    // Board: ESP32 Dev Module ("esp32 by Espressif" 3.3.x). No extra library needed.
    //
    // SAFETY: use a laser module labelled CLASS 2 (under 1 mW) only. Mount it at ankle height,
    // pointing into the sensor tube, never at eye level. Never look into the beam, never point it
    // at people or pets, no mirrors. Power: 3×AA pack or USB power bank, never a LiPo cell.
    
    #include <WiFi.h>
    #include <WiFiClientSecure.h>
    #include <HTTPClient.h>
  2. Chunk 2 Your settings

    Your Wi-Fi (2.4 GHz) and the secret topic the phone listens to. What to change: all three

    Show the code (5 lines)
    const char* WIFI_NAME = "YourWiFi";        // your home Wi-Fi name
    const char* WIFI_PASS = "YourPassword";    // your home Wi-Fi password
    // Phone alert topic: make up your own random name (anyone who guesses it can read it).
    // Keep names and personal details out of it.
    const char* NTFY_TOPIC = "tripwire-change-me-4821";
  3. Chunk 3 Pins, threshold, memory

    Names for each pin, the cut-off number, and two on/off memories (armed? alarm ringing?). What to change: threshold

    Show the code (8 lines)
    const int LDR_PIN = 34;      // light sensor (middle of the voltage divider)
    const int BUZZER_PIN = 25;
    const int LED_PIN = 26;      // green = armed
    const int BUTTON_PIN = 27;
    int threshold = 2000;        // halfway between your "lit" and "dark" numbers
    
    bool armed = false;
    bool alarmOn = false;
  4. Chunk 4 Send the phone alert

    Same as mission 01. If Wi-Fi is up, sends one short message to the ntfy service. What to change: the message text

    Show the code (9 lines)
    void sendPhoneAlert() {
      if (WiFi.status() != WL_CONNECTED) return;
      WiFiClientSecure client;
      client.setInsecure();      // simple mode, fine for a home project
      HTTPClient http;
      http.begin(client, String("https://ntfy.sh/") + NTFY_TOPIC);
      http.POST("Tripwire: someone crossed the beam at the door.");
      http.end();
    }
  5. Chunk 5 Setup, runs once

    Same as mission 01. Starts the Serial Monitor, sets each pin as output or input, joins Wi-Fi. INPUT_PULLUP means the button reads LOW when pressed. What to change: nothing

    Show the code (7 lines)
    void setup() {
      Serial.begin(115200);
      pinMode(BUZZER_PIN, OUTPUT);
      pinMode(LED_PIN, OUTPUT);
      pinMode(BUTTON_PIN, INPUT_PULLUP);  // pressed = LOW
      WiFi.begin(WIFI_NAME, WIFI_PASS);
    }
  6. Chunk 6 Read the light, repeat forever

    Reads the sensor (0 dark to 4095 bright) and prints it. What to change: nothing

    Show the code (3 lines)
    void loop() {
      int light = analogRead(LDR_PIN);    // 0 (dark) to 4095 (bright)
      Serial.println(light);
  7. Chunk 7 The button

    Each press flips armed/disarmed and stops the buzzer. When arming, the LED blinks for 5 s so you can leave. Then it waits for you to release the button. What to change: i < 10 (10 half-seconds = 5 s)

    Show the code (15 lines)
      if (digitalRead(BUTTON_PIN) == LOW) {   // button pressed
        armed = !armed;                       // flip armed / disarmed
        alarmOn = false;
        digitalWrite(BUZZER_PIN, LOW);
        if (armed) {                          // 5 seconds to leave the room
          for (int i = 0; i < 10; i++) {
            digitalWrite(LED_PIN, i % 2);
            delay(500);
          }
        }
        while (digitalRead(BUTTON_PIN) == LOW) delay(10);  // wait until the button is released
        delay(300);                           // ignore button bounce
      }
    
      digitalWrite(LED_PIN, armed);
  8. Chunk 8 The alarm

    If armed, not yet ringing and the light falls under the threshold: ring and alert once. Then a short pause. What to change: the pause (20) or add more actions

    Show the code (7 lines)
      if (armed && !alarmOn && light < threshold) {  // beam broken!
        alarmOn = true;
        digitalWrite(BUZZER_PIN, HIGH);
        sendPhoneAlert();
      }
      delay(20);
    }
The whole sketch, in one piece
// Tinker Mission — Project 02: Laser alarm
// Board: ESP32 Dev Module ("esp32 by Espressif" 3.3.x). No extra library needed.
//
// SAFETY: use a laser module labelled CLASS 2 (under 1 mW) only. Mount it at ankle height,
// pointing into the sensor tube, never at eye level. Never look into the beam, never point it
// at people or pets, no mirrors. Power: 3×AA pack or USB power bank, never a LiPo cell.

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>

const char* WIFI_NAME = "YourWiFi";        // your home Wi-Fi name
const char* WIFI_PASS = "YourPassword";    // your home Wi-Fi password
// Phone alert topic: make up your own random name (anyone who guesses it can read it).
// Keep names and personal details out of it.
const char* NTFY_TOPIC = "tripwire-change-me-4821";

const int LDR_PIN = 34;      // light sensor (middle of the voltage divider)
const int BUZZER_PIN = 25;
const int LED_PIN = 26;      // green = armed
const int BUTTON_PIN = 27;
int threshold = 2000;        // halfway between your "lit" and "dark" numbers

bool armed = false;
bool alarmOn = false;

void sendPhoneAlert() {
  if (WiFi.status() != WL_CONNECTED) return;
  WiFiClientSecure client;
  client.setInsecure();      // simple mode, fine for a home project
  HTTPClient http;
  http.begin(client, String("https://ntfy.sh/") + NTFY_TOPIC);
  http.POST("Tripwire: someone crossed the beam at the door.");
  http.end();
}

void setup() {
  Serial.begin(115200);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // pressed = LOW
  WiFi.begin(WIFI_NAME, WIFI_PASS);
}

void loop() {
  int light = analogRead(LDR_PIN);    // 0 (dark) to 4095 (bright)
  Serial.println(light);

  if (digitalRead(BUTTON_PIN) == LOW) {   // button pressed
    armed = !armed;                       // flip armed / disarmed
    alarmOn = false;
    digitalWrite(BUZZER_PIN, LOW);
    if (armed) {                          // 5 seconds to leave the room
      for (int i = 0; i < 10; i++) {
        digitalWrite(LED_PIN, i % 2);
        delay(500);
      }
    }
    while (digitalRead(BUTTON_PIN) == LOW) delay(10);  // wait until the button is released
    delay(300);                           // ignore button bounce
  }

  digitalWrite(LED_PIN, armed);

  if (armed && !alarmOn && light < threshold) {  // beam broken!
    alarmOn = true;
    digitalWrite(BUZZER_PIN, HIGH);
    sendPhoneAlert();
  }
  delay(20);
}

Is the code tested?

To test

The code is compiled automatically every time we change it, so it should build without errors. A real test on real hardware happens when we build this project; until then this sheet stays marked “To test”, and some details (cost, timing, threshold) may change.

Test checklist

  • Serial Monitor numbers change when you cover the sensor
  • The LED blinks for 5 seconds after pressing the button, then stays on
  • Walking through the beam sounds the buzzer
  • The phone gets the ntfy alert
  • Pressing the button again silences and disarms everything

Troubleshooting

The alarm goes off by itself
Room light is reaching the sensor: make the tube longer or darker, then set the threshold again.
The buzzer sounds as soon as you power up
It is a low-trigger module: return it, or swap HIGH and LOW for the buzzer in the code.
No phone alert
Check the Wi-Fi name and password (2.4 GHz network), and that the topic in the code matches the app exactly.

Go further

Back to the simpler infrared version, or try these ideas:

Mission 01 · Intrusion alarm

  • Two beams: guard two doors, or tell which way someone walked.
  • Night-only mode: arm the alarm only when the room is dark.

Level up

  • Secret disarm: replace the button with a knock code on the desk (a piezo disc as a knock sensor).
  • Quiet mode: blink the LED instead of the buzzer at night, and send the phone alert only.
  • Count the crossings: show how many times the beam was crossed today in the Serial Monitor.

Shopping list

Made from the parts and tools lists above, with quantities. Each line opens an Amazon.ca search; Amazon has no multi-item cart link without exact product codes, so you add items one by one. Copy the list to keep it.

Each link opens a plain Amazon.ca search built from the keywords (not an affiliate link). Pick a listing that matches the exact spec in the parts list.

Show the 19 items
  • ×1ESP32 board (Already have it from mission 01?)Amazon.ca
  • ×1Laser module—
  • ×13×AA battery holderAmazon.ca
  • ×1Photoresistor (LDR)Amazon.ca
  • ×110 kΩ resistorAmazon.ca
  • ×1220 Ω resistor (Already have it from mission 01?)Amazon.ca
  • ×1Green LED (Already have it from mission 01?)Amazon.ca
  • ×1Active buzzer module (Already have it from mission 01?)Amazon.ca
  • ×1Push button (Already have it from mission 01?)Amazon.ca
  • ×1Breadboard (Already have it from mission 01?)Amazon.ca
  • 1 setJumper wires (male-to-male) (Already have it from mission 01?)Amazon.ca
  • ×5Jumper wires (female-to-male)Amazon.ca
  • ×1USB cable (Already have it from mission 01?)Amazon.ca
  • ×1USB power bank (optional) (Already have it from mission 01?)Amazon.ca
  • ×1Computer with Arduino IDE—
  • ×1Small screwdriverAmazon.ca
  • ×1Multimeter (optional)Amazon.ca
  • ×13D printer (optional)Amazon.ca
  • ×1Tape (optional)Amazon.ca
Laser alarm
1 × ESP32 board (Already have it from mission 01?) — ESP32-WROOM-32 DevKit, 38 pins, USB (micro-USB or USB-C), 3.3 V logic. Clones may have a different pin order: check the labels printed on your board.
   https://www.amazon.ca/s?k=ESP32+DevKit+38+pin+WROOM-32
1 × Laser module — Red 650 nm dot laser module, 5 V. Buy only a module whose label or listing says Class 2 (≤ 1 mW). If the listing shows no class, or shows 5 mW, do not buy it: most cheap 3-pin modules are 5 mW (Class 3R) and unlabelled.
1 × 3×AA battery holder — 3-cell AA holder with on/off switch and bare or JST wires; 3 AA alkaline batteries (not included). 3×AA gives about 4.5 V for a 5 V module: to confirm when we build it.
   https://www.amazon.ca/s?k=3+AA+battery+holder+with+switch
1 × Photoresistor (LDR) — GL5528 type, 5 mm
   https://www.amazon.ca/s?k=GL5528+photoresistor+LDR
1 × 10 kΩ resistor — 10 kΩ, 1/4 W, through-hole, 5 % or better
   https://www.amazon.ca/s?k=10k+ohm+resistor+1%2F4W+through+hole
1 × 220 Ω resistor (Already have it from mission 01?) — 220 Ω, 1/4 W, through-hole
   https://www.amazon.ca/s?k=220+ohm+resistor+1%2F4W
1 × Green LED (Already have it from mission 01?) — 5 mm green LED, through-hole
   https://www.amazon.ca/s?k=5mm+green+LED
1 × Active buzzer module (Already have it from mission 01?) — Active (built-in tone) buzzer module, 3 pins S / + / −, 3.3–5 V, sounds when S is HIGH. If it buzzes right at power-up, it is low-trigger: return it or swap HIGH/LOW in the code.
   https://www.amazon.ca/s?k=active+buzzer+module+3+pin+5V
1 × Push button (Already have it from mission 01?) — 6 mm tactile switch, 4 legs
   https://www.amazon.ca/s?k=6mm+tactile+push+button
1 × Breadboard (Already have it from mission 01?) — Full-size 830 points. A 38-pin ESP32 leaves a free hole on one side only; board too wide? Use two breadboards side by side.
   https://www.amazon.ca/s?k=830+point+breadboard
1 set × Jumper wires (male-to-male) (Already have it from mission 01?) — Male-to-male, about 10–20 cm, 20 wires or more
   https://www.amazon.ca/s?k=male+to+male+jumper+wires
5 × Jumper wires (female-to-male) — Female-to-male, about 20 cm or longer. The battery holder wires will not hold on the laser's male pins by themselves (a mini breadboard also works there).
   https://www.amazon.ca/s?k=female+to+male+jumper+wires
1 × USB cable (Already have it from mission 01?) — Data cable (not charge-only) matching the board's port
   https://www.amazon.ca/s?k=USB+data+cable+micro+USB
1 × USB power bank (optional) (Already have it from mission 01?) — Any 5 V power bank
   https://www.amazon.ca/s?k=USB+power+bank+5V
1 × Computer with Arduino IDE — Arduino IDE 2.x + “esp32 by Espressif” 3.3.x (free download from arduino.cc)
1 × Small screwdriver — Small Phillips (or flat)
   https://www.amazon.ca/s?k=precision+screwdriver+set
1 × Multimeter (optional) — Any basic digital multimeter
   https://www.amazon.ca/s?k=digital+multimeter+basic
1 × 3D printer (optional) — Black PLA. No printer: a black straw or rolled black paper works
   https://www.amazon.ca/s?k=black+PLA+filament
1 × Tape (optional) — Masking tape or blue tack
   https://www.amazon.ca/s?k=masking+tape

Sources

Show the 11 sources
  1. Espressif, ESP32 Series Datasheet (GPIO and ADC sections)
  2. Espressif, Arduino-ESP32 ADC API (analogRead, 12-bit range 0–4095)
  3. Espressif, Arduino-ESP32 GPIO API (pinMode, INPUT_PULLUP)
  4. Arduino, analogRead() reference
  5. Arduino, pinMode() reference
  6. Arduino, Digital pins (INPUT_PULLUP)
  7. SparkFun, Photocell hookup guide (LDR in a voltage divider)
  8. SparkFun, Voltage dividers
  9. ntfy documentation (publishing, phone app) (to verify: docs.ntfy.sh not reachable from our network yet)
  10. GL5528 photoresistor datasheet (to verify: no reachable copy found yet)
  11. Laser classes, IEC 60825-1 (Class 2: 1 mW or less, Class 3R: up to 5 mW) (to verify: no reachable reputable summary found yet)

Print the sheet Print the project recap sheet

A print-ready version: parts checklist, wiring table and diagram, every step with its picture.

Built it?

I built this

Coming soon: a simple, anonymous count of builds. Nothing is collected for now.

Pinch or double-tap to zoom, swipe to change diagram.Scroll with Ctrl or double-click to zoom. Arrow keys change diagram.