Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

raspi-board

nebrius525MIT7.4.0TypeScript support: included

Pin mapping functionality for the Raspberry Pi in Node.js

nodebots, robotics, raspberry pi

readme

Raspi Board

Gitter Build Status Coverage Status

Raspi Board provides pin mapping functionality for the various Raspberry Pi models. This module is meant to be run on a physical Raspberry Pi only, and maps pins for the specific board it is being run on.

If you have a bug report, feature request, or wish to contribute code, please be sure to check out the Raspi IO Contributing Guide.

System Requirements

  • Raspberry Pi Model B Rev 1 or newer (sorry Model A users), except compute modules.
  • Raspbian Jessie or newer
  • Node 6.0.0 or newer

Detailed instructions for getting a Raspberry Pi ready for NodeBots, including how to install Node.js, can be found in the wiki

Installation

Install with npm:

npm install raspi-board

Note: this project is written in TypeScript and includes type definitions in the package.json file. This means that if you want to use it from TypeScript, you don't need to install a separate @types module.

Example

const board = require('raspi-board');

// Prints 14
console.log(board.getPinNumber('SCLK0'));

Pin Naming

The pins on the Raspberry Pi are a little complicated. There are multiple headers on some Raspberry Pis with extra pins, and the pin numbers are not consistent between Raspberry Pi board versions.

To help make it easier, you can specify pins in three ways. The first is to specify the pin by function, e.g. 'GPIO18'. The second way is to specify by pin number, which is specified in the form "P[header]-[pin]", e.g. 'P1-7'. The final way is specify the Wiring Pi virtual pin number, e.g. 7. If you specify a number instead of a string, it is assumed to be a Wiring Pi number.

Be sure to read the full list of pins on the supported models of the Raspberry Pi.

API

Module Constants

Constant Description
VERSION_1_MODEL_A Constant representing the Raspberry Pi 1 Model A. Returned from getBoardRevision()
VERSION_1_MODEL_B_REV_1 Constant representing the Raspberry Pi 1 Model B Rev 1. Returned from getBoardRevision()
VERSION_1_MODEL_B_REV_2 Constant representing the Raspberry Pi 1 Model B Rev 1. Returned from getBoardRevision()
VERSION_1_MODEL_A_PLUS Constant representing the Raspberry Pi 1 Model A+. Returned from getBoardRevision()
VERSION_1_MODEL_B_PLUS Constant representing the Raspberry Pi 1 Model B+. Returned from getBoardRevision()
VERSION_1_MODEL_ZERO Constant representing the Raspberry Pi Zero. Returned from getBoardRevision()
VERSION_1_MODEL_ZERO_W Constant representing the Raspberry Pi Zero W. Returned from getBoardRevision()
VERSION_2_MODEL_B Constant representing the Raspberry Pi 2 Model B. Returned from getBoardRevision()
VERSION_3_MODEL_B Constant representing the Raspberry Pi 3 Model B. Returned from getBoardRevision()
VERSION_3_MODEL_B_PLUS Constant representing the Raspberry Pi 3 Model B+. Returned from getBoardRevision()
VERSION_3_MODEL_A_PLUS Constant representing the Raspberry Pi 3 Model A+. Returned from getBoardRevision()
VERSION_4_MODEL_B Constant representing the Raspberry Pi 4 Model B. Returned from getBoardRevision()
VERSION_400 Constant representing the Raspberry Pi 400. Returned from getBoardRevision()
VERSION_UNKNOWN Constant representing an unknown or unsupported version of the Raspberry Pi. Returned from getBoardRevision()
module An easily consumable object for indirectly passing this module around. Intended specifically for use by Core IO (details coming soon)
Property Description
getPins Alias of the getPins method
getPinNumber Alias of the getPinNumber method
getGpioNumber Alias of the getGpioNumber method

getBoardRevision()

Gets the board revision.

Arguments: None.

Returns: A constant representing the board version, one of VERSION_1_MODEL_A, VERSION_1_MODEL_B_REV_1, VERSION_1_MODEL_B_REV_2, VERSION_1_MODEL_B_PLUS, VERSION_1_MODEL_A_PLUS, VERSION_1_MODEL_ZERO, VERSION_1_MODEL_ZERO_W, VERSION_2_MODEL_B, VERSION_3_MODEL_B, VERSION_3_MODEL_B_PLUS, VERSION_3_MODEL_A_PLUS, VERSION_4_MODEL_B, VERSION_UNKNOWN

getPins()

