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.
Do these lessons first
- Voltage, current, resistanceavailablethe voltage divider is the key new idea here: why the 10 kΩ, how a voltage is measured.
- Your first program: connect and upload code to your ESP32availableinstall the Arduino IDE, connect the board, upload a sketch.
- 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.
No link on purpose: we have not checked a listing yet.
- 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
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.
Show the 12 wires and the pin table
Wire list
| # | Colour | From | To |
|---|---|---|---|
| 1 | red | ESP32 3V3 | + rail (red) |
| 2 | black | ESP32 GND | − rail (blue) |
| 3 | yellow | ESP32 GPIO34 | sensor row |
| 4 | orange | ESP32 GPIO25 | buzzer S |
| 5 | red | + rail | buzzer + |
| 6 | black | buzzer − | − rail |
| 7 | green | ESP32 GPIO26 | 220 Ω → LED long leg |
| 8 | black | LED short leg | − rail |
| 9 | blue | ESP32 GPIO27 | button leg A |
| 10 | black | button leg B (diagonal) | − rail |
| 11 | red | battery pack red (+) | laser S (the + supply), NOT the ESP32 |
| 12 | black | battery 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
| Pin | Goes to | Role |
|---|---|---|
3V3 | + rail: LDR, buzzer + | Power out (3.3 V) |
GND | − rail: 10 kΩ, buzzer −, LED, button | Ground |
GPIO34 | Sensor row (LDR + 10 kΩ) | Input, analog (0–4095) |
GPIO25 | Buzzer S | Output |
GPIO26 | 220 Ω → green LED | Output |
GPIO27 | Button (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).
Step 1 · Lay out your parts.
Check: Every part in the list is on the table.
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.
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/27on 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.Check: The board sits flat and no pin is bent.
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.
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.
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.
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
Sand 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.Check: The pack is off. Someone experienced has checked the red/black wires and the Class 2 label.
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.
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.
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.
Check: Number high with the beam, low when you pass a hand through.
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.
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)
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>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";Chunk 3 Pins, threshold, memory
Names for each pin, the cut-off number, and two on/off memories (armed? alarm ringing?). What to change:
thresholdShow 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;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(); }Chunk 5 Setup, runs once
Same as mission 01. Starts the Serial Monitor, sets each pin as output or input, joins Wi-Fi.
INPUT_PULLUPmeans the button reads LOW when pressed. What to change: nothingShow 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); }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);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);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 actionsShow 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?
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
The buzzer sounds as soon as you power up
No phone alert
Go further
Back to the simpler infrared version, or try these ideas:
- 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
- Espressif, ESP32 Series Datasheet (GPIO and ADC sections)
- Espressif, Arduino-ESP32 ADC API (analogRead, 12-bit range 0–4095)
- Espressif, Arduino-ESP32 GPIO API (pinMode, INPUT_PULLUP)
- Arduino, analogRead() reference
- Arduino, pinMode() reference
- Arduino, Digital pins (INPUT_PULLUP)
- SparkFun, Photocell hookup guide (LDR in a voltage divider)
- SparkFun, Voltage dividers
- ntfy documentation (publishing, phone app) (to verify: docs.ntfy.sh not reachable from our network yet)
- GL5528 photoresistor datasheet (to verify: no reachable copy found yet)
- 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.