Module: j5e/led

Description

A module for controlling LED's using PWM.

A light-emitting diode (LED) is a semiconductor light source that emits light when current flows through it. Electrons in the semiconductor recombine with electron holes, releasing energy in the form of photons. The color of the light is determined by the energy required for electrons to cross the band gap of the semiconductor. White light is obtained by using multiple semiconductors or a layer of light-emitting phosphor on the semiconductor device.Read more on Wikipedia

Pulse-width modulation (PWM), or pulse-duration modulation (PDM), is a method of reducing the average power delivered by an electrical signal, by effectively chopping it up into discrete parts. The average value of voltage fed to the load is controlled by turning the switch between supply and load on and off at a fast rate. The longer the switch is on compared to the off periods, the higher the total power supplied to the load. Along with maximum power point tracking (MPPT), it is one of the primary methods of reducing the output of solar panels to that which can be utilized by a battery. PWM is particularly suited for running inertial loads such as motors, which are not as easily affected by this discrete switching, because their inertia causes them to react slowly. The PWM switching frequency has to be high enough not to affect the load, which is to say that the resultant waveform perceived by the load must be as smooth as possible.Read more on Wikipedia

Requires


Class: LED

The LED class allows for control of Light Emitting Diodes

Constructor

new LED(io)

Parameters

Name
Type
Description
io
(required)
number, string, or object

Description:
Pin identifier or IO Options (See instantiation)

Examples

// Using a pin number
import LED from "j5e/led";

const led = await new LED(12);
led.on();
// Using a pin identifier string
import LED from "j5e/led";

const led = await new LED("A1");
led.on();
// Using device options
import LED from "j5e/led";

const led = await new LED(12);
led.configure({
  pwm: true
});
led.on();

Properties

Name
Type
Description
value
(read only)
number
The current value of the LED
isOn
(read only)
boolean
Wether the LED is on
isRunning
(read only)
boolean
True if the LED is blinking, pulsing or animating

Methods

LED.configure

Configure an LED

led.configure(options);

Returns: The instance on which the method was called

Parameters

Name
Type
Description
options
(required)
object

Description:
Device configuration options

options.sink
number

Description:
True if an element is wired for sink drive

Example

import LED from "j5e/led";

const led = await new LED(14);
led.configure({
  sink: true
});

// With sink: true, led.on() sets pin 14 low
led.on();

LED.on

Turn an led on

led.on();

Returns: The instance on which the method was called

Example

import LED from "j5e/led";

const led = await new LED(12);
led.on();

LED.off

Turn an led off

led.off();

Returns:

Example

import LED from "j5e/led";
import {timer} from "j5e/fn";

const led = await new LED(12);
led.on();

// Wait one second and turn the led off
timer.setTimeout(function() {
  led.off();
}, 1000);

LED.toggle

Toggle the on/off state of an led

led.toggle();

Returns:

Example

import LED from "j5e/led";
import {timer} from "j5e/fn";

const led = await new LED(12);
led.toggle(); // It's on!

// Wait one second and turn the led off
timer.setTimeout(function() {
  led.toggle(); // It's off!
}, 1000);

Blink the LED on a fixed interval

led.blink(duration, callback);

Returns:

Parameters

Name
Type
Description
duration
Number

Description:
Time in ms on, time in ms off
Default: 100

callback
function

Description:
Method to call on blink

Example

import LED from "j5e/led";

const led = await new LED(12);
led.blink(1000);

LED.brightness

Set the brightness of an led

led.brightness(value);

Returns:

Parameters

Name
Type
Description
value
(required)
Number

Description:
Brightness value [0, 1]

Example

import LED from "j5e/led";

const led = await new LED(12, {
  pwm: true
});
led.brightness(0.5);

LED.intensity

Set the brightness of an led 0-100

led.intensity(value);

Returns:

Parameters

Name
Type
Description
value
(required)
Integer

Description:
Brightness value [0, 100]

Example

import LED from "j5e/led";

const led = await new LED(12, {
  pwm: true
});
led.intensity(50);

LED.fade

Fade an led from its current value to a new value (Requires ```pwm: true```)

led.fade(val, time, callback);

Returns:

Parameters

Name
Type
Description
val
(required)
Number

Description:
Target brightness value

time
Number

Description:
Time in ms that a fade will take
Default: 1000

callback
function

Description:
A function to run when the fade is complete

Example

import LED from "j5e/led";

const led = await new LED(12, {
  pwm: true
});
led.fade(512);

LED.fadeIn

fadeIn Fade an led in to full brightness (Requires ```pwm: true```)

led.fadeIn(time, callback);

Returns:

Parameters

Name
Type
Description
time
Number

Description:
Time in ms that a fade will take
Default: 1000

callback
function

Description:
A function to run when the fade is complete

Example

// Fade an LED to full brightness over half a second
import LED from "j5e/led";

const led = await new LED(12, {
  pwm: true
});
led.fadeIn(500);

LED.fadeOut

fadeOut Fade an led out until it is off (Requires ```pwm: true```)

led.fadeOut(time, callback);

Returns:

Parameters

Name
Type
Description
time
Number

Description:
Time in ms that a fade will take
Default: 1000

callback
function

Description:
A function to run when the fade is complete

Example

// Fade an LED out over half a second
import LED from "j5e/led";

const led = await new LED(12, {
  pwm: true
});
led.on();
led.fadeOut(500);

LED.pulse

Pulse the LED in and out in a loop with specified time using ```inOutSine``` easing (Requires ```pwm: true```)

led.pulse(time, callback);

Returns:

Parameters

Name
Type
Description
time
number

Description:
Time in ms that a fade in/out will elapse
Default: 1000

callback
function

Description:
A function to run each time the direction of pulse changes

Example

// Pulse an LED on a half second interval
import LED from "j5e/led";

const led = await new LED(12, {
  pwm: true
});
led.pulse(500);

LED.animate

Animate the LED by passing in a segment options object

led.animate(options);

Returns:

Parameters

Name
Type
Description
options
(required)
Object

Description:
(See animation)

Example

// Animate an LED using an animation segment options object
import LED from "j5e/led";

const led = await new LED(12, {
  pwm: true
});
led.animate({
  duration: 4000,
  cuePoints: [0,  0.33, 0.66, 1],
  keyFrames: [0, 0.75, 0.25, 1],
  loop: true,
  metronomic: true
});

LED.stop

stop Stop the led from blinking, pulsing, fading, or animating

led.stop();

Returns:

Example

Pulse an LED and then stop after five seconds
import {timer} from "j5e/fn";
import LED from "j5e/led";

const led = await new LED(12, {
  pwm: true
});
led.pulse(500);

// Stop pulsing after five seconds
timer.setTimeout(function() {
  led.stop();
}, 5000);