Gets a list of all pins for the device it's running on.

Arguments: None.

Returns: An array of pin entry objects structured such that the array index is the Wiring Pi pin number, each with the following structure:

Property Type Description
pins Array An array of all the pin names for pin, e.g. ['GPIO21', 'SCLK1', 'P1-40']
peripherals Array An array of all the peripherals supported on the pin, and may contain any combination of 'gpio', 'pwm', 'i2c', 'uart', or 'spi'
gpio number The GPIO number for the pin, e.g. `4` for `GPIO4`. This method works regardless of pinMode

getPinNumber(pin)

Normalizes a pin name to a Wiring Pi pin number. This method accounts for the differences between board revisions by checking which Raspberry Pi the code is running on.

Arguments:

Argument Type Description
pin String | Number The pin name/number to normalize. If a number is passed in, it is assumed to be the Wiring Pi pin number

Returns: A number representing the Wiring Pi pin number, or null if an invalid pin name was passed in.

getGpioNumber(pin)

Normalizes a pin name to a GPIO pin number, e.g. TXD0/GPIO14 returns 14. This method accounts for the differences between board revisions by checking which Raspberry Pi the code is running on.

Arguments:

Argument Type Description
pin String | Number The pin name/number to normalize. If a number is passed in, it is assumed to be the Wiring Pi pin number

Returns: A number representing the GPIO pin number, or null if an invalid pin name was passed in.

License

The MIT License (MIT)

Copyright (c) 2014-2017 Bryan Hughes bryan@nebri.us

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.

changelog

7.4.0 (2021-6-3)

  • Added more missing boards
  • Added Pi 400 board type
  • Removed warning message for unsupported types, since this project isn't really supported anymore.

7.3.4 (2020-1-17)

  • Added another missing board

7.3.3 (2020-9-2)

  • Added a new RPi 4 model
  • Tweaked the unknown board message to be clearer

7.3.2 (2020-7-15)

  • Added support for a handful of missing RPi models

7.3.1 (2020-3-9)

  • Added support for another Raspberry Pi 4 Model B variant

7.3.0 (2019-9-12)

  • Added better error checking so this module fails gracefully on non-Raspberry Pi systems (thanks to @wolasss for the PR!)

7.2.3 (2019-9-03)

  • Added support for another Raspberry Pi 4 Model B variant

7.2.2 (2019-7-17)

  • Fixed a bug where the Raspberry Pi 4 wasn't properly wired in to the board detection logic

7.2.1 (2019-7-11)

  • Added support for another Raspberry Pi 4 Model B variant

7.2.0 (2019-7-9)

  • Added support for the Raspberry Pi 4 Model B

7.1.2 (2019-2-20)

  • Updated dependencies

7.1.1 (2019-1-15)

  • Fixed a dependency issue where "core-io-types" is not found

7.1.0 (2019-1-10)

  • Reworked the exported pin info to use the Core IO Types pin info interfaces

7.0.0 (2019-1-9)

  • BREAKING CHANGE: Removed module export as Core IO Types is also dropping a board specific module

6.2.0 (2018-12-5)

  • Updated TypeScript to v3 (no compiled output change)
  • Added module export to conform with Core IO types

6.1.0 (2018-11-29)

  • Added support for the Raspberry Pi 3 Model A+

6.0.0 (2018-09-21)

  • BREAKING CHANGE: dropped support for Node.js 4.x.x
  • Rewrote unit tests to use Jasmine and bring it in line with other Raspi IO modules
  • Rewrote test mode to use an environment variable instead of a global variable
  • Upgraded dependencies, including bumping TypeScript to v3 and tweaking the TS configuration to target newer versions of JavaScript

5.3.1 (2018-06-12)

  • Added missing RPi 3 Model B revision number

5.3.0 (2018-04-28)

  • Added a missing RPi 1 Model B+ and RPi 3 Model B revision numbers
  • Changed the "unknown board" warning message to an info message and added a much longer description.

5.2.0 (2018-04-21)

  • Added support for the Raspberry Pi 3 Model B+

5.1.0 (2018-01-18)

  • Added support for overclocked Pi Zero W's

5.0.2 (2017-11-18)

  • Updated the tsconfig.json and tslint.json configuration files to use a newer style, build tools updated
  • Cleaned up some TypeScript type definitions to be more compatible with DefinitelyTyped
  • Note: there is no functionality change with this update

5.0.1 (2017-9-11)

  • Non-code change: updated the README and republishing to make sure it's in npm

