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

For most cases, a proper Component class should be used instead of Pin.

Parameters

  • pin A Number or String address for the pin. If digital, use the number, if analog use the "A" prefixed string.

  • options An object of property parameters.

    PropertyTypeValue/DescriptionDefaultRequired
    idNumber, StringNon-specific. Any string or number value. Defaults to nullno
    pinNumber, StringAny Pin. The Number or String address of the pin, defaults to 0, or addr when that is suppliedno
    boardObject InstanceThe board instance your Pin is connected to. Only required when multiple boards are mounted in your project.First board mountedno
    typeString"digital", "analog". For most cases, this can be omitted; the type will be inferred based on the pin address number.Inferredno
    modeNumberSee Modes1 or 2 depending on typeno

Shape

Property NameDescriptionRead Only
idA user definable id value. Defaults to nullNo
pinThe pin address of the pinNo
typeThe type of pin this is, either "digital" or "analog"No
valueThe most recently reported value for this pin.No
modeThe mode (number) for the pin. See "Modes" belowNo

Component Initialization

Basic

// Digital
new five.Pin(13);

// Analog
new five.Pin("A0");
// Digital
new five.Pin({
  pin: 13
});

// Analog
new five.Pin({
  pin: "A0"
});

// Analog As Digital
new five.Pin({
  pin: 14,
  type: "digital"
});

Usage

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

board.on("ready", function() {
  var strobe = new five.Pin(13);
  var state = 0x00;

  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,
      state: 1,
      analogChannel: 127 
    }
    
    PropertyDescription
    supportedModesThis is a list of modes that are supported by the pin. These numbers correspond to the modes below.
    modeThis is the present mode of the pin (eg. A digital output pin would have mode: 1).
    valueThis is the present value of the pin (eg. An analog pin with half voltage reading would have value: 512).
    reportThis pin is presently reporting its value.
    stateFor output modes, the state is any value that has been previously written to the pin. For input modes, the state is the status of the pullup resistor.
    analogChannelThe numeric "index" of an analog (ADC) pin, or 127 if digital (eg. "A0" would have analogChannel: 0).
  • 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(error, 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(error, value) {
      console.log(value);
    });
    

Events

Whenever a pin is set to INPUT or ANALOG, it will automatically 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

ModeValueConstant
INPUT0Pin.INPUT
OUTPUT1Pin.OUTPUT
ANALOG2Pin.ANALOG
PWM3Pin.PWM
SERVO4Pin.SERVO

Methods

  • Pin.write(pin instance, value) Write a value to a pin.

    // This will set pin 13 High
    var pin = new five.Pin(13);
    
    five.Pin.write(pin, 1);
    
  • Pin.read(pin instance, callback) Register a handler to be called whenever the board reports the value (digital or analog) of the specified pin.

    var pin = new five.Pin(13);
    
    five.Pin.read(pin, function(error, value) {
      console.log(value);
    });
    

Examples

Hi! The Johnny-Five community is building new projects every day. We made this newsletter to tell you about what's new, what's good, and what's next for Open Source robotics. Join us in exploring what we can make together.

Fork me on GitHub