raspi-i2c
TypeScript icon, indicating that this package has built-in type declarations

6.2.4 • Public • Published

Raspi I2C

Gitter

Raspi I2C is part of the Raspi.js suite that provides access to the hardware I2C on pins 3 (SDA0) and 5 (SCL0).

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

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

Installation

First, be sure that you have installed raspi.

Install with npm:

npm install raspi-i2c

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 Usage

In TypeScript/ES6:

import { init } from 'raspi';
import { I2C } from 'raspi-i2c';
 
init(() => {
  const i2c = new I2C();
  console.log(i2c.readByteSync(0x18)); // Read one byte from the device at address 18
});

In JavaScript:

const raspi = require('raspi');
const I2C = require('raspi-i2c').I2C;
 
raspi.init(() => {
  const i2c = new I2C();
  console.log(i2c.readByteSync(0x18)); // Read one byte from the device at address 18
});

Notes and Limitations

There are a few limitations and extra steps to be aware of when using I2C on the Raspberry Pi.

First and foremost, be aware that once you use an I2C pin for GPIO, you cannot use it for I2C again until you reboot your Raspberry Pi! If you run the following Johnny-Five code which leverages this library under the hood, you will get an exception stating "I2C pins not in I2C mode."

const Raspi = require('raspi-io');
const five = require('johnny-five');
const board = new five.Board({
  io: new Raspi()
});
 
board.on('ready', () => {
  new five.Pin('SDA');
  board.io.i2cWrite(0x18, 0x5, 'hello');
});

Also note that you will need to edit /boot/config.txt in order to change the I2C baud rate from the default, if you need to. If you notice that behavior is unstable while trying to communicate with another microcontroller, try setting the baudrate to 10000 from the default 100000. This instability has been observed on the Arduino Nano before.

After you install Raspi IO for the first time, you must reboot your Raspberry Pi. I2C support is not enabled by default, and this module runs a script to enable it automatically and adjust a few I2C settings. These settings will not take effect until you reboot your Pi.

Finally, if you try to access a device that doesn't exist, you will get an error stating EIO, i/o error (sorry it's not very descriptive).

API

Module Constants

Constant Description
module An easily consumable object for indirectly passing this module around. Intended specifically for use by Core IO (details coming soon)
Function
Property Type Description
createI2C()
Pass through for the I2C constructor

new I2C(pins)

Instantiates a new I2C instance on the given pins. Note that I2C is limited to only 1 pair of pins on all current models of the Raspberry Pi. Check the wiring information wiki for more information.

Arguments:

Argument Type Description
pins (optional) Number[] | Object The configuration of the I2C port. If the config is an array of numbers or strings, it is assumed to be the SDA and SCL pins for the peripheral. If it is an object, the following properties are supported:
Property Type Description
pins

Instance Methods

read(address, register, length, cb)

Reads data from the peripheral at address from the optional register and calls the callback once length bytes have been read.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to read from. Must be between 0 and 0x7f.
length Number The address of the peripheral to read from. Must be between 0 and 0xff.
cb Function The callback to call once the read is complete
Argument Type Description
err String | null The error, if one occurred, else null
data null | Buffer If no error occurred, the read data

Returns: None

readSync(address, register, length)

Synchronous version of read.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to read from. Must be between 0 and 0x7f.
length Number The address of the peripheral to read from. Must be between 0 and 0xff.

Returns: A buffer containing the read data

readByte(address, register, cb)

Reads a single byte from the peripheral at address from the optional register and calls the callback.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to read from. Must be between 0 and 0x7f.
cb Function The callback to call once the read is complete
Argument Type Description
err String | null The error, if one occurred, else null
data null | Number If no error occurred, the read byte

Returns: None

readByteSync(address, register)

Synchronous version of readByte.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to read from. Must be between 0 and 0x7f.

Returns: The byte in the form of a number.

readWord(address, register, cb)

Reads a single word from the peripheral at address from the optional register and calls the callback.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to read from. Must be between 0 and 0x7f.
cb Function The callback to call once the read is complete
Argument Type Description
err String | null The error, if one occurred, else null
data null | Number If no error occurred, the read byte

Returns: None

readWordSync(address, register)

Synchronous version of readWord.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to read from. Must be between 0 and 0x7f.

Returns: The word in the form of a number.

write(address, register, buffer, cb)

Writes the data in buffer to the peripheral at address to the optional register and calls the callback once the write has completed.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to read from. Must be between 0 and 0x7f.
buffer Buffer The data to write to the peripheral.
cb Function The callback to call once the write is complete
Argument Type Description
err String | null The error, if one occurred, else null

Returns: None

writeSync(address, register, buffer)

Synchronous version of write.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to read from. Must be between 0 and 0x7f.
buffer Buffer The data to write to the peripheral.

Returns: None

writeByte(address, register, byte, cb)

Writes byte to peripheral at address to the optional register and calls the callback.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to write to. Must be between 0 and 0x7f.
byte Number The byte to write. The value is coerced to an unsigned 8-bit integer.
cb Function The callback to call once the write is complete
Argument Type Description
err String | null The error, if one occurred, else null

Returns: None

writeByteSync(address, register, byte)

Synchronous version of writeByte.

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to write to. Must be between 0 and 0x7f.
byte Number The byte to write. The value is coerced to an unsigned 8-bit integer.

Returns: None

writeWord(address, register, word, cb)

Writes word to peripheral at address to the optional register and calls the callback.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to write to. Must be between 0 and 0x7f.
word Number The word to write. The value is coerced to an unsigned 16-bit integer.
cb Function The callback to call once the write is complete
Argument Type Description
err String | null The error, if one occurred, else null

Returns: None

writeWordSync(address, register, word)

Synchronous version of writeWord.

Arguments:

Argument Type Description
address Number The address of the peripheral to read from. Must be 0 or greater and less than 0x80.
register (optional) Number The register to write to. Must be between 0 and 0x7f.
word Number The word to write. The value is coerced to an unsigned 16-bit integer.

Returns: None

License

The MIT License (MIT)

Copyright (c) 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.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 6.2.4
    39
    • latest

Version History

Package Sidebar

Install

npm i raspi-i2c

Weekly Downloads

39

Version

6.2.4

License

MIT

Unpacked Size

55.1 kB

Total Files

8

Last publish

Collaborators

  • nebrius