ADC, GPIO, PWM, UARTs, and more on the BeagleBone Black.
$ npm install bot-io
- LEDs - Light Emitting Diodes
- Buttons and Switches
- ADC - Analog to Digital Conversion
- GPIO - General Purpose Input Output
- PWM - Pulse Width Modulation
- UART - Serial Communication
Let's start off with something simple that doesn't require any hadrware other than the BeagleBone Black itself. The following example will blink onboard user LED0 at a frequency of 1Hz.
var bot = require('bot-io'),
led0 = new bot.Led(bot.Led.USR0);
led0.once('ready', function () {
// Blink at 1Hz. Cycle = 1000ms, on for 500ms, off for 500ms.
led0.blink();
});
The next example blinks all four onboard user LEDs at 100Hz. Every 250ms the blink delays are adjusted. The LEDs will alternate between glowing dimly and brightly.
var bot = require('bot-io'),
Led = bot.Led,
leds;
leds = [Led.USR0, Led.USR1, Led.USR2, Led.USR3].map(function (usrledName) {
return new Led(usrledName);
});
bot.once('ready', leds, function () {
var blinkLeds = function (delayOn, delayOff) {
leds.forEach(function (led) {
led.blink(delayOn, delayOff);
});
};
setInterval(function () {
// Blink at 100Hz. Cycle = 10ms, on for 1ms, off for 9ms.
blinkLeds(1, 9);
setTimeout(function () {
// Blink at 100Hz. Cycle = 10ms, on for 9ms, off for 1ms.
blinkLeds(9, 1);
}, 250);
}, 500);
});
Toggle the state of an LED every time a button is pressed.
var bot = require('bot-io'),
button = new bot.Button(bot.pins.p9_24),
led = new bot.Led(bot.pins.p9_26),
ledState = 0;
button.on('pressed', function () {
led.value(ledState ^= 1);
});
Fade an LED on and off once per second.
var bot = require('bot-io'),
led = new bot.Pwm(bot.pins.p9_42);
led.once('ready', function () {
var period = led.period(),
duty = 0,
delta = period / 50;
(function updateDuty() {
led.duty(duty);
duty += delta;
if (duty < 0 || duty > period) {
delta = -delta;
duty += delta;
}
setTimeout(updateDuty, 10);
}());
});
Determine the ambient light level with an analog ambient light sensor.
var bot = require('bot-io'),
ain = new bot.Ain(bot.pins.p9_36);
ain.once('ready', function () {
setInterval(function () {
console.log('value: ' + ain.value() + ', rawValue: ' + ain.rawValue());
}, 1000);
});
- Ain - Analog to Digital Conversion
- Button - Buttons and Switches
- Gpio - General Purpose Input Output
- Led - Light Emitting Diodes
- Pwm - Pulse Width Modulation
- Uart - Serial Communication
- once - One Time Listener
Tested with Debian Image 2014-09-03 and Node.js v0.10.25 on the BeagleBone Black rev. A5C.