I2C

The i2c module supports communication with I2C (Inter-Integrated Circuit) / TWI (Two Wire Interface) devices. Use require('i2c') to access this module.

  • Slave Mode is NOT supported yet.

Class: I2C

An instances of I2C represents a I2C bus.

I2C.MASTER

  • <number> = 0

I2C.SLAVE

  • <number> = 1

new I2C(bus[, options])

  • bus <number> I2C bus number. This value should be less than board.NUM_I2C

  • options <object> The object of I2C options.

    • mode <number> I2C mode, I2C.MASTER or I2C.SLAVE mode. Default: I2C.MASTER

    • baudrate <number> Clock speed (bit/s) for Master mode. Default: 100000. Maximum speed is 400000.

  • Returns: <object> The return value is I2C object.

Instances of the I2C class can be created using the new keyword or by calling i2c.I2C() as a function. A RangeError will be thrown if bus exceeds board.NUM_I2C

// Javascript example: Create the I2C instance with master mode
var I2C = require('i2c').I2C;
// open bus 0 in master mode
var i2c0 = new I2C(0); // equals to board.i2c(0)
// read or write ...
i2c0.close();
// open bus 1 in master mode, full speed
var i2c1 = new I2C(1, {mode: I2C.MASTER, baudrate: 400000);
// read or write ...
i2c1.close();

i2c.write(data, address[, timeout[, count]])

  • data <ArrayBuffer|TypedArray|number[]|string> Data to write.

  • address <number> I2C slave address. (7bit)

  • timeout <number> Timeout in milliseconds. Default: 5000.

  • count <number> Indicates how many times to write data. Default: 1

  • Returns: <number> The number of bytes written, -1 if it failed to write or timeout.

This method writes data to the specified address (slave device) and returns the number of bytes written. This method can be called only in master mode.

// Javascript example: Create the I2C instance with master mode and write 2byte data to the slave device
var I2C = require('i2c').I2C;
var i2c0 = new I2C(0, {baudrate: 50000}); // master mode 50 kbits/s
// Writes 2 bytes with an array of numbers
var array = [0x6b, 0x00];
i2c0.write(array, 0x68);
// Writes 2 bytes with an instance of TypedArray or ArrayBuffer
var typedArray = new Uint8Array([0x6b, 0x00]);
i2c0.write(typedArray, 0x68); // pass a typed array
i2c0.write(typedArray.buffer, 0x68); // or, pass an array buffer
// Writes 2 bytes with a string
var str = 'abcdef';
i2c0.write(str, 0x68);
i2c0.close();

i2c.read(length, address[, timeout])

  • length <number> Data length to read.

  • address <number> I2C slave address. (7bit)

  • timeout <number> Timeout in milliseconds. Default: 5000.

  • Returns: <ArrayBuffer> An array buffer having data read, null if failed to read.

This method read data from the specified address (slave device) and returns an array buffer object. This method can be called only in master mode.

// Javascript example: Create the I2C instance with master mode and read 14byte data from the slave device
var I2C = require('i2c').I2C;
var i2c0 = new I2C(0);
var buf = i2c0.read(14, 0x68); // Read 14 bytes from the address 0x68.
i2c0.close();
if (buf) {
var data = new Uint8Array(buf);
console.log(data.length); // 14
console.log(data[0]); // first byte
}

i2c.memWrite(memAddress, data, address[, memAddr16bit[, timeout[, count]]])

  • memAddress <number> Memory address to write.

  • data <ArrayBuffer|TypedArray|number[]|string> Data to write.

  • address <number> I2C slave address. (7bit)

  • memAddr16bit <number> set 1 when memAddress is 16bit address. set 0 when memAddress is 8bit address. Default: 0

  • timeout <number> Timeout in milliseconds. Default: 5000.

  • count <number> Indicates how many times to write data. Default: 1

  • Returns:<number> The number of bytes written, -1 if failed to write or timeout.

This method writes data to the memory address in the specified slave device and returns the number of bytes written. This method can be called only in master mode.

// Javascript example: Create the I2C instance with master mode and write 2byte memory data to the slave device
var I2C = require('i2c').I2C;
var i2c0 = new I2C(0); // master mode
// Writes 2 bytes with an array of numbers
var array = [0x6b, 0x00];
i2c0.memWrite(0x10, array, 0x68);
// Writes 2 bytes with an instance of TypedArray or ArrayBuffer
var typedArray = new Uint8Array([0x6b, 0x00]);
i2c0.memWrite(0x10, typedArray, 0x68); // pass a typed array
i2c0.memWrite(0x10, typedArray.buffer, 0x68); // or, pass an array buffer
// Writes strings
var str = 'abcdef';
i2c0.memWrite(0x10, str, 0x68);
i2c0.close();

i2c.memRead(memAddress, length, address[, memAddr16bit[, timeout]])

  • memAddress <number> Memory address to read.

  • length <number> Data length to read.

  • address <number> I2C slave address. (7bit)

  • memAddr16bit <number> set 1 when memAddress is 16bit address. set 0 when memAddress is 8bit address. Default: 0

  • timeout <number> Timeout in milliseconds. Default: 5000

  • Returns: <ArrayBuffer> An array buffer having data read, null if failed to read.

This method read data at memory address from the specified slave device and returns an array buffer object. This method can be called only in master mode.

// Javascript example: Create the I2C instance with master mode and read 14byte memory data from the slave device
var I2C = require('i2c').I2C;
var i2c0 = new I2C(0);
var buf = i2c0.memRead(0x0100, 14, 0x68, 1); // Read 14 bytes at memory address 0x0100 from slave 0x68
i2c0.close();
if (buf) {
var data = new Uint8Array(buf);
console.log(data.length); // 14
console.log(data[0]); // first byte
}

i2c.close()

This method closes the I2C bus.

// Javascript example: Create the I2C instance with master mode and close it.
var I2C = require('i2c').I2C;
var i2c0 = new I2C(0);
// Write or Read from I2C
i2c0.close(); // Close i2c device