Skip to content

Instantly share code, notes, and snippets.

@sofroniewn
Last active February 28, 2016 01:28
Show Gist options
  • Save sofroniewn/12a39565833170c660cb to your computer and use it in GitHub Desktop.
Save sofroniewn/12a39565833170c660cb to your computer and use it in GitHub Desktop.
Using https://github.com/nebrius/raspi-gpio on a Raspberry Pi to control digital pin states at 2 kHz from node. In this example a switch was placed on pin 2 and an led was hooked up to pin 0. When the switch (yellow trace) was flipped, the led turned on (blue trace) within 500 us
console.log('Hello Pi')
var raspi = require('raspi')
var gpio = require('raspi-gpio')
var NanoTimer = require('nanotimer')
var fs = require('fs')
var now = require('performance-now')
var path = require('path')
var timer = new NanoTimer()
var interval = process.argv[2]
var repeats = process.argv[3]
var filename = path.basename(process.argv[1], '.js')
var ws = fs.createWriteStream('./logs/' + filename + '-' + interval + '-' + repeats +'.log' )
var iteration = 0
raspi.init(function() {
obj = {
'board': true,
'date': new Date(),
'time': now().toFixed(3)
}
console.log(JSON.stringify(obj))
// ws.write(JSON.stringify(obj))
var input = new gpio.DigitalInput(2)
var output = new gpio.DigitalOutput(0)
var curTime = 0
var prevTime = 0
var value = 0
timer.setInterval(function() {
curTime = now()
for (i=0; i<repeats; i++) {
value = input.read()
}
for (i=0; i<repeats; i++) {
output.write(value)
}
obj = {
'sw': value,
'it': iteration,
'ts': curTime.toFixed(3),
'df': (curTime - prevTime).toFixed(3)
}
prevTime = curTime
iteration++
// console.log(JSON.stringify(obj))
ws.write(JSON.stringify(obj) + '\n')
}, '', interval)
})
@sofroniewn
Copy link
Author

image
$sudo node switch-multi.js '500u' 20

@sofroniewn
Copy link
Author

image
$sudo node switch-multi.js '500u' 20

@sofroniewn
Copy link
Author

image
$sudo node switch-multi.js '500u' 20
20 pin read, 20 pin writes, and a block of JSON at 2 kHz controlling the switching on and off of an LED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment