JavaScript: Laser Intrusion Detection with Johnny-Five on Node.js

The Johnny-Five Tutorial Series is geared towards Arduino programming on Node.js, using the Johnny-Five framework. Get caught up here.


A fairly common security mechanism, the laser intrusion detection system (or laser trip wire), is actually the composition of two devices:

The laser emits a beam of light directly at the photocell; the photocell’s resistance can be read to determine the light exposure. The resistance will decrease as the amount of light intensifies. For the laser trip wire, a low resistance level will indicate an “unbroken” beam.


Parts:

Connect the following:

  • Laser
    • Ground to GND
    • Signal to D9
    • Power to 5V
  • Photocell
    • Power to 5V (via 200Ω resistor)
    • Ground to GND
    • Signal to 200Ω

Save the following as laser-trip-wire.js:

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  var laser = new five.Led(9);
  var detection = new five.Sensor("A0");
  var isSecure = false;

  laser.on();

  detection.scale(0, 1).on("change", function() {
    var reading = !(this.value | 0);

    if (isSecure !== reading) {
      isSecure = reading;

      if (!isSecure) {
        console.log("Intruder");
      }
    }
  });
});

Run node laser-trip-wire.js to see the program in action! To trigger the intrusion message, break the laser’s beam with your hand or any solid object.

Homework

Based on the program above, how might it be extended to trigger some kind of audible or actuated reaction to the intrusion?

* The laser and photocell used in this tutorial are included in the Sun Founder 37-in-1 Sensor Kit. Other lasers, other photocells

Comments

Contact Us

We'd love to hear from you. Get in touch!

Boston

201 South Street, Boston, MA 02111

New York

315 Church St, New York, NY 10013

Phone & Email

(617)379-2752 hello@bocoup.com