Skip to content
Rick Waldron edited this page Aug 5, 2013 · 28 revisions

The Pin class constructs objects that represent any one pin on the physical board.

Parameters

  • pin A Number or String address for the pin. If digital, use the number, if analog use the "A" prefixed string.
var digital = new five.Pin(13);

var analog = new five.Pin("A0");
  • options An object of property parameters.
Property Name Type Value(s) Description Required
pin Number, String Any Pin The Number or String address of the pin yes
type String "digital", "analog" For most cases, this can be omitted; the type will be inferred based on the pin address number. no
```js var digital = new five.Pin({ pin: 13 });

var analog = new five.Pin({ pin: "A0" });



### Shape

```js
{ 
  board: ...A reference to the board object the pin is on
  id: ...A user definable id value. Defaults to null
  pin: ...The pin address of the pin
  addr: ...The pin address of the pin
  type: ...The type of pin this is, either "digital" or "analog"
  value: ...The most recently reported value for this pin.
}

Usage Example

var five = require("johnny-five"),
    board = new five.Board();

board.on("ready", function() {
  var strobe, state;

  strobe = new five.Pin(13);

  this.loop(500, function() {
    strobe.write( state ^= 0x01 );
  });
});

API

  • query(callback(value)) Query the board for the current state of this pin, invoking callback with argument state when complete.
var pin = new five.Pin(13);

pin.query(function(state) {
  console.log(state);
});

An example pin state object looks like:

{ 
  supportedModes: [ 0, 1, 3, 4 ],
  mode: 0,
  value: 0,
  report: 1,
  analogChannel: 127 
}

See Modes for supportedModes mapping.

  • high() Set the pin HIGH.
var pin = new five.Pin(13);
// This will set pin 13 high (on)
pin.high();
  • low() Set the pin LOW.
var pin = new five.Pin(13);
// This will set pin 13 low (off)
pin.low();
  • write(value) Write a value to this pin.
var pin = new five.Pin(13);

pin.write(1);
  • read(callback(value)) Register a handler to be called whenever the board reports the value (digital or analog) of this pin.
var pin = new five.Pin(13);

pin.read(function(value) {
  console.log(value);
});

Events

Whenever a pin is set to INPUT, it will automattically emit the following events:

  • high The "high" event is emitted whenever the pin goes high.

  • low The "low" event is emitted whenever the pin goes low.

  • data The "data" event is emitted for every all data (firehose).

Static

Modes

Mode Value Constant
INPUT 0 Pin.INPUT
OUTPUT 1 Pin.OUTPUT
ANALOG 2 Pin.ANALOG
PWM 3 Pin.PWM
SERVO 4 Pin.SERVO

Methods

  • Pin.write(pin, value) Write a value to a pin.
// This will set pin 13 High
five.Pin.write(13, 1);
  • Pin.read(pin, value) Register a handler to be called whenever the board reports the value (digital or analog) of the specified pin.
five.Pin.read(13, function(value) {
  console.log(value);
});

Examples

Clone this wiki locally