npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

rpi-sysfs-io

v1.0.1

Published

A NodeJS module for Raspberry PI gpio and pwm io through the linux sysfs interface.

Readme

rpi-sysfs-io

A NodeJS module for Raspberry PI gpio and pwm io through the linux sysfs interface.

Many of the Raspberry PI GPIO features can be accessed via the linux sysfs interface. This includes pulse width modulation (PWM) and general purpose I/O (GPIO). The rpi-sysfs-io module provides methods to access these features through the sysfs interface.

install

In your node project directory use npm to install the module...

npm install --save rpi-sysfs-io

usage

Usage of the rpi-sysfs-io module involves the creation of an instance of the class followed by calling the asynchronous methods to control the I/O signals.

GPIO example

This example assumes that GPIO 18 is being used as an ouput to control an LED.

// create an instance of the rpi-sysfs-io class
const RPiSysfsIO = require('rpi-sysfs-io');
const LED = 18;
let gpio = new RPiSysfsIO();

// initialize the gpio
init()
.then(() => {
  // start the LED blinking
  blinkLED();
})
.catch(error => {
  console.log('ERROR:', error.message);
});


// asynchronous method to initialize the LED GPIO
async function init () {
  // make sure the GPIO is exported
  if (!(await gpio.isExportedGPIO(LED))) {
    // export the GPIO and wait until the export is complete
    await gpio.exportGPIO(18, true);
  }
  // set the LED GPIO for output and initialize the initial state
  await gpio.directionGPIO(LED, 'out');
  await gpio.writeGPIO(LED, 0);
}

// method to blink the LED on the GPIO pin
function blinkLED() {
  // get the current GPIO value
  gpio.readGPIO(LED)
  .then(value => {
    // toggle the GPIO value
    return gpio.writeGPIO(LED, (value ? 0 : 1));
  })
  .then(() => {
    // wait a second then call blinkLED again
    setTimeout(blinkLED, 1000);
  })
}

PWM example

This example assumes the Raspberry Pi has been configured to boot with the PWM hardware enabled. I.E. the /boot/config.txt file has the following device tree overlay setting...

dtoverlay=pwm,pin=12,func=4

// create an instance of the rpi-sysfs-io class
const RPiSysfsIO = require('rpi-sysfs-io');
const PWM = 0;
const PWM_PERIOD = 1000000;
const PWM_DUTY_CYCLE = 100000;
let gpio = new RPiSysfsIO();
let enable = 0;

// initialize the gpio
init()
.then(() => {
  // start the PWM cycling
  togglePWM();
})
.catch(error => {
  console.log('ERROR:', error.message);
});


// asynchronous method to initialize the PWM
async function init () {
  // make sure the GPIO is exported
  if (!(await gpio.isExportedPWM(PWM))) {
    // export the PWM and wait until the export is complete
    await gpio.exportPWM(PWM, true);
  }

  // apply the PWM settings
  await gpio.periodPWM(PWM, PWM_PERIOD);
  await gpio.dutyCyclePWM(PWM, PWM_DUTY_CYCLE);
  await gpio.enablePWM(PWM, enable);
}

// method to toggle the PWM off and on
function togglePWM() {
  enable = (enable ? 0 : 1);
  gpio.enablePWM(PWM, enable)
  .then(() => {
    // wait a moment then call togglePWM again
    setTimeout(togglePWM, 500);
  })
}

methods

constructor

The rpi-sysfs-io module is a Javascript class with a constructor. You can pass parameters to the constructor when creating an instance of the class if you need to override any of the default settings.

I.E.

const RPiSysfsIO = require('rpi-sysfs-io');
// create instance and override the timeout delay when waiting for an export success
let gpio = new RPiSysfsIO({ exportWaitTimeout: 500 });

exportWaitTimeout

When calling the exportGPIO() or exportPWM() methods they may include a wait for the exported path to complete. The exportWaitTimeout is the number of milliseconds to wait before throwing a timeout error.

exportWaitInterval

When the exportGPIO() or exportPWM() methods are waiting for the export to complete there is a delay between checks to see if the export has completed. The exportWaitInterval is the number of milliseconds between checks to see if the export has completed.

sysfsGPIOPath

This is the GPIO path in the sysfs interface.

sysfsPWMPath

This is the PWM path in the sysfs interface.

exportGPIO (gpio, waitExport)

An asynchronous method used to export a GPIO pin in the sysfs interface. Pass an integer as the argument for the GPIO to export. If a boolean true is passed as the waitExport argument then the method will only resolve after the GPIO is finished with the export process.

unexportGPIO (gpio)

An asynchronous method to unexport a GPIO in the sysfs interface by passing the number of the GPIO interface to be unexported.

isExportedGPIO (gpio)

An asynchronous method to check if a GPIO is already exported in the sysfs interface. Provide the gpio number and the method will resolve with a boolean true if the GPIO is already exported.

directionGPIO (gpio, direction)

An asynchronous method to set the direction of the GPIO, 'in' or 'out'.

writeGPIO (gpio, data)

An asynchronous method to write a value to a GPIO.

readGPIO (gpio)

An asynchronous method to read the value of a GPIO.

exportPWM (pwm, waitExport)

An asynchronous method used to export a PWM pin in the sysfs interface. Pass an integer as the argument for the PWM to export. If a boolean true is passed as the waitExport argument then the method will only resolve after the PWM is finished with the export process.

unexportPWM (pwm)

An asynchronous method to unexport a PWM in the sysfs interface by passing the number of the PWM interface to be unexported.

isExportedPWM (pwm)

An asynchronous method to check if a PWM is already exported in the sysfs interface. Provide the PWM number and the method will resolve with a boolean true if the PWM is already exported.

periodPWM (pwm, period)

Set the total period of a PWM channel in nanoseconds. This will determine the frequency of the PWM signal.

dutyCyclePWM (pwm, dutyCycle)

Set the active time period of the PWM channel in nanoseconds. This will determine the on time in the PWM signal.

enablePWM (pwm, enable)

Set the PWM signal on or off.

TODO

  • Add polling methods