The Led.RGB class constructs objects that represent an RGB Led.

Parameters

  • pins An Array containing the pins red, green and blue. All pins must support PWM.

    IndexTypeValue/DescriptionDefaultRequired
    0NumberPWM capable to control redyes
    1NumberPWM capable to control greenyes
    2NumberPWM capable to control blueyes

  • options

    PropertyTypeValue/DescriptionDefaultRequired
    pinsObjectSee pins table belowYes (either)
    pinsArraySee pins table belowYes (either)
    isAnodeBooleantrue, false. Set this to true to indicate the LED is a common anode LED. Defaults to false, indicating a common cathode LED.no
    controllerStringDEFAULT, PCA9685, BLINKM. Controller interface type."DEFAULT"no

    • pins (Object)

      PropertyTypeValue/DescriptionDefaultRequired
      redNumberPWM capable to control redyes
      greenNumberPWM capable to control greenyes
      blueNumberPWM capable to control blueyes

    • pins (Array)

      IndexTypeValue/DescriptionDefaultRequired
      0NumberPWM capable to control redyes
      1NumberPWM capable to control greenyes
      2NumberPWM capable to control blueyes

Shape

Property NameDescriptionRead Only
redLed objectNo
greenLed objectNo
blueLed objectNo
isAnodeBooleanYes

Component Initialization

LED RGB

// With Options object & pins object
new five.Led.RGB({
  pins: {
    red: 6,
    green: 5,
    blue: 3
  }
});

// With Options object & pins array
new five.Led.RGB({
  pins: [6, 5, 3]
});

// With pins array
new five.Led.RGB([6, 5, 3]);

led rgb

LED RGB Anode

// With Options object & pins object
new five.Led.RGB({
  pins: {
    red: 6,
    green: 5,
    blue: 3
  },
  isAnode: true
});

// With Options object & pins array
new five.Led.RGB({
  pins: [6, 5, 3],
  isAnode: true
});

led rgb anode

LED RGB PCA9685

// With Options object & pins object
new five.Led.RGB({
  controller: "PCA9685",
  pins: {
    red: 2,
    green: 1,
    blue: 0
  }
});

// With Options object & pins array
new five.Led.RGB({
  controller: "PCA9685",
  pins: [2, 1, 0]
});

Tessel PCA9685

Arduino PCA9685

LED RGB BlinkM

new five.Led.RGB({
  controller: "BLINKM",
});

BlinkM

BlinkM

Usage

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

board.on("ready", function() {
  var a = new five.Led.RGB([ 9, 10, 11 ]);

  var b = new five.Led.RGB({
    pins: {
      red: 3,
      green: 5,
      blue: 6
    }
  });

  this.repl.inject({
    a: a,
    b: b
  });

  a.strobe(500);
  b.strobe(1000);
});

API

  • on() Turn the led on, honoring any previous color value

    var led = new five.Led.RGB([9, 10, 11]);
    
    led.on();
    
  • off() Turn the Led off.

    var led = new five.Led.RGB([9, 10, 11]);
    
    led.off();
    
  • color(value) Sets the Led color. If no value is supplied, it returns a copy of the state value.

    var led = new five.Led.RGB([9, 10, 11]);
    
    led.color("#ff00ff");
    
    console.log( led.color() );
    // {red: 255, blue: 0, green: 255}
    
Valid Color ArgumentsExample(s)In Situ
Any CSS color name string"red", "green", "blue"rgb.color("red")
Hexadecimal color strings"ff0000", "00ff00", "0000ff"rgb.color("0000ff")
Hexadecimal color strings, w/ leading #"#ff0000", "#00ff00", "#0000ff"rgb.color("#0000ff")
Array of 8-bit bytes[0xff, 0x00, 0x00]rgb.color([0xff, 0x00, 0x00])
Object of 8-bit bytes{ red: 0x00, green: 0xFF, blue: 0x00 }rgb.color({ red: 0x00, green: 0xFF, blue: 0x00 })
  • intensity(value) Sets the overall intensity or "brightness" of the Led. Maintains current color state. If no value is supplied, it returns the current intensity value.

    var led = new five.Led.RGB([9, 10, 11]);
    
    // Set the initial color (red)
    led.color("#ff0000");
    
    ...Sometime later...
    
    // Stays red, but at 30% intensity
    led.intensity(30);
    
    
  • toggle() Toggle the current state, if on then turn off, if off then turn on.

    var led = new five.Led.RGB([9, 10, 11]);
    
    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.RGB([9, 10, 11]);
    
    // Strobe on-off in 500ms phases
    led.strobe(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().

    var led = new five.Led.RGB([9, 10, 11]);
    
    // Blink from on to off in 500ms phases
    led.strobe(500);
    
    ...Sometime later...
    
    led.stop();
    
    

Events

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

Examples

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