In spy movies, thieves have to climb past laser beams without touching them. You can build the real thing with two modules from the Sensor Kit. The laser module shines a red dot onto the photoresistor. When someone walks through the beam, the light level drops and the buzzer sounds. The sketch even calibrates itself, so it works in a dark room and a bright one.
What you'll learn
- How a laser and a light sensor form a light barrier
- How a sketch can calibrate itself at the start
- How to choose a threshold between two measured values
- How to switch the laser module from a pin
What you need
- UNO R3 board and USB cable (UNO R3 Starter Kit)
- Laser module (45-in-1 Sensor Kit)
- Photoresistor module (45-in-1 Sensor Kit)
- Active buzzer module (45-in-1 Sensor Kit)
- Tape or modelling clay to hold the modules in place
- Breadboard and jumper wires (UNO R3 Starter Kit)
Laser safety
Never look into the laser or point it at anyone's eyes, including pets. The module is a low-power laser, but the beam can still hurt your eyes. Aim it low and only at the photoresistor. Children should build this with an adult nearby.
Wiring

| From | To |
|---|---|
| Laser module S | UNO pin 7 |
| Laser module − | GND |
| Photoresistor module S | UNO pin A0 |
| Photoresistor module + and − | 5V and GND |
| Active buzzer module S | UNO pin 8 |
| Active buzzer module + and − | 5V and GND |
Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.
Fix the laser at one end of the table and the photoresistor at the other, up to a metre apart, so that the red dot lands exactly on the photoresistor's round window. Modelling clay is ideal for aiming.
The sketch
const int LASER_PIN = 7;
const int LIGHT_PIN = A0;
const int BUZZER_PIN = 8;
int threshold = 0;
int lightLevel() {
return analogRead(LIGHT_PIN); // higher = brighter on most modules
}
void setup() {
Serial.begin(9600);
pinMode(LASER_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Calibrate: measure the light with the laser off and on
digitalWrite(LASER_PIN, LOW);
delay(500);
int dark = lightLevel();
digitalWrite(LASER_PIN, HIGH);
delay(500);
int lit = lightLevel();
threshold = (dark + lit) / 2; // halfway between the two
Serial.print("Laser off: ");
Serial.print(dark);
Serial.print(" laser on: ");
Serial.print(lit);
Serial.print(" threshold: ");
Serial.println(threshold);
if (abs(lit - dark) < 50) Serial.println("Check the aim: the laser hardly changes the reading!");
}
void loop() {
int level = lightLevel();
bool broken = level < threshold;
if (broken) {
Serial.println("Beam broken!");
for (int i = 0; i < 6; i++) { // short alarm
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
delay(100);
}
}
delay(20);
}
Upload the sketch with the laser already aimed, and keep your hands out of the beam for the first second while it calibrates. Open the Serial Monitor at 9600 baud to see the readings, then pass your hand through the beam.
How it works
The photoresistor's resistance drops when light falls on it, so the reading on A0 changes with brightness. A laser concentrates its light into a small dot, which makes a big difference to the reading even across a room.
Every room is different, so instead of a fixed number the sketch calibrates itself: it measures once with the laser off and once with it on, and puts the threshold exactly halfway between. Anything below the threshold means something is blocking the beam. The laser module has its own resistor, so a digital pin can switch it on and off directly.
Try this next
- Count how many times the beam was broken and print the total.
- Use two beams to tell which direction someone walked.
- Use a mirror to bounce the beam around a corner before it reaches the sensor.
Troubleshooting
- "Check the aim" appears: the dot isn't on the photoresistor's window. Re-aim and press the UNO's reset button.
-
The alarm goes off all the time: your module reads lower values in bright light. Change
level < thresholdtolevel > threshold. - The laser doesn't light: check S on pin 7 and − on GND. On some modules the middle pin is unused.
- The buzzer is always on: some buzzer modules switch on with LOW. Swap HIGH and LOW in the buzzer lines.