Digital I/O

pinMode(pin[, mode])

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

  • mode <number> The pin mode INPUT (0) or OUTPUT (1)or INPUT_PULLUP (2)

    . Default: INPUT

Set the mode of the GPIO pin to INPUT or OUTPUTor INPUT_PULLUP. A RangeError will be thrown if pin does not support GPIO function. Use INPUT_PULLUPwhen you want to enable internal pull-up for this pin.

// Javascript example: Set pin 1 to OUTPUT HIGH.
pinMode(1, OUTPUT); // Set the pin 1 to OUTPUT.
digitalWrite(1, HIGH); // Set the pin 1 to HIGH.

digitalRead(pin)

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

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

Read the digital input from the GPIO INPUT pin. A RangeError will be thrown if pin does not support GPIO function.

// Javascript example: Read value from the pin 1.
pinMode(1, INPUT); // Set the pin 1 to GPIO INPUT
var value = digitalRead(1); // Read the digital input value from the pin 1.

digitalWrite(pin[, value])

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

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

Set the GPIO OUTPUT pin to HIGH or LOW. A RangeError will be thrown if pin does not support GPIO function.

// Javascript example: Set the pin 1 to HIGH.
pinMode(1, OUTPUT); // Set the pin 1 to GPIO OUTPUT
digitalWrite(1, HIGH); // Set the pin 1 to HIGH

digitalToggle(pin)

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

Set the GPIO OUTPUT pin to the reverse state of the currnet state. Set to HIGH if the current state is LOW. Set to LOW if the current state is HIGH. A RangeError will be thrown if pin does not support GPIO function.

// Javascript example: Set the pin 1 to HIGH and toggle (Set to LOW)
pinMode(1, OUTPUT); // Set the pin 1 to GPIO OUTPUT
digitalWrite(1, HIGH); // Set the pin 1 to HIGH
digitalToggle(1) // Toggle the pin 1, change the pin state to LOW from HIGH.

setWatch(callback, pin[, event[, debounce]])

  • callback <function> The function is called when the pin event is triggered.

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

  • event <number> set the event of the pin. There are three events, FALLING (0), RISING (1), and CHANGE (2). Default: FALLING.

  • debounce <number> debounce time in ms(milliseconds). Default: 0ms

  • Returns: <number> the ID of the watcher.

Run the callback function when the event is triggered on the pin. There are three events. The FALLING event is triggered when the pin state is changed from HIGH to LOW. The RISING (1) event is triggered when the pin state is changed from LOW to HIGH. The CHANGE 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 debounce time can be set when you can see the bouncing on the GPIO pin. A RangeError will be thrown if pin does not support GPIO function.

The pin mode is changed to INPUT when you call this function. So you don't need to set the mode of the pin to INPUT before calling this function.

// Javascript example: Print out 'click' string to the terminal when user press the button.
var pin = board.button_pins[0]; // Set the pin to the the on-board button
var id = setWatch(function () {
console.log("click"); // Print out the 'click' to the terminal.
}, pin, FALLING, 10); // Set falling event with 10ms debouncing time.

clearWatch(id)

  • id <number> The ID of the watcher which is the return value of the setWatch() fucntion.

Stop watching the event which is set by setWatch() function.

// Javascript example: Print out 'click' string to the terminal when user press the button.
var pin = board.button_pins[0]; // Set the pin to the the on-board button
var id = setWatch(function () {
console.log("click"); // Print out the 'click' to the terminal.
}, pin, FALLING, 10); // Set falling event with 10ms debouncing time.
clearWatch(id); // Stop watching the on-board pin event.