Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rpi-gpio

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rpi-gpio

Control Raspberry Pi GPIO pins with node.js

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
314
decreased by-38.55%
Maintainers
1
Weekly downloads
 
Created
Source

rpi-gpio.js

Control Raspberry Pi GPIO pins with node.js

Setup

See this guide on how to get node.js running on Raspberry Pi.

Usage

After loading the module, initialise a pin by calling setup. Each GPIO pin can be set as either an input or output, which lets you read and write to it respectively. The 'channel' must be specified, to indicate which pin to use. There are two different ways to reference a channel; either using the Raspberry Pi or the BCM naming schema (sadly, neither of which match the physical pins!). This module supports both schemas, with Raspberry Pi being the default. Please see this page for more details.

All functions within this module are asynchronous, so where necessary - for example in reading the value of a pin - a callback must be provided.

Query the value of a pin

var gpio = require('./rpi-gpio');

gpio.setup(7, gpio.DIR_IN);
gpio.read(7, function(value) {
    console.log('The value is ' + value);
});

Listen for changes on a pin

The GPIO module inherits from EventEmitter so any of the EventEmitter functions can be used. The example below shows how to listen for a change in value to a channel.

var gpio = require('./rpi-gpio');

gpio.setup(7, gpio.DIR_IN);
gpio.on('change', function(channel, value) {
	console.log('Channel ' + channel + ' value is now ' + value);
});

Voltage cycling a pin

This example shows how to set up a channel for output mode. After it is set up, it executes a callback which in turn calls another, causing the voltage to alternate up and down three times.

var gpio = require('./rpi-gpio');

var pin   = 7,
    delay = 2000,
    count = 0,
    max   = 3;

gpio.setup(pin, gpio.DIR_OUT, on);

function on() {
    if (count >= max) {
        process.exit(0);
        return;
    }

    setTimeout(function() {
        gpio.write(pin, 1, off);
        count += 1;
    }, delay);
}

function off() {
    setTimeout(function() {
        gpio.write(pin, 0, on);
    }, delay);
}

Querying a pin in BCM mode

This queries the same physical pin as in the previous examples.

var gpio = require('./rpi-gpio');

gpio.setMode(gpio.MODE_BCM);
gpio.setup(4, gpio.DIR_IN);

gpio.read(4, function(value) {
    console.log('The value is ' + value);
});

FAQs

Package last updated on 24 Jun 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc