Skip to content
Scott González edited this page Apr 14, 2015 · 56 revisions

The Led class constructs objects that represent a single Led attached to the physical board.

See also:

Parameters

  • pin A Number or String address for the Led pin (digital/PWM). For Leds that only have on/off states, use a digital pin:

    var digital = new five.Led(13);

    For Leds that have on/off states, as well as inverval or color related state (Pulse, Brightness, RGB, etc), use a PWM pin (usually demarcated by either a "~" or "#" next to the pin number on the actual board).

    // Look for "~", ie. ~11
    var pwm = new five.Led(11);
  • options An object of property parameters.

    Property Type Value(s) Description Required
    pin Number Any Digital Pin The Number address of the pin the led is attached to yes
    type String "OUTPUT", "PWM" For most cases, this can be omitted; the type will be inferred based on the pin address number. no
    controller String "DEFAULT", "PCA9685" Controller interface type. Defaults to "DEFAULT". no
    address Number 0x40 Address for I2C devices. Defaults to 0x40. no

Shape

{ 
  id: ...A user definable id value. Defaults to null
  pin: ...The pin address that the Led is attached to
}

Component Initialization

LED

// Just a pin
var led = new five.Led(13);

// Options object with pin property
var led = new five.Led({
  pin: 13
});

LED

LED PCA9685

var led = new five.Led({
  controller: "PCA9685",
  pin: 0, 
});

PCA9685

Usage

// Blink an LED
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  var led = new five.Led(13);
  led.blink();
});

Tinkerkit:

// Attached to "Output 0"
var digital = new five.Led("O0");

I2C Led (i.e. via Adafruit PWM controller)

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

board.on("ready", function() {
  var led = new five.Led({
    controller: "PCA9685",
    pin: 0,
  });

  led.blink()
});

API

  • on() Turn the led on.

    var led = new five.Led(13);
    
    led.on();
  • off() Turn the led off. If a led is strobing, it will not stop. Use led.stop().off() to turn off a led while strobing.

    var led = new five.Led(13);
    
    led.off();
  • toggle() Toggle the current state, if on then turn off, if off then turn on.

    var led = new five.Led(13);
    
    led.toggle();
  • strobe(ms, callback) Strobe/Blink the Led on/off in phases over ms with an optional callback. This is an interval operation and can be stopped by calling led.stop(), however that will not necessarily turn it "off". The callback will be invoked every time the Led turns on or off. Defaults to 100ms.

    var led = new five.Led(13);
    
    // Strobe on-off in 500ms phases
    led.strobe(500);
  • blink(ms, callback) alias to strobe.

    var led = new five.Led(13);
    
    // Strobe on-off in 500ms phases
    led.blink(500);
  • brightness(0-255) Set the brightness of led. This operation will only work with Leds attached to PWM pins.

    var led = new five.Led(11);
    
    // This will set the brightness to about half 
    led.brightness(128);
  • fade(brightness, ms, callback) Fade from current brightness to brightness over ms with an optional callback. This is an interval operation and can be stopped by calling pin.stop(), however that will not necessarily turn it "off". This operation will only work with Leds attached to PWM pins.

    var led = new five.Led(11);
    
    // Fade to half brightness over 2 seconds
    led.fade(128, 2000);
  • fadeIn(ms, callback) Fade in from current brightness over ms with an optional callback. This is an interval operation and can be stopped by calling pin.stop(), however that will not necessarily turn it "off". This operation will only work with Leds attached to PWM pins.

    var led = new five.Led(11);
    
    // Fade in over 500ms.
    led.fadeIn(500);
  • fadeOut(ms, callback) Fade out from current brightness over ms with an optional callback. This is an interval operation and can be stopped by calling pin.stop(), however that will not necessarily turn it "off". This operation will only work with Leds attached to PWM pins.

    var led = new five.Led(11);
    
    // Fade out over 500ms.
    led.fadeOut(500);
  • pulse(ms, callback) Pulse the Led in phases from on to off over ms time, with an optional callback. This is an interval operation and can be stopped by calling pin.stop(), however that will not necessarily turn it "off". The callback will be invoked every time the Led is fully on or off. This operation will only work with Leds attached to PWM pins.

    var led = new five.Led(11);
    
    // Pulse from on to off in 500ms phases
    led.pulse(500);
  • stop() For interval operations, call stop to stop the interval. stop does not necessarily turn "off" the Led, in order to fully shut down an Led, a program must call stop().off(). This operation will only work with Leds attached to PWM pins.

    var led = new five.Led(11);
    
    // Pulse from on to off in 500ms phases
    led.pulse(500);
    
    ...Sometime later...
    
    led.stop();

Events

Led objects are output only and therefore do not emit any events.

Examples

Clone this wiki locally