Module: j5e/light

Description

A module for measuring light levels using a photoresistor.

A photoresistor is a passive component that decreases resistance with respect to receiving luminosity (light) on the component's sensitive surface. The resistance of a photoresistor decreases with increase in incident l intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive detector circuits and light-activated and dark-activated switching circuits acting as a resistance semiconductor. In the dark, a photoresistor can have a resistance as high as several megaohms (MΩ), while in the light, a photoresistor can have a resistance as low as a few hundred ohms. If incident light on a photoresistor exceeds a certain frequency, photons absorbed by the semiconductor give bound electrons enough energy to jump into the conduction band. The resulting free electrons conduct electricity, thereby lowering resistance. The resistance range and sensitivity of a photoresistor can substantially differ among dissimilar devices. Moreover, unique photoresistors may react substantially differently to photons within certain wavelength bands.Read more on Wikipedia

In electronics, an analog-to-digital converter is a system that converts an analog signal, such as a sound picked up by a microphone or light entering a digital camera, into a digital signal. An ADC may also provide an isolated measurement such as an electronic device that converts an analog input voltage or current to a digital number representing the magnitude of the voltage or current. Typically the digital output is a two's complement binary number that is proportional to the input, but there are other possibilities.Read more on Wikipedia

Requires


Class: Light

The Light class allows for control of photoresistors

Constructor

new Light(io)

Parameters

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

Description:
Pin identifier or IO Options (See instantiation)

Example

// Use a photoresistor
import Light from "j5e/light";

const light = await new Light(12);

light.on("change", data => {
  trace(light.level);
});

Properties

Name
Type
Description
level
(read only)
number
Return the current value, scaled [0,1]
limit
Array.<number>
Limits the output range
threshold
number
The minimum amount of change required to emit a "change" event
interval
number
The interval between readings (in ms)
smoothing
number
The number of samples to take before finding the median
aref
number
The reference voltage
samples
number
The number of samples to take before finding the median
range
(read only)
Array.<number>
The input range of the sensor
raw
(read only)
number
Get the most recent raw ADC reading
median
(read only)
number
Get the most recent median ADC reading
resolution
(read only)
number
The maximum possible ADC reading
scaled
(read only)
number
Get the most recent scaled raw reading
value
(read only)
number

Events

  • event:data
  • event:change

Methods

Light.configure

Note: This method is not part of the public API, and is subject to change.

Configure a Light sensor

light.configure();

Returns: The instance on which the method was called

Example

import Light from "j5e/light";

const light = await new LED(14);
light.configure({
  interval: 50
});

Light.configure

Note: This method is not part of the public API, and is subject to change.

Configure a Sensor

light.configure(options);

Returns: The instance on which the method was called

Parameters

Name
Type
Description
options
(required)
object

Description:
Device configuration options

options.aref
number

Description:
Analog reference voltage
Default: 3.3

options.enabled
boolean

Description:
Wether the device is currently performing reads every ms
Default: true

options.interval
number

Description:
Interval between readings in millseconds
Default: 100

options.limit
Array.<number>

Description:
Limit the output range

options.range
Array.<number>

Description:
The input range of the sensor
Default: [0, N]

options.scale
Array.<number>

Description:
The output range for the sensor's value
Default: [0, N]

options.threshold
number

Description:
The minimum amount of change required to emit a "change" event
Default: 1

Example

// Passing in Cofiguration Options
import Sensor from "j5e/sensor";

const sensor = await new Sensor({
  pin: 12
});

sensor.configure({
  interval: 500
});

sensor.on("change", data => {
  trace(data);
});

Light.enable

Enable a disabled sensor.

light.enable();

Returns: instance

Example

import Sensor from "j5e/sensor";

const sensor = await new Sensor(12);

sensor.disable();

// Wait 5 seconds and then take readings
timer.setTimeout(function() {
  sensor.enable();
});

Light.disable

Disable an enabled sensor.

light.disable();

Returns: instance

Example

import Sensor from "j5e/sensor";

const sensor = await new Sensor(12);

// Take reading for 5 seconds and then stop
timer.setTimeout(function() {
  sensor.disable();
});

Light.read

Synchronous read of a sensor.

light.read();

Returns: sensor value

Example

import Sensor from "j5e/sensor";

const sensor = await new Sensor(12);

let myValue = sensor.read();

Light.scale

scale/scaleTo Set a value scaling range

light.scale(low, high, low, high);

Returns: instance

Parameters

Name
Type
Description
low
(required)
Number

Description:
Lowerbound

high
(required)
Number

Description:
Upperbound

low, high
Array

Description:
Lowerbound

Example

import Sensor from "j5e/sensor";

const sensor = await new Sensor(12);

// Scale all future values to 8-bit range
sensor.scale([0, 255]);

Light.scaleTo

scaleTo Scales value to integer representation

light.scaleTo(low, low, high);

Returns: The scaled value

Parameters

Name
Type
Description
low
(required)
Number

Description:
An array containing a lower and upper bound

low
(required)
Number

Description:
A number to use as a lower bound

high
(required)
Number

Description:
A number to use as an upper bound

Example

import Sensor from "j5e/sensor";

const sensor = await new Sensor(12);

// Scale the returned value to 8-bit range
sensor.scaleTo([0, 255]);

Light.fscaleTo

fscaleTo Scales value to single precision float representation

light.fscaleTo(low, low, high);

Returns: The scaled value

Parameters

Name
Type
Description
low
(required)
Number

Description:
An array containing a lower and upper bound

low
(required)
Number

Description:
A number to use as a lower bound

high
(required)
Number

Description:
A number to use as an upper bound

Example

import Sensor from "j5e/sensor";

const sensor = await new Sensor(12);

// Scale the returned value to float between 0 and 1
sensor.fscaleTo([0, 1]);

Light.within

Fire a callback when the value is within a certain range

light.within(range, unit, callback);

Parameters

Name
Type
Description
range
(required)
Array.<number>

Description:
The upper and lower ends of the range to watch

unit
(required)
string

Description:
The property to test

callback
(required)
function

Description:
A callback to run when the event is fired.


Light.on

Create an event listener

light.on(event, listener);

Parameters

Name
Type
Description
event
(required)
string

Description:
The name of the event to listen for

listener
(required)
function

Description:
A callback to run when the event is fired.


Light.removeListener

Remove an event listener

light.removeListener(event, listener);

Parameters

Name
Type
Description
event
(required)
string

Description:
The name of the event that we are removing a listener from

listener
(required)
function

Description:
The callback that we are removing


Light.emit

Note: This method is not part of the public API, and is subject to change.

Emit an event

light.emit(event);

Parameters

Name
Type
Description
event
(required)
string

Description:
The name of the event to emit


Light.once

Create an event listener that will only fire one time.

light.once(event, listener);

Parameters

Name
Type
Description
event
(required)
string

Description:
The name of the event to listen for

listener
(required)
function

Description:
A callback to run when the event is fired.


Light.configure

Configure a Sensor

light.configure(options);

Returns: The instance on which the method was called

Parameters

Name
Type
Description
options
(required)
object

Description:
Device configuration options

options.aref
number

Description:
Analog reference voltage
Default: 3.3

options.enabled
boolean

Description:
Wether the device is currently performing reads every ms
Default: true

options.interval
number

Description:
Interval between readings in millseconds
Default: 100

options.limit
Array.<number>

Description:
Limit the output range

options.range
Array.<number>

Description:
The input range of the sensor
Default: [0, N]

options.scale
Array.<number>

Description:
The output range for the sensor's value
Default: [0, N]

options.threshold
number

Description:
The minimum amount of change required to emit a "change" event
Default: 1

Example

// Passing in Cofiguration Options
import Sensor from "j5e/sensor";

const sensor = await new Sensor({
  pin: 12
});

sensor.configure({
  interval: 500
});

sensor.on("change", data => {
  trace(data);
});