Skip to content
Rick Waldron edited this page Aug 5, 2013 · 14 revisions

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

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);

Tinkerkit:

// Attached to "Output 0"
var digital = new five.Led("O0");
  • options An object of property parameters.
Property Name 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
```js var pwm = new five.Led({ pin: 11, type: "PWM" }); ```

Shape

{ 
  board: ...A reference to the board object the Led is attached to
  id: ...A user definable id value. Defaults to null
  pin: ...The pin address that the Led is attached to
  value: ...The current value of the Led. READONLY
  interval: ...An interval reference, if an interval exists
}

Usage

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

board.on("ready", function() {

  // Leonardo's pin 13 is PWM capable
  var pin = this.type === "LEONARDO" ? 13 : 11;

  // Create a standard `led` hardware instance
  var led = new five.Led(pin);

  // Inject led object into REPL session
  this.repl.inject({
    led: led
  });
});

API

  • on() Turn the led on.
var led = new five.Led(13);

led.on();
  • off() Turn the led off.
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) Strobe/Blink the Led on/off in phases over ms. This is an interval operation and can be stopped by calling led.stop(), however that will not necessarily turn it "off". Defaults to 500ms.
var led = new five.Led(13);

// Strobe on-off in 500ms phases
led.strobe(500);
  • blink(ms) 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(13);

// This will set the brightness to about half 
led.brightness(128);
  • fadeIn(ms) Fade in from current brightness over ms. 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(13);

// Fade in over 500ms.
led.fadeIn(500);
  • fadeOut(ms) Fade out from current brightness over ms. 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(13);

// Fade out over 500ms.
led.fadeOut(500);
  • pulse(ms) Pulse the Led in phases from on to off over ms time. 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(13);

// Pulse from on to off in 500ms phases
led.pulse(500);
  • stop(ms) 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(13);

// 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