I2C

The I2C module supports the I2C protocol. I2C bus has two signals - SDA and SCL.

i2c.open(configuration, callback)

  • configuration {Object} Configuration for open I2CBus.
    • device {string} Device path. (only on Linux)
    • bus {number} The specified bus number. (Tizen, TizenRT and NuttX only)
    • address {number} Device address.
  • callback {Function}
    • err {Error|null}
    • i2cBus {Object} An instance of I2CBus.
  • Returns: {Object} An instance of I2CBus.

Get I2CBus object with configuration asynchronously.

Example

  1. var i2c = require('i2c');
  2. i2c.open({device: '/dev/i2c-1', address: 0x23}, function(err, wire) {
  3. if (err) {
  4. throw err;
  5. }
  6. });

i2c.openSync(configuration)

  • configuration {Object} Configuration for open I2CBus.
    • device {string} Device path. (only on Linux)
    • bus {number} The specified bus number. (Tizen, TizenRT and NuttX only)
    • address {number} Device address.
  • Returns: {Object} An instance of I2CBus.

Get I2CBus object with configuration synchronously.

Example

  1. var i2c = require('i2c');
  2. var wire = i2c.openSync({device: '/dev/i2c-1', address: 0x23});

Class: I2CBus

i2cbus.read(length[, callback])

  • length {number} Number of bytes to read.
  • callback {Function}
    • err {Error|null}
    • res {Array} Array of bytes.

Read bytes from I2C device asynchronously.

Example

  1. var i2c = require('i2c');
  2. i2c.open({device: '/dev/i2c-1', address: 0x23}, function(err, wire) {
  3. wire.read(2, function(err, res) {
  4. if (!err) {
  5. console.log('read result: ' + res);
  6. }
  7. });
  8. });

i2cbus.readSync(length)

  • length {number} Number of bytes to read.
  • Returns: {Array} Array of bytes.

Read bytes from I2C device synchronously.

Example

  1. var i2c = require('i2c');
  2. var wire = i2c.openSync({device: '/dev/i2c-1', address: 0x23});
  3. var res = wire.readSync(2);
  4. console.log(res);

i2cbus.write(bytes[, callback])

  • bytes {Array} Array of bytes to write.
  • callback {Function}
    • err {Error|null}

Write bytes to I2C device asynchronously.

Example

  1. var i2c = require('i2c');
  2. i2c.open({device: '/dev/i2c-1', address: 0x23}, function(err, wire){
  3. wire.write([0x10], function(err) {
  4. if(!err) {
  5. console.log('write done');
  6. }
  7. });
  8. });

i2cbus.writeSync(bytes)

  • bytes {Array} Array of bytes to write.

Write bytes to I2C device synchronously.

Example

  1. var i2c = require('i2c');
  2. var wire = i2c.openSync({device: '/dev/i2c-1', address: 0x23});
  3. wire.writeSync([0x10]);

i2cbus.close([callback])

  • callback {Function}
    • err {Error|null}

Close I2C device asynchronously.

Example

  1. var i2c = require('i2c');
  2. i2c.open({device: '/dev/i2c-1', address: 0x23}, function(err, wire) {
  3. wire.close();
  4. });

i2cbus.closeSync()

Close I2C device synchronously.

Example

  1. var i2c = require('i2c');
  2. var wire = i2c.openSync({device: '/dev/i2c-1', address: 0x23});
  3. wire.closeSync();