The Sensor
class constructs objects that represent a single analog sensor component attached to the physical board. This class is intentionally generic and will work well with many types of sensors, including (but not limited to):
- Linear Potentiometer
- Rotary Potentiometer
- Flex Sensitive Resistor
- Pressure Sensitive Resistor
- Force Sensitive Resistor
- Hall Sensor
- Tilt Sensor
- Photoresistor/Light Dependent Resistor
...And more, see examples
Parameters
pin A Number or String address for the Sensor pin (analog).
options An object of property parameters.
Property Type Value/Description Default Required pin Number, String Analog Pin. The Number or String address of the pin the sensor is attached to, ie. "A0"
or0
mean the same thing.yes freq Number Milliseconds. The frequency in ms of data events. 25ms no threshold Number Any. The change threshold (+/- value). 1 no enabled boolean Whether to start emitting events right away (>= v0.9.12) true
no options (experimental) These options can be used with the
Sensor
class, but are considered experimental.Property Type Value/Description Default Required type String "digital", "analog". Specify that this is a sensor attached to a digital pin. Allows using a digital pin as the value of the pin
optionno
Shape
Property Name | Description | Read Only |
---|---|---|
id | A user definable id value. Defaults to a generated uid | No |
pin | The pin address that the Sensor is attached to | No |
threshold | The change threshold (+/- value). Defaults to 1 | No |
boolean | ADC value scaled to a boolean. | Yes |
raw | ADC value (0-1023). | Yes |
analog | ADC reading scaled to 8 bit values (0-255). | Yes |
constrained | ADC reading constrained to 8 bit values (0-255). | Yes |
value | ADC reading, scaled. | Yes |
freq | The rate in milliseconds to emit the data event. Disables the event if set to null . (>= v0.9.12) | No |
Component Initialization
Analog Sensor (Most common case)
new five.Sensor("A0");
Digital Sensor (Less common, but supported experimentally)
new five.Sensor({
pin: 2,
type: "digital"
});
// Can also be written as:
new five.Sensor.Digital(2);
Common Options
// - attached to pin "A0"
// - emits data events every 250ms
// - emits change events when the ADC value has changed by +5/-5
//
var temp = new five.Sensor({
pin: "A0",
freq: 250,
threshold: 5
});
Usage
Analog Sensor
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var sensor = new five.Sensor("A0");
// Scale the sensor's data from 0-1023 to 0-10 and log changes
sensor.on("change", function() {
console.log(this.scaleTo(0, 10));
});
});
Digital Sensor
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var sensor = new five.Sensor.Digital(2);
sensor.on("change", function() {
console.log(this.value);
});
});
If you prefer arrow functions...
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", () => {
var sensor = new five.Sensor("A0");
// Scale the sensor's data from 0-1023 to 0-10 and log changes
sensor.on("change", () => {
console.log(sensor.scaleTo(0, 10));
});
});
Digital Sensor
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", () => {
var sensor = new five.Sensor.Digital(2);
sensor.on("change", () => {
console.log(sensor.value);
});
});
API
scaleTo(low, high) (integer) Return the sensor's present value, scaled to a new value within the specified low/high range.
fscaleTo(low, high) (float) Return the sensor's present value, scaled to a new value within the specified low/high range.
var sensor = new five.Sensor("A0"); sensor.on("change", function() { // this.value will reflect a scaling from 0-1023 to 0-180 console.log(this.scaleTo(0, 180)); // integer console.log(this.fscaleTo(0, 180)); // float });
var sensor = new five.Sensor("A0"); sensor.on("change", () => { // this.value will reflect a scaling from 0-1023 to 0-180 console.log(sensor.scaleTo(0, 180)); // integer console.log(sensor.fscaleTo(0, 180)); // float });
scaleTo([low, high]) (integer) Return the sensor's present value, scaled to a new value within the specified low/high range.
fscaleTo([low, high]) (float) Return the sensor's present value, scaled to a new value within the specified low/high range.
var sensor = new five.Sensor("A0"); sensor.on("change", function() { // this.value will reflect a scaling from 0-1023 to 0-180 console.log(this.scaleTo([0, 180])); // integer console.log(this.fscaleTo([0, 180])); // float });
var sensor = new five.Sensor("A0"); sensor.on("change", () => { // this.value will reflect a scaling from 0-1023 to 0-180 console.log(sensor.scaleTo([0, 180])); // integer console.log(sensor.fscaleTo([0, 180])); // float });
scale(low, high) Scale the sensor's value to a new value within a specified range.
scale(...)
is deprecated, usescaleTo(...)
scale([low, high]) Same as
scale(low, high)
.scale(...)
is deprecated, usescaleTo(...)
booleanAt(barrier) Set a midpoint barrier value used to calculate returned value of the .boolean property. The barrier is based on the scaled value, not the raw value. Defaults to 50% (512 when unscaled).
var sensor = new five.Sensor("A0"); // ADC readings less than 100 will result // in the value of the `boolean` property being false. // // ADC readings greater than 100 will result // in the value of the `boolean` property being true. // sensor.booleanAt(100);
within(range, handler) When value is within the provided range, execute callback.
var sensor = new five.Sensor("A0"); sensor.within([ 100, 200 ], function() { // This is called when the sensor's value property falls within 100-200 });
enable() Start emitting "data" and "change" events, at a rate determined by the user defined
freq
in milliseconds. No-op if Sensor is already enabled. (>= v0.9.12)disable() Stop emitting "data" and "change" events. No-op if Sensor is already disabled. (>= v0.9.12)
Events
- change The "change" event is emitted whenever the value of the sensor changes more than the threshold value allows.
- data The "data" event is fired as frequently as the user defined
freq
will allow in milliseconds. ("data" replaced the deprecated "read" event)
Collection
Sensor
supports a Sensors
collection class, which allows multiple Sensor
instances to be controlled via a single instance object. Events emitted by instances of the Sensor
class are forwarded through instances of the Sensors
class. The handler receives the instance that emitted the event as the first parameter.
new five.Sensors([ 2, 3, 4, 5 ]);
new five.Sensors([ { pin: 2 }, { pin: 3 }, { pin: 4 }, { pin: 5 } ]);
new five.Sensors([ sensor1, sensor2, sensor3 ]);
Examples
- Custom Data Properties
- LED - Slider
- LEDs - Controlling an array of LEDs
- Servo - Slider control
- Motor - Current
- Motor - Sparkfun TB6612FNG
- ESC - PCA9685
- ESC - Bidirectional
- Touchpad - Grove QTouch
- Multi - MPL3115A2
- Multi - HTU21D
- Multi - SHT31D
- Sensor
- Sensor - Slide potentiometer
- Sensor - Force sensitive resistor
- Sensor - Microphone
- Sensor - Digital Microwave
- Sensor - Photoresistor
- Sensor - Potentiometer
- Sensor - Flex sensor
- Thermometer - TMP36
- Thermometer - TMP102
- Thermometer - LM35
- Thermometer - LM335
- Thermometer - DS18B20
- Thermometer - Dual DS18B20
- Thermometer - MPU6050
- Thermometer - HTU21D
- Thermometer - SHT31D
- Barometer - MPL3115A2
- Altimeter - MPL3115A2
- Thermometer - MPL3115A2
- Thermometer - MPL115A2
- Hygrometer - HTU21D
- Hygrometer - SHT31D
- Expander - LIS3DH
- Expander - PCF8591
- Expander - MUXSHIELD2, Digital Input and Output
- Expander - MUXSHIELD2, Analog Sensors
- Expander - CD74HC4067, 16 Channel Analog Input Breakout
- Photon Weather Shield: Moisture
- Intel Edison + Grove - Barometer (BMP180)
- Intel Edison + Grove - Humidity & Temperature (TH02)
- Intel Edison + Grove - Flame Sensor
- Intel Edison + Grove - Light Sensor (TSL2561)
- Intel Edison + Grove - Moisture Sensor
- Intel Edison + Grove - Gas (MQ2)
- Intel Edison + Grove - Air quality sensor
- Intel Edison + Grove - Rotary Potentiometer
- Intel Edison + Grove - RGB LCD
- Intel Edison + Grove - RGB LCD temperature display
- Intel Edison + Grove - Servo
- Intel Edison + Grove - Q Touch
- Grove - Rotary Potentiometer
- Grove - RGB LCD
- Grove - RGB LCD temperature display
- Grove - Servo
- TinkerKit - Combo
- TinkerKit - Continuous servo
- TinkerKit - Linear potentiometer
- TinkerKit - Rotary potentiometer
- TinkerKit - Tilt
- Whisker
- Phoenix Hexapod
- Laser Trip Wire