raspi-i2c
v6.2.4
Published
Provides access to I2C on the Raspberry Pi from Node.js
Downloads
194
Maintainers
Readme
Raspi I2C
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
- Raspberry Pi Model B Rev 1 or newer (sorry Model A users)
- Raspbian Jessie or newer
- Node-RED works, but can be finicky and difficult to debug.
- See https://github.com/nebrius/raspi-io/issues/24 for more info about support for other OSes
- 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
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
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:
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:
Returns: None
readSync(address, register, length)
Synchronous version of read
.
Arguments:
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:
Returns: None
readByteSync(address, register)
Synchronous version of readByte
.
Arguments:
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:
Returns: None
readWordSync(address, register)
Synchronous version of readWord
.
Arguments:
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:
Returns: None
writeSync(address, register, buffer)
Synchronous version of write
.
Arguments:
Returns: None
writeByte(address, register, byte, cb)
Writes byte to peripheral at address to the optional register and calls the callback.
Arguments:
Returns: None
writeByteSync(address, register, byte)
Synchronous version of writeByte
.
Returns: None
writeWord(address, register, word, cb)
Writes word to peripheral at address to the optional register and calls the callback.
Arguments:
Returns: None
writeWordSync(address, register, word)
Synchronous version of writeWord
.
Arguments:
Returns: None
License
The MIT License (MIT)
Copyright (c) Bryan Hughes [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.