5.0.0 (2017-9-11)

  • BREAKING CHANGE: dropped support for Node.js < 4.0.0, and now enforce it via package.json "engines" field.
  • SORT OF BREAKING-ISH CHANGE: dropped support for attempting to install on non-arm platforms via package.json's "cpu" field.
    • Attempting to install this on a non-Raspberry Pi platform previously wasn't very useful because it usually crashed with a cryptic error. Now it complains earlier and harder.

4.2.0 (2017-4-21)

  • Added the getGpioNumber method
  • Added the gpio property on pin info returned from the getPins method
  • Fixed some bugs around VERSION_UNKNOWN not being exported nor returned from getBoardRevision

4.1.0 (2017-4-19)

  • Added support for the Raspberry Pi Zero W

4.0.2 (2017-1-22)

  • Publishing a new version to update the README on npmjs.com. No other changes.

4.0.1 (2016-12-3)

  • Converted the project to TypeScript and cleaned up a bunch of odds and ends
    • Note: there is no functionality change or bug fixes with this release

4.0.0 (2016-10-13)

  • Added missing revision IDs for certain batches of the Raspbery Pi Zero
  • Updated logic so an unknown board assumes Raspberry Pi 3 Model B pinout
    • POTENTIALLY BREAKING CHANGE: previous behavior was to throw an exception

3.2.0 (2016-9-19)

  • Added missing revision ID for certain batches of the Raspberry Pi Model A+

3.1.0 (2016-6-21)

  • Added missing revision ID for certain batches of the Raspbery Pi Zero

3.0.0 (2016-3-20)

  • Fixed a bug where PWM1 wasn't mapped to 23.
    • POTENTIALLY BREAKING CHANGE: using the identifier PWM1 now maps to Wiring Pi pin 23, not 24
  • New build system

2.4.1 (2016-3-7)

  • Added a missing revision ID for certain batches of Raspberry Pi 3s

2.4.0 (2016-3-4)

  • Added Raspberry Pi 3 Model B support

2.3.0 (2015-12-8)

  • Added Raspberry Pi Zero support
  • Refactored unit tests to use nodeunit

2.2.2 (2015-9-3)

  • Fixed a bug where getPinNumber(0) was returning null instead of 0

2.2.1 (2015-7-14)

  • Updated the repository links to match the new location

2.2.0 (2015-7-14)

  • Realized that this should be a minor bump, not a patch bump
  • There is a very subtle behavior change in getPinNumber
    • If a pin is not found, it now returns null instead of undefined
    • The behavior when a pin is not found was previously undocumented before this release

2.1.3 (2015-7-14)

  • Fixed a bug with detecting overclocked RPi revisions
  • Added .eslintrc file
  • Updated code to pass linting

2.1.2 (2015-3-29)

  • Added some error checking to getPinNumber to prevent a crash when passing in undefined or null

2.1.1 (2015-3-17)

  • Fixed a broken unit test

2.1.0 (2015-2-21)

  • Switched from traceur to babel for ES6->ES5 compilation

2.0.0 (2015-2-17)

  • Added support for the Raspberry Pi 2
  • Added constants for the different board revisions
  • Cleaned up error handling surrounding unsupported or unknown versions
  • Reworked getBoardRevision to return one of the above constants. THIS IS A BREAKING CHANGE!

1.3.0 (2015-2-12)

  • Added getBoardRevision method

1.2.2 (2014-12-30)

  • Added support for looking up Wiring Pi pin numbers that are passed in as a string

1.2.1 (2014-12-29)

  • README updates

1.2.0 (2014-12-20)

  • Added P5 support for R2 models
  • Added some missed peripherals
  • POTENTIALLY BREAKING: Modified all peripherals to have a number, so "SDA"->"SDA0", etc
    • This change was done to allow for multiple SPI connections
    • It's possible there will be multiple I2C and UART pins on future revisions (the BCM2835 supports them)

1.1.2 (2014-12-2)

  • Well this is embarrassing, I had a bug in package.json

1.1.1 (2014-12-1)

  • Bug fix with looking up a pin number by said pin number

1.1.0 (2014-12-1)

  • Switched to using ECMAScript 6
  • Added peripheral usage and removed # => P1-# mappings
    • This is a breaking API change!

1.0.2 (2014-11-27)

  • Reworked aliases so that they map to Wiring Pi numbers, not header numbers
  • Added support for "P1-#" style identifiers so that eventually P5 can be supported too

1.0.0-1.0.1 (2014-11-26)

  • Implemented initial functionality