The Board
class constructs objects that represent the physical board itself. All device objects depend on an initialized and ready board object.
Johnny-Five (sans IO Plugin) has been tested on, but is not limited to, the following boards:
- Arduino UNO
- Arduino Leonardo
- Arduino MEGA
- Arduino FIO
- Arduino Pro
- Arduino Pro Mini
- Arduino Nano
- TinyDuino
- BotBoarduino
- Dagu Micro Magician v2
- Red Back Spider
- TI Launchpad (with Energia Firmata)
For non-Arduino based projects, a number of IO Plugins are available:
- BeagleBone-IO
- Blend-Micro-IO
- Galileo-IO
- Bean-IO
- Nino-IO
- pcDuino-IO
- Pinoccio-IO
- Raspi-IO
- Spark-IO
- Imp-IO
- Remote-IO
- Board-IO
- Tessel-IO
- Playground-IO
See also:
Parameters
options Optional object of themselves optional parameters.
Property Type Value/Description Default Required id Number, String Any. User definable identification Generated no port String or object eg. /dev/ttyAM0
,COM1
,new SerialPort()
. Path or name of device port/COM or SerialPort objectDetected no repl Boolean true
,false
. Set tofalse
to disable REPLtrue
no debug Boolean true
,false
. Set tofalse
to disable debugging outputtrue
no timeout Number Timeout in milliseconds for the board to get connected 10000
no
Shape
Property Name | Description | Read Only |
---|---|---|
io | A reference to the IO protocol layer. | No |
id | A user definable id value. Defaults to a generated uid. | No |
repl | A reference to the active REPL. | No |
Component Initialization
To initialize control of a board, construct an instance of the Board
class.
When connecting to a USB serial device, such as an Arduino, you do not need to specify the device path or COM port, Johnny-Five will determine which to connect to and connect automatically.
new five.Board();
You may optionally specify the port by providing it as a property of the options object parameter.
// OSX
new five.Board({ port: "/dev/tty.usbmodem****" });
// Linux
new five.Board({ port: "/dev/ttyUSB*" });
// Windows
new five.Board({ port: "COM*" });
* Denotes system specific enumeration value (ie. a number)
You can specify a SerialPort
object by providing it as a property of the options object parameter:
var SerialPort = require("serialport").SerialPort;
var five = require("johnny-five");
var board = new five.Board({
port: new SerialPort("COM4", {
baudrate: 9600,
buffersize: 1
})
});
Usage
A basic, but complete example usage of the Board
constructor:
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
/*
Initialize pin 13, which
controls the built-in LED
*/
var led = new five.Led(13);
/*
Injecting object into the REPL
allow access while the program
is running.
Try these in the REPL:
led.on();
led.off();
led.blink();
(One at a time to see each action)
*/
this.repl.inject({
led: led
});
});
Sub-process Usage
When running Johnny-Five programs as a sub-process (eg. init.d, or npm scripts), be sure to shut the REPL off!
var five = require("johnny-five");
var board = new five.Board({
repl: false,
debug: false,
});
board.on("ready", function() {
// it's very quiet in here now!
});
API
Logging
info(class, message [, ...detail]) Displays an info message in the console (when
debug: true
, which is the default) and emits an event of the same name, as well as a generic message event.class
is the class that triggered the message, or that is being reported for;message
is any relevant message. This argument accept an arbitrary number of detail arguments. If the last detail argument is an object or array, it will be passed along as the value of adata
property of the event object.board.info("Board", "I got somethin' to say!", { foo: 1 }); board.info("Servo", "This pin doesn't have hardware PWM support, but will function correctly with software Servo support.");
warn(class, message [, ...detail]) Displays a warn message in the console (when
debug: true
, which is the default) and emits an event of the same name, as well as a generic message event.class
is the class that triggered the message, or that is being reported for;message
is any relevant message. This argument accept an arbitrary number of detail arguments. If the last detail argument is an object or array, it will be passed along as the value of adata
property of the event object.board.warn("Board", "Watch out!", { bar: 2 });
fail(class, message [, ...detail]) Displays a fail message in the console (when
debug: true
, which is the default) and emits an event of the same name, as well as a generic message event.class
is the class that triggered the message, or that is being reported for;message
is any relevant message. This argument accept an arbitrary number of detail arguments. If the last detail argument is an object or array, it will be passed along as the value of adata
property of the event object.board.fail("Led", "This pin is already in use!", { baz: NaN }); board.fail("Board", "This program attempted to do something that isn't possible");
See Events for specific log event details.
repl This is a reference to the active REPL automatically created by the
Board
class. This object has aninject
method that may be called as many times as desired:repl.inject(object) Inject objects or values, from the program, into the REPL session.
var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { // Initialize an LED directly in the REPL this.repl.inject({ led: new five.Led(13) }); }); /* From the terminal... $ node program.js 1423012815316 Device(s) /dev/cu.usbmodem1421 1423012818908 Connected /dev/cu.usbmodem1421 1423012818908 Repl Initialized >> led.on(); >> led.off(); */
pinMode(pin, mode) Set the
mode
of a specificpin
, one of INPUT, OUTPUT, ANALOG, PWM, SERVO. Mode constants are exposed via thePin
classMode Value Constant INPUT 0 Pin.INPUT OUTPUT 1 Pin.OUTPUT ANALOG 2 Pin.ANALOG PWM 3 Pin.PWM SERVO 4 Pin.SERVO // Set a pin to INPUT mode var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { // pin mode constants are available on the Pin class this.pinMode(13, five.Pin.INPUT); });
analogWrite(pin, value) Write an unsigned, 8-bit value (0-255) to an analog
pin
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { // Assuming an Led is attached to pin 9, // this will turn it on at full brightness // PWM is the mode used to write ANALOG // signals to a digital pin this.pinMode(9, five.Pin.PWM); this.analogWrite(9, 255); });
analogRead(pin, handler(voltage)) Register a handler to be called whenever the board reports the voltage value (0-1023) of the specified analog
pin
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { // Assuming a sensor is attached to pin "A1" this.pinMode(1, five.Pin.ANALOG); this.analogRead(1, function(voltage) { console.log(voltage); }); });
digitalWrite(pin, value) Write a digital value (0 or 1) to a digital
pin
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { // Assuming an Led is attached to pin 13, this will turn it on this.pinMode(13, five.Pin.OUTPUT); this.digitalWrite(13, 1); });
digitalRead(pin, handler(value)) Register a
handler
to be called whenever the board reports the value (0 or 1) of the specified digitalpin
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { // Assuming a button is attached to pin 9 this.pinMode(9, five.Pin.INPUT); this.digitalRead(9, function(value) { console.log(value); }); });
Note:
digitalRead
will only call its handler when the value of the pin changes.
- i2cConfig([milliseconds] | options) This must be called prior to any I2C reads or writes.
- May be called with a period in milliseconds to delay between read operations.
- May be called with an object containing options relevant to the platform for which the code is being written. For more information, see: IO-Plugins, i2cConfig(options)
i2cWrite(address, arrayOfBytes) Write an
arrayOfBytes
to the component ataddress
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { this.i2cConfig(); this.i2cWrite(0x01, [0x02, 0x03]); });
i2cWrite(address, register, arrayOfBytes) Write an
arrayOfBytes
to the component ataddress
, for a specificregister
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { this.i2cConfig(); this.i2cWrite(0x01, 0x00, [0x02, 0x03]); });
i2cWriteReg(address, register, byte) Write a single
byte
to the component ataddress
, for a specificregister
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { this.i2cConfig(); this.i2cWriteReg(0x01, 0x00, 0x7e); });
i2cRead(address, bytesToRead, handler(arrayOfBytes)) Repeatedly read the specified number of bytes (
bytesToRead
) and callhandler
with the results asarrayOfBytes
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { this.i2cConfig(); this.i2cRead(0x01, 0x02, 6, function(bytes) { console.log("Bytes read: ", bytes); }); });
i2cRead(address, register, bytesToRead, handler(arrayOfBytes)) Repeatedly read the specified number of bytes (
bytesToRead
), starting at a specific register, and callhandler
with the results asarrayOfBytes
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { this.i2cConfig(); this.i2cRead(0x01, 0x00, 6, function(bytes) { console.log("Bytes read: ", bytes); }); });
i2cReadOnce(address, register, bytesToRead, handler(arrayOfBytes)) Read the specified number of bytes (
bytesToRead
), starting at a specific register, and callhandler
with the results asarrayOfBytes
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { this.i2cConfig(); this.i2cReadOnce(0x01, 0x02, 6, function(bytes) { console.log("Bytes read: ", bytes); console.log("Done!"); }); });
i2cReadOnce(address, bytesToRead, handler) Read the specified number of bytes (
bytesToRead
) and callhandler
with the results asarrayOfBytes
.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { this.i2cConfig(); this.i2cRead(0x01, 6, function(bytes) { console.log("Bytes read: ", bytes); console.log("Done!"); }); });
servoWrite(pin, angle) Write an angle in degrees from 0-180 to a servo.
var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { this.pinMode(9, five.Pin.SERVO); this.servoWrite(9, 90); });
shiftOut(dataPin, clockPin, isBigEndian, value) Write a byte to
dataPin
, followed by toggling theclockPin
. Understanding Big and Little Endian Byte Orderwait(milliseconds, handler()) Register a handler to be called once in another execution turn and after
milliseconds
has elapsed.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { // Assuming an Led is attached to pin 13 this.pinMode(13, this.MODES.OUTPUT); // Turn it on... this.digitalWrite(13, 1); this.wait(1000, function() { // Turn it off... this.digitalWrite(13, 0); }); });
loop(milliseconds, handler()) Register a handler to be called repeatedly, in another execution turn, every
milliseconds
period.handler
recieves one argument which is a function that will cancel the loop if called.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { var state = 0; this.pinMode(13, this.MODES.OUTPUT); this.loop(500, () => { this.digitalWrite(13, (state ^= 1)); }); });
Events
connect This event will emit once the program has "connected" to the board. This may be immediate, or after some amount of time, but is always asynchronous. For on-board execution,
connect
should emit as soon as possible, but asynchronously.ready This event will emit after the connect event and only when the
Board
instance object has completed any hardware initialization that must take place before the program can operate. This process is asynchronous, and completion is signified to the program via a "ready" event. For on-board execution,ready
should emit afterconnect
.exit This event is emitted synchronously on
SIGINT
. Use this handler to do any necessary cleanup before your program is "disconnected" from the board.close This event is emmited when the device does not respond. Can be used to detect if board gets disconnected.
info This event will emit for
board.info(class, message [, ...])
board.on("info", function(event) { /* Event { type: "info"|"warn"|"fail", timestamp: Time of event in milliseconds, class: name of relevant component class, message: message [+ ...detail] } */ console.log("%s sent an 'info' message: %s", event.class, event.message); });
warn This event will emit for
board.warn(class, message [, ...])
board.on("warn", function(event) { /* Event { type: "info"|"warn"|"fail", timestamp: Time of event in milliseconds, class: name of relevant component class, message: message [+ ...detail] } */ console.log("%s sent a 'warn' message: %s", event.class, event.message); });
fail This event will emit for
board.fail(class, message [, ...])
board.on("fail", function(event) { /* Event { type: "info"|"warn"|"fail", timestamp: Time of event in milliseconds, class: name of relevant component class, message: message [+ ...detail] } */ console.log("%s sent a 'fail' message: %s", event.class, event.message); });
message This event will emit for any logging message:
info
,warn
orfail
.board.on("message", function(event) { /* Event { type: "info"|"warn"|"fail", timestamp: Time of event in milliseconds, class: name of relevant component class, message: message [+ ...detail] } */ console.log("Received a %s message, from %s, reporting: %s", event.type, event.class, event.message); });
Examples
- Board - Basic Initialization
- Board - Specify port
- Board - Cleanup in 'exit' event
- Board - Multiple in one program
- REPL
- Board - Specify Sampling Interval
- Pin
- Custom Data Properties
- LED
- LED - PCA9685
- LED - Tessel Servo Module
- LED - Blink
- LED - Pulse
- LED - Pulse with animation
- LED - Fade
- LED - Fade callback
- LED - Fade with animation
- LED - Demo sequence
- LED - Slider
- LEDs - An array of LEDs
- LEDs - Controlling an array of LEDs
- LED - RGB (Common Anode)
- LED - RGB (Common Anode) PCA9685
- LED - RGB Intensity
- LED - Rainbow
- LED - Rainbow BlinkM
- LED - Digital Clock
- LED - Digital Clock, HT16K33
- LED - Digital Clock, Dual Displays
- LED - Matrix
- LED - Matrix Demo
- LED - Matrix HT16K33
- LED - Matrix HT16K33 16x8
- LED - Draw Matrix Characters Demo
- LED - Enumerate Matrix Characters & Symbols
- Servo
- Servo - PCA9685
- Servo - Tessel Servo Module
- Servo - Continuous
- Servo - Multi-Turn
- Servo - Slider control
- Servo - Prompt
- Servo - Drive
- Servos - An array of servos
- GPS - Default GPS
- GPS - Sparkfun GP-20U7
- GPS - Hardware Serial
- GPS - Adafruit Ultimate GPS Breakout
- Servo - Animation
- Servo - Leg Animation
- Color - EVShield EV3 (Code)
- Color - EVShield EV3 (Raw)
- Color - EVShield NXT (Code)
- Color - ISL29125
- Motor
- Motor - Directional
- Motor - Brake
- Motor - Current
- Motor - Enable Pin
- Motor - Sparkfun Dual H-bridge Edison Block
- Motor - H-Bridge
- Motor - Sparkfun TB6612FNG
- Motors - Dual H-Bridge
- Motor - PCA9685
- Motor - GROVE_I2C_MOTOR_DRIVER
- Motor - l298 Breakout
- Motor - LUDUS
- Motor - 3 pin
- Motor - EVShield EV3
- Motor - EVShield NXT
- Motor - Adafruit DRV8871 DC Motor Driver Breakout
- Motor - Pololu VNH5019 Dual Motor Driver Breakout
- Stepper - Driver
- Stepper - Four Wire
- Stepper - Sweep
- ESC - Keypress controlled ESCs
- ESC - PCA9685
- ESC - Bidirectional
- Button
- Buttons - Collection w/ AT42QT1070
- Button - Bumper
- Button - Options
- Button - Pullup
- Toggle Switch
- Button - EVShield EV3
- Button - EVShield NXT
- Switch - Magnetic Door
- Switch - Tilt SW-200D
- Switch
- Keypad - VKEY
- Keypad - Waveshare AD
- Touchpad - MPR121
- Touchpad - MPR121_SHIELD
- Touchpad - MPR121_KEYPAD
- Touchpad - MPR121, Sensitivity
- Touchpad - MPR121QR2_SHIELD
- Touchpad - Grove QTouch
- Keypad - 3x4 I2C Nano Backpack
- Keypad - 4x4 I2C Nano Backpack
- Relay
- Relay On Analog Pin
- Relay - Collection
- Shift Register
- Shift Register - Seven Segment controller
- Shift Register - Common Anode Seven Segment controller
- Shift Register - Seven segments, Chained
- Shift Register - Common Anode Seven segments, Chained
- IR Motion
- IR Proximity
- IR Reflectance
- IR Reflectance Array
- Proximity
- Proximity - GP2Y0A710K0F
- Proximity - SRF10
- Proximity - MB1000
- Proximity - MB1010
- Proximity - MB1003
- Proximity - MB1230
- Proximity - HC-SR04
- Proximity - HC-SR04 (Analog)
- Proximity - HC-SR04 I2C Backpack
- Proximity - LIDAR-Lite
- Proximity - EVShield EV3 (IR)
- Proximity - EVShield EV3 (Ultrasonic)
- Proximity - EVShield EV3 (IR)
- Proximity - EVShield EV3 (Ultrasonic)
- Motion
- Motion - GP2Y0D805Z0F
- Motion - GP2Y0D810Z0F
- Motion - GP2Y0D810Z0F
- Motion - GP2Y0A60SZLF
- Joystick
- Joystick - Esplora
- Joystick - Sparkfun Shield
- Joystick - Pan + Tilt control
- LCD
- LCD - Tessel 2 16x2
- LCD - Enumerate characters
- LCD - Runner 20x4
- LCD - Runner 16x2
- LCD - I2C
- LCD - I2C PCF8574
- LCD - I2C Runner
- Tessel 2 + Grove - RGB LCD Display
- Grove - RGB LCD Color Previewer
- Tessel 2 + Grove - RGB LCD Color Previewer
- Compass / Magnetometer
- Compass - HMC6352
- Compass - MAG3110
- Compass - MAG3110 on Tessel 2
- Compass - HMC5883L
- Compass - Logger
- Compass - Find north
- Piezo
- IMU - LSM303C
- IMU - MPU6050
- IMU - BNO055
- IMU - BNO055 (Orientation)
- Multi - MPL115A2
- Multi - MPL3115A2
- Multi - BME280
- Multi - BMP180
- Multi - BMP085
- Multi - HTU21D
- Multi - SHT31D
- Multi - HIH6130
- Multi - DHT11_I2C_NANO_BACKPACK
- Multi - DHT21_I2C_NANO_BACKPACK
- Multi - DHT22_I2C_NANO_BACKPACK
- Multi - TH02
- Multi - SI7020
- Multi - SI7021
- Multi - MS5611
- Sensor
- Sensor - Slide potentiometer
- Sensor - Force sensitive resistor
- Sensor - Microphone
- Sensor - Digital Microwave
- Sensor - Photoresistor
- Sensor - Potentiometer
- Sensor - Flex sensor
- Accelerometer
- Accelerometer - LIS3DH
- Accelerometer - MMA8452
- Accelerometer - ADXL345
- Accelerometer - ADXL335
- Accelerometer - MMA7361
- Accelerometer - MPU6050
- Accelerometer - Pan + Tilt
- Gyro
- Gyro - Analog LPR5150AL
- Gyro - I2C MPU6050
- Thermometer - TMP36
- Thermometer - TMP102
- Thermometer - LM35
- Thermometer - LM335
- Thermometer - DS18B20
- Thermometer - Dual DS18B20
- Thermometer - MAX31850
- Thermometer - MPU6050
- Thermometer - HTU21D
- Thermometer - SHT31D
- Thermometer - HIH6130
- Thermometer - MCP9808
- Barometer - MPL115A2
- Barometer - MPL3115A2
- Altimeter - MPL3115A2
- Thermometer - MPL3115A2
- Thermometer - MPL115A2
- Barometer - BMP180
- Barometer - BMP085
- Barometer - MS5611
- Hygrometer - HTU21D
- Hygrometer - SI7021
- Hygrometer - SHT31D
- Hygrometer - HIH6130
- Thermometer - BMP180
- Thermometer - BMP085
- Thermometer - SI7020
- Thermometer - SI7021
- Thermometer - MS5611
- Altimeter - MS5611
- Altimeter - BMP180
- Altimeter - BMP085
- Thermometer - TH02
- Hygrometer - TH02
- Thermometer - DHT11_I2C_NANO_BACKPACK
- Hygrometer - DHT11_I2C_NANO_BACKPACK
- Thermometer - DHT21_I2C_NANO_BACKPACK
- Hygrometer - DHT21_I2C_NANO_BACKPACK
- Thermometer - DHT22_I2C_NANO_BACKPACK
- Hygrometer - DHT22_I2C_NANO_BACKPACK
- Expander - MCP23017
- Expander - MCP23008
- Expander - LIS3DH
- Expander - PCF8574
- Expander - PCF8575
- Expander - PCA9685
- Expander - PCF8591
- Expander - 74HC595
- Expander - MUXSHIELD2, Digital Input and Output
- Expander - MUXSHIELD2, Analog Sensors
- Expander - CD74HC4067, 16 Channel Analog Input Breakout
- Photon Weather Shield: Moisture
- Light - EVShield EV3 (Ambient)
- Light - EVShield NXT (Ambient)
- Light - EVShield EV3 (Reflected)
- Light - EVShield NXT (Reflected)
- Light - BH1750
- Light - TSL2561
- Intel Edison + Grove - Accelerometer (ADXL345)
- Intel Edison + Grove - Accelerometer (MMA7660)
- Intel Edison + Grove - Barometer (BMP180)
- Intel Edison + Grove - Humidity & Temperature (TH02)
- Intel Edison + Grove - Compass (HMC588L)
- Intel Edison + Grove - Flame Sensor
- Intel Edison + Grove - LED
- Intel Edison + Grove - Light Sensor (TSL2561)
- Intel Edison + Grove - Button
- Intel Edison + Grove - Moisture Sensor
- Intel Edison + Grove - Gas (MQ2)
- Intel Edison + Grove - Air quality sensor
- Intel Edison + Grove - Joystick
- Intel Edison + Grove - Rotary Potentiometer
- Intel Edison + Grove - RGB LCD
- Intel Edison + Grove - RGB LCD Color Previewer
- Intel Edison + Grove - RGB LCD temperature display
- Intel Edison + Grove - Relay
- Intel Edison + Grove - Servo
- Intel Edison + Grove - Touch
- Intel Edison + Grove - Q Touch
- Intel Edison + Grove - I2C Motor Driver
- Grove - LED
- Grove - Button
- Grove - Touch
- Grove - Joystick
- Grove - Rotary Potentiometer
- Grove - RGB LCD
- Grove - RGB LCD temperature display
- Grove - Servo
- Grove - Motor (I2C Driver)
- Micro Magician V2 - Accelerometer
- Micro Magician V2 - Motor
- Micro Magician V2 - Servo
- TinkerKit - Accelerometer
- TinkerKit - Blink
- TinkerKit - Button
- TinkerKit - Combo
- TinkerKit - Continuous servo
- TinkerKit - Gyro
- TinkerKit - Joystick
- TinkerKit - Linear potentiometer
- TinkerKit - Rotary potentiometer
- TinkerKit - Temperature
- TinkerKit - Tilt
- TinkerKit - Touch
- Wii Nunchuck
- Wii Classic Controller
- Line Follower
- Motobot
- Navigator
- Nodebot
- Whisker
- Phoenix Hexapod
- Bug
- Lynxmotion Biped BRAT
- Robotic Claw
- Kinect Robotic Arm Controller
- Laser Trip Wire
- Radar
- Example plugin
- Led Blink on Electric Imp
- Led Blink on Spark Core
- Led Blink on pcDuino3
- Led Blink on Intel Galileo Gen 2
- Led Blink on Raspberry Pi
- Led Blink on Intel Edison Arduino Board
- Led Blink on Intel Edison Mini Board