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

Warning: 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.

constGpio=require('pigpio').Gpio;

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

let dutyCycle =0;

setInterval(()=>{

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.

constGpio=require('pigpio').Gpio;

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

constbutton=newGpio(4,{

mode:Gpio.INPUT,

pullUpDown:Gpio.PUD_DOWN,

edge:Gpio.EITHER_EDGE

});

button.on('interrupt',(level)=>{

led.digitalWrite(level);

});

Servo Control

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

constGpio=require('pigpio').Gpio;

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

let pulseWidth =1000;

let increment =100;

setInterval(()=>{

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.

constGpio=require('pigpio').Gpio;

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

constMICROSECDONDS_PER_CM=1e6/34321;

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

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

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

constwatchHCSR04=()=>{

let startTick;

echo.on('alert',(level,tick)=>{

if(level ==1){

startTick = tick;

}else{

constendTick= tick;

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

console.log(diff /2/MICROSECDONDS_PER_CM);

}

});

};

watchHCSR04();

// Trigger a distance measurement once per second

setInterval(()=>{

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.

// Assumption: the LED is off when the program is started

constGpio=require('pigpio').Gpio;

constled=newGpio(17,{

mode:Gpio.OUTPUT,

alert:true

});

constwatchLed=()=>{

let startTick;

// Use alerts to determine how long the LED was turned on

led.on('alert',(level,tick)=>{

if(level ==1){

startTick = tick;

}else{

constendTick= tick;

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

console.log(diff);

}

});

};

watchLed();

// Turn the LED on for 15 microseconds once per second

setInterval(()=>{

led.trigger(15,1);

},1000);

Here's an example of the typical output to the console:

15
15
15
15
15
15
20
15
15
15
15

Debounce a Button

The GPIO glitch filter will prevent alert events from being emitted if the
corresponding level change is not stable for at least a specified number of
microseconds. This can be used to filter out unwanted noise from an input
signal. In this example, a glitch filter is applied to filter out the contact
bounce of a push button.