Analog RGB LED Control

Using RGB LEDs with Espruino is straightforward and you don't really need any extra modules - you can interact with them directly using the digitalWrite() and analogWrite() functions.

On the other hand, if you want to start quickly with operations like strobing or changing the color in a familiar way then you may want to use this module instead.

There is only one thing to remember: on the board use either DAC or PWM output pins.

Examples

Create a LED instance with red on C7, green on C8 and blue on C9. By default it is on and the color is "#ffffff"

var led = require("RGBLed").connect([C7, C8, C9])

If you want the LED to be off by default then pass falsey value as a second argument:

var led = require("RGBLed").connect([C7, C8, C9], false)

If you want, you can also specify initial color:

var led = require("RGBLed").connect([C7, C8, C9], true, "#00ff00")

The same as above but for common anode LED (eg all LEDs connected to VCC rather than GND):

var led = require("RGBLed").connect([C7, C8, C9], true, "#00ff00");
led.invert(true)

Start blinking every second:

led.strobe(1000)

Change the color:

led.setColor("0000FF")

Stop blinking and leave LED on:

led.on()

Module reference

require("RGBLed").connect(pins, initialState, initialColor, isAnode) - The initialisation function has four parameters, but only the first one is required for a common cathode LED.

  • pins (Array) - Three pins, for Red, Green, and Blue
  • initialState (Boolean) - Optional. LED is on by default but you can change it with this parameter.
  • initialColor (String) - Optional. Initial color is set to "#FFFFFF" but you can change it with this parameter.

function invert(inv) - Set inv to true to indicate the LED is a common anode LED (eg all LEDs connected to VCC rather than GND). Defaults to false, indicating a common cathode LED.

function on() - Turn the Led on using previous color value. Stops the interval operation, if any.

function off() - Turn the Led off and save its color value for later use. Stops the interval operation, if any.

function toggle() - Toggle the current state. If Led is on then turn it off, if off then turn it on.

function getState() - Return current state.

function setColor(color) - Set the Led color specified as hexadecimal color value (#RRGGBB). Leading hash sign is optional. Doesn't change LED's state, so if it was off then it will stay off.

function strobe(ms) - Strobe the Led on/off in phases over time specified in milliseconds (defaults to 100ms). This is an interval operation and can be stopped by calling on() or off() function.

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.