gpio - talk to your Single Board Computer's gpio headers
Introduction
This plain JavaScript module is generic and only rely on system's sysfs.
Please consider other (more mature) gpio libraries out there which support better your hardware,
For instance, of you're looking for a reliable way to communicate with the Raspberry Pi using JavaScript, check out the wiring-pi JavaScript library. It provides direct bindings to the fully-featured Wiring Pi C library.
But if you want/need a generic lightweight module, this one can be used as fallback.
Support
Following hardware was reported to work (with some limitations or workarounds)
- ARTIK10 (inputs' pull down resistors are enabled by default)
- Raspberry Pi (use wiringpi's /usr/bin/gpio to change mode: gpio -g mode 11 up)
Installation
Get node.js for your SBC, If using Debian or deviates (Raspbian for RPi), you can simply run: sudo apt-get install nodejs
otherwise, install from node or compile it
Usage
This library is an npm package, just define "gpio" in your package.json dependencies or
npm install gpio
Note: you must be have proper privileges to access the GPIO headers (or run node as root).
Standard setup
var gpio = ; // Calling export with a pin number will export that header and return a gpio header instancevar gpio4 = gpio;
Header direction IN
If you plan to set the header voltage externally, use direction in
and read value from your program.
var gpio = ;var gpio4 = gpio;
API Methods
// sets pin to highgpio4;
// sets pin to low (can also call gpio4.reset())gpio4;
// Since setting a value happens asynchronously, this method also takes a// callback argument which will get fired after the value is setgpio4;gpio4;
// unexport program when donegpio4;
EventEmitter
This library uses node's EventEmitter which allows you to watch for value changes and fire a callback.
// bind to the "change" eventgpio4; // you can bind multiple eventsvar { console; };gpio4; // unbind a particular callback from the "change" eventgpio4; // unbind all callbacks from the "change" eventgpio4; // you can also manually change the direction anytime after instantiation gpio4;gpio4;
Example
Cycle voltage every half a second
var gpio = ;var gpio22 gpio4 intervalTimer; // Flashing lights if LED connected to GPIO22gpio22 = gpio; // Lets assume a different LED is hooked up to pin 4, the following code // will make that LED blink inversely with LED from pin 22 gpio4 = gpio; // reset the headers and unexport after 10 seconds
References
Demos on Raspberry Pi:
- Demo using LED: http://www.youtube.com/watch?v=2Juo-CJ6eu4
- Demo using RC car: http://www.youtube.com/watch?v=klQdX8-YVaI
- Source code here: https://github.com/EnotionZ/node-rc