Raspberry Pi A+ NODE.JS GPIO

node.jsでGPIOを使ってみようと思います。
GPIO20を入力、GPIO21を出力にします。入力には、スイッチを、出力には、LEDを接続しました。
GPIO21 → 100Ω → LED1 → 3.3V

GPIO20 → 33kΩ → 3.3V
→ SW1  → GND

Jpeg

GPIOの制御ライブラリには、onoffを使用しました。
まずは、ライブラリのインストールを行います。

npm install onoff
npm install

以下サンプルソフトです。
########################################
// button is attaced to pin 20, led to 21
var GPIO = require(‘onoff’).Gpio,
led = new GPIO(21, ‘out’),
button = new GPIO(20, ‘in’, ‘both’);

// define the callback function
function light(err, state)
{
// check the state of the button
// 0 == pressed, 1 == not pressed
if(state == 0)
{
// turn LED on
led.writeSync(0);
}
else
{
// turn LED off
led.writeSync(1);
}
}

// pass the callback function to the
// as the first argument to watch()
button.watch(light);
########################################

スイッチを押すとLEDがつくソフトです。ボタンに従ってLEDが点灯、消灯することができました。

© 2014 Spineedge Corporation.