The Light class constructs objects that represent a single analog or digital light light component attached to the physical board. Light sensors are used to measure ambient or direct light intensity.

Supported Light sensors:

This list will continue to be updated as more devices are confirmed.

Parameters

  • pin A Number or String address for the Light pin (analog).

  • options An object of property parameters.

    PropertyTypeValue/DescriptionDefaultRequired
    pinNumber, StringAnalog Pin. The Number or String address of the pin the light is attached to, ie. "A0"No
    controllerstringDEFAULT, EVS_EV3, TSL2561. The Name of the controller to use.no
    freqNumberMilliseconds. The rate in milliseconds to emit the data event25msno
    thresholdNumberAny. The change threshold (+/- value).1no

Shape

Property NameDescriptionRead Only
idA user definable id value. Defaults to a generated uidNo
pinThe pin address that the Light is attached toNo
thresholdThe change threshold (+/- value). Defaults to 1No
valueRaw valueYes
levelIntensity level 0-1 (%)Yes

Component Initialization

Analog Light (Most common case)

new five.Light("A0");

HTU21D

Usage

Analog Light

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

board.on("ready", function() {
  var light = new five.Light("A0");
  light.on("change", function() {
    console.log(this.level);
  });
});

API

  • within(range, handler) When value is within the provided range, execute callback.
    var light = new five.Light("A0");
    
    light.within([ 0.10, 0.20 ], function() {
      
      // This is called when the light's value property falls within 100-200
    
    });
    

Events

  • change The "change" event is emitted whenever the value of the light changes more than the threshold value allows.
  • data The "data" event is fired as frequently as the user defined freq will allow in milliseconds.

Examples

Hi! The Johnny-Five community is building new projects every day. We made this newsletter to tell you about what's new, what's good, and what's next for Open Source robotics. Join us in exploring what we can make together.

Fork me on GitHub