Kaluma
Search…
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) , OUTPUT (1) , INPUT_PULLUP (2) or INPUT_PULLDOWN (3) . Default: INPUT
Instances of the GPIO class can be created using the new keyword.
1
// Create the GPIO instance
2
const {GPIO} = require('gpio');
3
const pin = new GPIO(0, OUTPUT);
Copied!

gpio.read()

  • Returns: <number> The return value is HIGH (1) or LOW (0)
Read the value from the GPIO pin.
1
// Read value from the pin 0.
2
const {GPIO} = require('gpio');
3
const pin = new GPIO(0, INPUT);
4
const value = pin.read();
Copied!

gpio.write(value)

  • value <number> The value could be HIGH (1) or LOW (0).
Writes a value to the GPIO pin.
1
// Write HIGH to the pin 0.
2
const {GPIO} = require('gpio');
3
const pin = new GPIO(0, OUTPUT);
4
pin.write(HIGH);
Copied!

gpio.toggle()

Toggles the output value of the GPIO pin.
1
// Write LOW to the pin 0 and toggling it.
2
const {GPIO} = require('gpio');
3
const pin = new GPIO(0, OUTPUT);
4
pin.write(LOW); // Set to LOW
5
pin.toggle(); // HIGH
6
pin.toggle(); // LOW
Copied!

gpio.low()

Set the GPIO pin to LOW.
1
const {GPIO} = require('gpio');
2
const pin = new GPIO(0, OUTPUT);
3
pin.low();
Copied!

gpio.high()

Set the GPIO pin to HIGH.
1
const {GPIO} = require('gpio');
2
const pin = new GPIO(0, OUTPUT);
3
pin.high();
Copied!

gpio.irq(callback[, event])

  • callback <function>(pin, event) The function is called when the event is triggered on the pin. the pin and event are the arguments of the callback function.
  • event <number> set the event of the pin. There are three events,FALLING (4), RISING (8), and CHANGE (12). Default: CHANGE.
Add interrupt on the GPIO pin. Run the callback function when the event is triggered on the pin. There are three events for the interrupts. The FALLING (4) event is triggered when the pin state is changed from HIGH to LOW. The RISING (8) event is triggered when the pin state is changed from LOW to HIGH. The CHANGE (12) event is triggered when the pin state is changed to any states, which means the CHANGE event is the same as the FALLING + RISING events.
The interrupt does not support level triggering, you can use setWatch() function when you want to use level triggering events (LOW_LEVEL, HIGH_LEVEL).
1
// Set interrupt on the GPIO0
2
const {GPIO} = require('gpio');
3
// GPIO0 and GPIO1 are connected together
4
const pin0 = new GPIO(0, INPUT); // GPIO0 is input port which check the state change of GPIO1
5
const pin1 = new GPIO(1, OUTPUT); // GPIO 1 is output
6
pin1.low(); // Set LOW on the GPIO1
7
8
// Callback function for the GPIO0
9
function callback0(pin, mode) {
10
console.log("Event " + mode + " is triggered on the GPIO" + pin);
11
}
12
13
// Set interrupt callback function on GPIO01
14
pin0.irq(callback0, CHANGE);
15
16
pin1.high(); // Callback is called bcause GPIO0 is changed from LOW to HIGH
17
delay(1);
18
pin0.low(); // Callback is called bcause GPIO0 is changed from HIGH to LOW
Copied!

gpio.pin

  • <number>
Pin number of the GPIO object.
1
// Write LOW to the pin 0 and print the pin number.
2
const {GPIO} = require('gpio');
3
const gpio0 = new GPIO(0, OUTPUT);
4
console.log(gpio0.pin) // Print pin number 0.
Copied!

gpio.mode

  • <number>
Current mode of the GPIO pin. The value is INPUT (0) , OUTPUT (1) , INPUT_PULLUP (2) , or INPUT_PULLDOWN (3) .
1
// Write LOW to the pin 0 and print the mode.
2
const {GPIO} = require('gpio');
3
const gpio0 = new GPIO(0, OUTPUT);
4
console.log(gpio0.mode) // Print pin number 0.
Copied!
Last modified 7mo ago