Installation

Step 1 - Install the pigpio C library

Raspbian Jessie 2016-05-10 or newer comes with the pigpio C library
pre-installed so it need not be manually installed.

Raspbian Jessie Lite 2016-05-10 or newer does not come with the pigpio C
library pre-installed so it must be manually installed with the following
commands:

sudo apt-get update
sudo apt-get install pigpio

The pigpio C library contains a number of utilities. One of these utilities
is pigpiod which launches the pigpio C library as a daemon. This utility
should not be used as the pigpio Node.js package uses the C library directly.

Installation instructions for the pigpio C library on versions of Raspbian
prior to 2016-05-10 can be found
here.

Step 2 - Install the pigpio Node.js package

npm install pigpio

Usage

Assume there's an LED connected to GPIO17 (pin 11) and a momentary push button
connected to GPIO4 (pin 7).

Pulse an LED with PWM

Use PWM to pulse the LED connected to GPIO17 from fully off to fully on
continuously.

var Gpio =require('pigpio').Gpio,

led =newGpio(17,{mode:Gpio.OUTPUT}),

dutyCycle =0;

setInterval(function(){

led.pwmWrite(dutyCycle);

dutyCycle +=5;

if(dutyCycle >255){

dutyCycle =0;

}

},20);

Buttons and interrupt handling

Turn the LED connected to GPIO17 on when the momentary push button connected to
GPIO4 is pressed. Turn the LED off when the button is released.

var Gpio =require('pigpio').Gpio,

button =newGpio(4,{

mode:Gpio.INPUT,

pullUpDown:Gpio.PUD_DOWN,

edge:Gpio.EITHER_EDGE

}),

led =newGpio(17,{mode:Gpio.OUTPUT});

button.on('interrupt',function(level){

led.digitalWrite(level);

});

Servo control

Continuously move a servo connected to GPIO10 clockwise and anti-clockwise.

var Gpio =require('pigpio').Gpio,

motor =newGpio(10,{mode:Gpio.OUTPUT}),

pulseWidth =1000,

increment =100;

setInterval(function(){

motor.servoWrite(pulseWidth);

pulseWidth += increment;

if(pulseWidth >=2000){

increment =-100;

}elseif(pulseWidth <=1000){

increment =100;

}

},1000);

Measure distance with a HC-SR04 ultrasonic sensor

The trigger function can be used to generate a pulse on a GPIO and alerts can
be used to determine the time of a GPIO state change accurate to a few
microseconds. These two features can be combined to measure distance using a
HC-SR04 ultrasonic sensor.

var Gpio =require('pigpio').Gpio,

trigger =newGpio(23,{mode:Gpio.OUTPUT}),

echo =newGpio(24,{mode:Gpio.INPUT, alert:true});

// The number of microseconds it takes sound to travel 1cm at 20 degrees celcius

varMICROSECDONDS_PER_CM=1e6/34321;

trigger.digitalWrite(0);// Make sure trigger is low

(function(){

var startTick;

echo.on('alert',function(level,tick){

var endTick,

diff;

if(level ==1){

startTick = tick;

}else{

endTick = tick;

diff =(endTick >>0)-(startTick >>0);// Unsigned 32 bit arithmetic

console.log(diff /2/MICROSECDONDS_PER_CM);

}

});

}());

// Trigger a distance measurement once per second

setInterval(function(){

trigger.trigger(10,1);// Set trigger high for 10 microseconds

},1000);

Determine the width of a pulse with alerts

Alerts can be used to determine the time of a GPIO state change accurate to a
few microseconds. Typically, alerts will be used for GPIO inputs but they can
also be used for outputs. In this example, the trigger method is used to
pulse the LED connected to GPIO17 on for 15 microseconds once per second.
Alerts are used to measure the length of the pulse.