Exploring BB-Air GPIO

From Neuromeka Wiki
Jump to: navigation, search
BB-Air


GPIO control

You've seen in the previous tutorials how to program General Purpose Input/Output (GPIO) pins. BB-Air provides a variety of programmable pins for:

  • Digital Input (GPIO) – sensing a DC signal level at low (GND) or high (Vcc)
  • Digital Output (GPIO) – driving a DC level (GND or Vcc) that can be controlled dynamically


We'll begin by doing it manually through the Linux command line, and then compare that to doing it programmatically using:

  • node JS


1. Request control of the desired GPIO pin.

  • sysfs responds by creating a GPIO-specific directory (in this case, gpio69).
echo 69 > /sys/class/gpio/export 


2. Configure the GPIO pin to be an output.

  • You're just writing the string "out" to a file named direction.
echo out > /sys/class/gpio/gpio69/direction


3. Set the output level by writing the value file contents to "1":

echo 1 > /sys/class/gpio/gpio69/value
  • or "0":
echo 0 > /sys/class/gpio/gpio69/value


4. Using nodeJS

  • install the onoff module
npm install onoff
var Gpio = require('onoff').Gpio;
var exec = require('child_process').exec;
var gpioIN = new Gpio(68, 'in', 'both');
var gpioOUT = new Gpio(69, 'out');

setTimeout(function() {
  exec('echo 1 > /sys/class/gpio/gpio69/value');
}, 2000);

gpioIN.watch(function(err,value){
  if(err){
    throw err;
  }
  console.log('gpio68: ' + value);
});