GPIO

The GPIO module provides access the General Purpose Input Output pins of the hardware.

On Linux each pin has a logical number starting from 1. The logical number might be different from the physical pin number of the board. The mapping is available in the documentation of a given board.

  • On Tizen, the pin number is defined in this documentation.
  • On NuttX, the pin number is defined in the documentation of the target board. For more information, please check the following list: STM32F4-discovery

DIRECTION

  • IN Input pin.
  • OUT Output pin.

An enumeration which can be used to specify the direction of the pin.

MODE

  • NONE None.
  • PULLUP Pull-up (pin direction must be IN).
  • PULLDOWN Pull-down (pin direction must be IN).
  • FLOAT Float (pin direction must be OUT).
  • PUSHPULL Push-pull (pin direction must be OUT).
  • OPENDRAIN Open drain (pin direction must be OUT).

An enumeration which can be used to specify the mode of the pin. These options are only supported on NuttX.

EDGE

  • NONE None.
  • RISING Rising.
  • FALLING Falling.
  • BOTH Both.

An enumeration which can be used to specify the edge of the pin.

gpio.open(configuration, callback)

  • configuration {Object} Configuration for open GPIOPin.
    • pin {number} Pin number. Mandatory field.
    • direction {gpio.DIRECTION} Pin direction. Default: gpio.DIRECTION.OUT
    • mode {gpio.MODE} Pin mode. Default: gpio.MODE.NONE
    • edge {gpio.EDGE} Pin edge. Default: gpio.EDGE.NONE
  • callback {Function}
    • error {Error|null}
    • gpioPin {Object} An instance of GPIOPin.
  • Returns: {Object} An instance of GPIOPin.

Get GPIOPin object with configuration asynchronously.

The callback function will be called after opening is completed. The error argument is an Error object on failure or null otherwise.

Example

  1. var gpio = require('gpio');
  2. var gpio10 = gpio.open({
  3. pin: 10,
  4. direction: gpio.DIRECTION.OUT,
  5. mode: gpio.MODE.PUSHPULL,
  6. edge: gpio.EDGE.RISING
  7. }, function(err, pin) {
  8. if (err) {
  9. throw err;
  10. }
  11. });

gpio.openSync(configuration)

  • configuration {Object} Configuration for open GPIOPin.
    • pin {number} Pin number. Mandatory field.
    • direction {gpio.DIRECTION} Pin direction. Default: gpio.DIRECTION.OUT
    • mode {gpio.MODE} Pin mode. Default: gpio.MODE.NONE
    • edge {gpio.EDGE} Pin edge. Default: gpio.EDGE.NONE
  • Returns: {Object} An instance of GPIOPin.

Get GPIOPin object with configuration synchronously.

Example

  1. var gpio = require('gpio');
  2. var gpio10 = gpio.openSync({
  3. pin: 10,
  4. direction: gpio.DIRECTION.IN,
  5. mode: gpio.MODE.PULLUP
  6. });

Class: GPIOPin

This class represents an opened and configured GPIO pin. It allows getting and setting the status of the pin.

gpiopin.setDirectionSync(direction)

Set the direction of a GPIO pin.

Example

  1. gpio10.setDirectionSync(gpio.DIRECTION.OUT);
  2. gpio10.writeSync(1);
  3. gpio10.setDirectionSync(gpio.DIRECTION.IN);
  4. var value = gpio10.readSync();

gpiopin.write(value[, callback])

  • value {number|boolean}
  • callback {Function}
    • error {Error|null}

Asynchronously writes out a boolean value to a GPIO pin (a number value is converted to boolean first).

The optional callback function will be called after the write is completed. The error argument is an Error object on failure or null otherwise.

Example

  1. gpio10.write(1, function(err) {
  2. if (err) {
  3. throw err;
  4. }
  5. });

gpiopin.writeSync(value)

  • value {number|boolean}

Writes out a boolean value to a GPIO pin synchronously (a number value is converted to boolean first).

Example

  1. gpio10.writeSync(1);

gpiopin.read([callback])

  • callback {Function}
    • error {Error|null}
    • value {boolean}

Asynchronously reads a boolean value from a GPIO pin.

The optional callback function will be called after the read is completed. The error argument is an Error object on failure or null otherwise. The value argument contains the boolean value of the pin.

Example

  1. gpio10.read(function(err, value) {
  2. if (err) {
  3. throw err;
  4. }
  5. console.log('value:', value);
  6. });

gpiopin.readSync()

  • Returns: {boolean}

Returns the boolean value of a GPIO pin.

Example

  1. console.log('value:', gpio10.readSync());

gpiopin.close([callback])

  • callback {Function}
    • error {Error|null}

Asynchronously closes a GPIO pin.

The optional callback function will be called after the close is completed. The error argument is an Error object on failure or null otherwise.

Example

  1. gpio10.close(function(err) {
  2. if (err) {
  3. throw err;
  4. }
  5. // prints: gpio pin is closed
  6. console.log('gpio pin is closed');
  7. });

gpiopin.closeSync()

Synchronously closes a GPIO pin.

Example

  1. gpio10.closeSync();
  2. // prints: gpio pin is closed
  3. console.log('gpio pin is closed');