Skip to content

A simple node.js-based GPIO helper for the Raspberry Pi

Notifications You must be signed in to change notification settings

brentnycum/pi-gpio

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pi-gpio

pi-gpio is a simple node.js based library to help access the GPIO of the Raspberry Pi (Debian Wheezy). It's modelled loosely around the built-in fs module.

var gpio = require("pi-gpio");

gpio.open(16, "output", function(err) {		// Open pin 16 for output
	gpio.write(16, 1, function() {			// Set pin 16 high (1)
		gpio.close(16);						// Close pin 16
	});
});

Unmaintained

This project is unmaintained, and is up for adoption. If anyone wants to take over the development of this project, please drop me a line, and we'll take it from there. Another way to help would be to review the pull requests and test them on a Pi for correctness.

About the pin configuration

This couldn't have been more confusing. Raspberry Pi's physical pins are not laid out in any particular logical order. Most of them are given the names of the pins of the Broadcom chip it uses (BCM2835). There isn't even a logical relationship between the physical layout of the Raspberry Pi pin header and the Broadcom chip's pinout. The OS recognizes the names of the Broadcom chip and has nothing to do with the physical pin layout on the Pi. To add to the fun, the specs for the Broadcom chip are nearly impossible to get!

This library simplifies all of this (hopefully), by abstracting away the Broadcom chip details. You only need to refer to the pins as they are on the physical pin layout on the Raspberry PI. For your reference, the pin layout follows. The cells with the grey background represent the pins, with P1 at the top left.

P1 - 3.3v 1 2 5v
I2C SDA 3 4 --
I2C SCL 5 6 Ground
GPIO 7 8 TX
-- 9 10 RX
GPIO 11 12 GPIO
GPIO 13 14 --
GPIO 15 16 GPIO
-- 17 18 GPIO
SPI MOSI 19 20 --
SPI MISO 21 22 GPIO
SPI SCLK 23 24 SPI CE0
-- 25 26 SPI CE1

That gives you several GPIO pins to play with: pins 7, 11, 12, 13, 15, 16, 18 and 22. You should provide these physical pin numbers to this library, and not bother with what they are called internally. Easy-peasy.

Installation

If you haven't already, get node and npm on the Pi. The simplest way is:

sudo apt-get install nodejs npm

The Raspberry Pi's GPIO pins require you to be root to access them. That's totally unsafe for several reasons. To get around this problem, you should use the excellent gpio-admin.

Do the following on your raspberry pi:

git clone git://github.com/quick2wire/quick2wire-gpio-admin.git
cd quick2wire-gpio-admin
make
sudo make install
sudo adduser $USER gpio

After this, you will need to logout and log back in. Details, if you are interested.

Next, cd to your project directory and use npm to install pi-gpio in your project.

npm install pi-gpio

That's it!

Usage

.open(pinNumber, [direction = "output"], [callback])

Aliased to .export

Makes pinNumber available for use.

  • pinNumber: The pin number to make available. Remember, pinNumber is the physical pin number on the Pi.
  • direction: (Optional) Direction can either be input or output, depending on what you want to do with the pin. You could alternatively use in or out. Default: output.
  • callback: (Optional) Will be called when the pin is available for use. May receive an error as the first argument if something went wrong.

.close(pinNumber, [callback])

Aliased to .unexport

Closes pinNumber.

  • pinNumber: The pin number to close. Again, pinNumber is the physical pin number on the Pi.
  • callback: (Optional) Will be called when the pin is closed. Again, may receive an error as the first argument.

.setDirection(pinNumber, direction, [callback])

Changes the direction from input to output or vice-versa.

  • pinNumber: As usual.
  • direction: Either input or in or output or out.
  • callback: Will be called when direction change is complete. May receive an error as usual.

.getDirection(pinNumber, [callback])

Gets the direction of the pin. Acts like a getter for the method above.

  • pinNumber: As usual
  • callback: Will be called when the direction is received. The first argument could be an error. The second argument will either be in or out.

.read(pinNumber, [callback])

Reads the current value of the pin. Most useful if the pin is in the input direction.

  • pinNumber: As usual.
  • callback: Will receive a possible error object as the first argument, and the value of the pin as the second argument. The value will be either 0 or 1 (numeric).

Example:

gpio.read(16, function(err, value) {
	if(err) throw err;
	console.log(value);	// The current state of the pin
});

.write(pinNumber, value, [callback])

Writes value to pinNumber. Will obviously fail if the pin is not in the output direction.

  • pinNumber: As usual.
  • value: Should be either a numeric 0 or 1. Any value that isn't 0 or 1 will be coerced to be boolean, and then converted to 0 (false) or 1 (true). Just stick to sending a numeric 0 or 1, will you? ;)
  • callback: Will be called when the value is set. Again, might receive an error.

Misc

  • To run tests: npm install && npm test where you've got the checkout.
  • This module was created, git push'ed and npm publish'ed all from the Raspberry Pi! The Pi rocks!

Coming soon

  • Support for I2C and SPI (though it should already be possible to bit-bang the SPI protocol).
  • Any other suggestions?

License

(The MIT License)

Copyright (c) 2012 Rakesh Pai [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A simple node.js-based GPIO helper for the Raspberry Pi

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%