HomeTutorialsArduinoHow to build an optical/light barrier with the Arduino, the Keyes laser...

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
Jumper wires (female/male)
KY-008 red laser module 
Unnamed laser detector/receiver
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, http://www.mschoeffler.de

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:

How to use the red laser module KY-008 with the Arduino Uno | UATS A&S #3
Michael Schoeffler
Michael Schoeffler
Let me know in the comments what you think!


  1. I had major trouble with this until I realised that the laser ‘receiver’ was picking up the daylight for a reading rather than the laser!!

  2. Hi, would you mind sharing the frizzing file of your project. I am looking for the light barrier to use in my own sketch. But I cant find it in the frizzing library provided.
    Thank you so much, L.


Please enter your comment!
Please enter your name here


Most Popular