Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 88 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,145 +1,125 @@
# i2c
# i2c-bus

Bindings for i2c-dev. Plays well with Raspberry Pi and Beaglebone.
[![npm](https://img.shields.io/npm/v/i2c-bus.svg)](https://www.npmjs.com/package/i2c-bus)
[![Build Status](https://travis-ci.org/korevec/node-i2c.svg?branch=master)](https://travis-ci.org/korevec/node-i2c)

## Install
`i2c-bus` provides native bindings for `i2c-dev`. It's designed to play well with Raspberry Pi and BeagleBone.

````bash
$ npm install i2c
````
## Features

## Usage

```javascript

var i2c = require('i2c');
var address = 0x18;
var wire = new i2c(address, {device: '/dev/i2c-1'}); // point to your i2c address, debug provides REPL interface

wire.scan(function(err, data) {
// result contains an array of addresses
});

wire.writeByte(byte, function(err) {});

wire.writeBytes(command, [byte0, byte1], function(err) {});

wire.readByte(function(err, res) { // result is single byte })

wire.readBytes(command, length, function(err, res) {
// result contains a buffer of bytes
});

wire.on('data', function(data) {
// result for continuous stream contains data buffer, address, length, timestamp
});
- **Promise and Callback API:** Supports modern `async/await` and traditional callback patterns.
- **Robust Error Handling:** Provides clear and consistent error messages.
- **Comprehensive Platform Support:** Works seamlessly with Raspberry Pi, BeagleBone, and other Linux-based systems.

wire.stream(command, length, delay); // continuous stream, delay in ms
## Installation

```bash
npm install i2c-bus
```

// plain read/write

wire.write([byte0, byte1], function(err) {});

wire.read(length, function(err, res) {
// result contains a buffer of bytes
});


````

## Raspberry Pi Setup
## Usage

Here's a quick example of how to use `i2c-bus` with `async/await`:

````bash
$ sudo vi /etc/modules
````
```javascript
const i2c = require('i2c-bus');
const { promisify } = require('util');

Add these two lines
const I2C_ADDR = 0x18;
const wire = new i2c(I2C_ADDR, { device: '/dev/i2c-1' });

````bash
i2c-bcm2708
i2c-dev
````
const scan = promisify(wire.scan.bind(wire));
const readByte = promisify(wire.readByte.bind(wire));
const writeByte = promisify(wire.writeByte.bind(wire));

````bash
$ sudo vi /etc/modprobe.d/raspi-blacklist.conf
````
(async () => {
try {
const devices = await scan();
console.log('Found devices:', devices);

Comment out blacklist i2c-bcm2708
await writeByte(0x42);
const byte = await readByte();
console.log('Read byte:', byte);
} catch (err) {
console.error('Error:', err);
}
})();
```

````
#blacklist i2c-bcm2708
````
## API Reference

Load kernel module
All methods are available on the `wire` object created by `new i2c(address, options)`.

````bash
$ sudo modprobe i2c-bcm2708
$ sudo modprobe i2c-dev
````
**`scan(callback)`**

Make device writable
- Scans the I2C bus for connected devices.
- `callback(err, devices)`:
- `err`: An error object if the scan fails.
- `devices`: An array of detected I2C addresses.

````bash
sudo chmod o+rw /dev/i2c*
````
**`writeByte(byte, callback)`**

Install gcc 4.8 (required for Nan)
- Writes a single byte to the device.
- `callback(err)`: Called once the write is complete.

````bash
sudo apt-get install gcc-4.8 g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
sudo update-alternatives --config gcc
**`writeBytes(command, buffer, callback)`**

````
- Writes a sequence of bytes to the device.
- `command`: The command to write.
- `buffer`: A `Buffer` or array of bytes.
- `callback(err)`: Called once the write is complete.

Set correct device for version
**`readByte(callback)`**

```javascript
- Reads a single byte from the device.
- `callback(err, byte)`:
- `err`: An error object if the read fails.
- `byte`: The byte read from the device.

new i2c(address, device: '/dev/i2c-0') // rev 1
new i2c(address, device: '/dev/i2c-1') // rev 2
**`readBytes(command, length, callback)`**

````
- Reads a block of bytes from the device.
- `command`: The command to read from.
- `length`: The number of bytes to read.
- `callback(err, buffer)`:
- `err`: An error object if the read fails.
- `buffer`: A `Buffer` containing the bytes read.

## Beaglebone
**`stream(command, length, delay)`**

````bash
$ ntpdate -b -s -u pool.ntp.org
$ opkg update
$ opkg install python-compile
$ opkg install python-modules
$ opkg install python-misc
$ npm config set strict-ssl false
$ npm install i2c
````
- Starts a continuous stream of data from the device.
- `command`: The command to read from.
- `length`: The number of bytes to read in each chunk.
- `delay`: The delay in milliseconds between reads.
- Emits a `data` event with a data object containing the buffer, address, length, and timestamp.

## Node 0.11 and under
## Raspberry Pi Setup

````bash
npm install i2c@0.1.8
````
1. **Enable I2C:**
- Run `sudo raspi-config`.
- Navigate to `Interfacing Options` > `I2C`.
- Select `<Yes>` to enable the I2C interface.

## Projects using i2c
2. **Install Dependencies:**
- Ensure `build-essential` and `python-dev` are installed:
```bash
sudo apt-get install build-essential python-dev
```

- **bonescript** https://github.com/jadonk/bonescript/
- **ADXL345** https://github.com/timbit123/ADXL345
- **HMC6343** https://github.com/omcaree/node-hmc6343
- **LSM303** https://github.com/praneshkmr/node-lsm303
- **MPU6050** https://github.com/jstapels/mpu6050/
- **MCP3424** https://github.com/x3itsolutions/mcp3424
- **blinkm** https://github.com/korevec/blinkm
- **click boards** https://github.com/TheThingSystem/node-click-boards
- more: https://www.npmjs.org/browse/depended/i2c
3. **Set Device Permissions:**
- Make the I2C device writable:
```bash
sudo chmod o+rw /dev/i2c*
```

## BeagleBone Setup

## Contributors
For BeagleBone, the I2C buses are typically enabled by default. You may need to load the appropriate cape if you're using a custom hardware setup.

Thanks to @alphacharlie for Nan rewrite, and @J-Cat for Node 14 updates.
## Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

## Questions?
## License

http://www.twitter.com/korevec
This project is licensed under the BSD-3-Clause license. See the [LICENSE](LICENSE) file for details.