The gpio
module supports GPIO pin abstraction. Use require('gpio')
to access this module.
An instances of GPIO
represents a GPIO pin.
pin
<number>
The pin number which can support GPIO function.
mode
<number>
The pin mode INPUT (0)
, OUTPUT (1)
, or INPUT_PULLUP (2)
Default: INPUT
INPUT_PULLUP
enable a chip internal pull up and INPUT_PULLDOWN
enables a chip internal pull down.
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 instancevar GPIO = require('gpio').GPIO;var pin = new GPIO(0, OUTPUT);// or, shortlyvar pin = board.gpio(0, OUTPUT);
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();
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);
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 LOWpin.toggle(); // HIGHpin.toggle(); // LOW
Set the GPIO pin to LOW
.
var pin = board.gpio(0, OUTPUT);pin.low();
Set the GPIO pin to HIGH
.
var pin = board.gpio(0, OUTPUT);pin.high();
<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.
<number>
Current mode of the GPIO pin. The value is INPUT (0)
, OUTPUT (1)
, or INPUT_PULLUP (2)
.
// 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.