How to build an optical/light barrier with the Arduino, the Keyes laser module (KY-008) and a laser receiver/detector module

Light barrier built with the Arduino, a laser module, and a laser detector module.

In this tutorial, it shown how to build an optical barrier. The barrier detects any movement between the sender and the receiver. The sender is a laser module (Keyes KY-008) that emits red light (wavelength: 650nm). The laser detector/receiver is an unnamed module that returns either a LOW signal or a HIGH signal (no “analog values” in between).

List of materials:

– Arduino Uno [Search on Aliexpress | Amazon]
– Jumper wires (female/male) [Search on Aliexpress | Amazon]
– KY-008 red laser module [Search on Aliexpress | Amazon]
– Unnamed laser detector/receiver [Search on Aliexpress | Amazon]

Schematic of a laser barrier.

How to connect the laser module and laser detector to the Arduino?

The laser module has three pins. Only two pins are used: The “-” pin is connected to one of the Arduino’s GND pins. The “S” pin of the laser module is connected to the Arduino’s pin “2”. Therby, the laser can be switched off or on by sending either a LOW signal or a HIGH signal. The detector module has also three pins. The module’s “VCC” pin is connected to the Arduino’s “5V” pin and the GND of the module is connected to the Arduino’s GND. Lastly, the signal pin”OUT” of the module is connected to pin “3”.

How to program the light/optical barrier?
First, the pin values for the laser and the receiver are initialized. Next, the laser pin is set to OUTPUT mode and the receiver pin is set to INPUT mode. Then, the laser is switched on by setting “pinLaser” to HIGH. In order to monitor the light barrier’s state, a serial connection is started.
In the loop function, the value of the receiver is read. The retrieved value can either be LOW or HIGH. LOW means that the laser does not reach the detector, e.g., something went through the light barrier and is located between the laser and the detector. Then, the value of the detector is written to the console. Finally, a delay of 1000ms is added for convenience purposes (serial monitor is not “flashing” the values).

// (c) Michael Schoeffler 2014,

const int pinLaser = 2; // output signal pin of laser module/laser pointer
const int pinReceiver = 3; // input signal pin of receiver/detector (the used module does only return a digital state)

void setup() {
  pinMode(pinLaser, OUTPUT); // set the laser pin to output mode
  pinMode(pinReceiver, INPUT); // set the laser pin to output mode
  digitalWrite(pinLaser, HIGH); // emit red laser
  Serial.begin(9600); // Setup serial connection for print out to console

void loop() {
  int value = digitalRead(pinReceiver); // receiver/detector send either LOW or HIGH (no analog values!)
  Serial.println(value);  // send value to console
  delay(1000); // wait for 1000ms

If the code has been successfully uploaded to the Arduino, the output on the Serial Monitor (Tools->Serial Monitor, Ctrl+Shift+M) should look like this:

Serial monitor output of the light barrier example. If something is inside the light barrier, “0” is printed out to the monitor. Otherwise, if the red laser light points to the detecctor, “1” is printed out.

The detector’s LED, which is located next to the detector, indicates whether the detector is connected to a power supply.

Video Tutorial:

Leave a Reply

Your email address will not be published. Required fields are marked *