Skip to content

Latest commit

History

30 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

gpio

Talk to your Raspberry Pi's GPIO

Usage

This library is an npm package, just define "gpio" in your package.json dependencies or

npminstallgpio
Pin direction "out"

When you export a pin, the default direction is out. This allows you to set the pin value to either LOW or HIGH (3.3V) from your program.

vargpio=require("gpio");vargpio4=gpio.export(4);// returns a gpio pin instance and exports that pingpio4.set();// sets pin to highconsole.log(gpio4.value);// should log 1gpio4.set(0);// sets pin to low (can also call gpio4.reset()console.log(gpio4.value);// should log 0gpio4.unexport();// all done
Pin direction "in" and event binding

You can set the pin direction so that the pin value can be set externally, then read pin value from your program. This library comes with an observer pattern which allows you to watch for value changes and fire a callback.

vargpio=require("gpio");vargpio4=gpio.export(4,"in");// creates pin instance with direction "in"// bind to the "valueChange" eventgpio4.on("valueChange",function(value){// value will report either 1 or 0 (number) when the value changes});// bind multiple eventsvarprocessPin4=function(val){console.log(val);};gpio4.on("valueChange",processPin4);// unbind a particular callback from the "valueChange" eventgpio4.off("valueChange",processPin4);// unbind all callbacks from the "valueChange" eventgpio4.off("valueChange");// you can also manually change the direction anytime after instantiationvargpio17=gpio.export(17);gpio17.setDirection("in");gpio17.setDirection("out");

Example

Flashing Lights (with LED connected to GPIO22)
vargpio=require("gpio");vargpio22=gpio.export(22);setInterval(function(){gpio22.set();setTimeout(function(){gpio22.reset();},500);},1000);// 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 vargpio4=gpio.export(4);gpio22.on("valueChange",function(val){gpio4.set(1-val);// when gpio22 changes value, set gpio4 to the opposite value});
Controlling an RC car

Source code here: https://github.com/EnotionZ/node-rc

##Installation (Get node.js on your Raspberry Pi)

  1. Install packages:
sudo apt-get install git-core build-essential libssl-dev
  1. Grab node source
git clone https://github.com/joyent/node.git
git checkout v0.6.18-release (or current stable)
  1. Use armv6 architecture to compile
export CCFLAGS='-march=armv6'export CXXFLAGS='-march=armv6'
  1. Edit deps/v8/SConstruct, line 82
all': {'CCFLAGS': ['$DIALECTFLAGS', '$WARNINGFLAGS', '-march=armv6'],'CXXFLAGS': ['-fno-rtti', '-fno-exceptions', '-march=armv6'], },
  1. Edit same file, line 157, remove 'vfp3:on' and *'simulator:none'

  2. configure the build

./configure --openssl-libpath=/usr/lib/ssl
  1. Install!!
make
sudo make install

About

Talk to your Raspberry Pi's GPIO

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors