GPIO

The gpio module supports GPIO pin abstraction. Use require('gpio') to access this module.

Class: GPIO

An instances of GPIO represents a GPIO pin.

new GPIO(pin[, mode])

  • pin <number> The pin number which can support GPIO function.

  • mode <number> The pin mode INPUT (0) or OUTPUT (1). Default: INPUT

Instances of the GPIO class can be created using the new keyword or by calling gpio.GPIO() as a function.

// Javascript example: Create the GPIO instance
var GPIO = require('gpio').GPIO;
var pin = new GPIO(0, OUTPUT);
‚Äč
// or, shortly
var pin = board.gpio(0, OUTPUT);

gpio.read()

  • Returns: <number> The return value is HIGH (1) or LOW (0)

Read the value from the GPIO pin.

// Javascript example: Read value from the pin 0.
var pin = board.gpio(0, INPUT);
var value = pin.read();

gpio.write(value)

  • value <number> The value could be HIGH (1) or LOW (0).

Writes a value to the GPIO pin.

// Javascript example: Write HIGH to the pin 0.
var pin = board.gpio(0, OUTPUT);
pin.write(HIGH);

gpio.toggle()

Toggles the output value of the GPIO pin.

// Javascript example: Write LOW to the pin 0 and toggling it.
var pin = board.gpio(0, OUTPUT);
pin.write(LOW); // Set to LOW
pin.toggle(); // HIGH
pin.toggle(); // LOW

gpio.low()

Set the GPIO pin to LOW.

var pin = board.gpio(0, OUTPUT);
pin.low();

gpio.high()

Set the GPIO pin to HIGH.

var pin = board.gpio(0, OUTPUT);
pin.high();

gpio.pin

  • <number>

Pin number of the GPIO object.

// Javascript example: Write LOW to the pin 0 and print the pin number.
var gpio0 = board.gpio(0, OUTPUT);
console.log(gpio0.pin) // Print pin number 0.

gpio.mode

  • <number>

Current mode of the GPIO pin. The value is INPUT (0) or OUTPUT (1).

// Javascript example: Write LOW to the pin 0 and print the mode.
var gpio0 = board.gpio(0, OUTPUT);
console.log(gpio0.mode) // Print pin number 0.