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.
An instances of I2C
represents a I2C bus.
<number>
= 0
<number>
= 1
bus
<number>
I2C bus number.
options
<object>
The object of I2C options. Default values are depends on board (Check in a board page).
mode
<number>
I2C mode, I2C.MASTER
or I2C.SLAVE
mode. Default: I2C.MASTER
baudrate
<number>
Clock speed (bit/s) for Master mode.
scl
<number>
SCL pin number.
sda
<number>
SDA pin number.
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 max bus number.
// Create the I2C instance with master modevar I2C = require('i2c').I2C;// open bus 0 in master modevar i2c0 = new I2C(0);// read or write ...i2c0.close();// open bus 1 in master mode, full speedvar i2c1 = new I2C(1, {mode: I2C.MASTER, baudrate: 400000);// read or write ...i2c1.close();
data
<Uint8Array|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.
var I2C = require('i2c').I2C;var i2c0 = new I2C(0, {baudrate: 50000}); // master mode 50 kbits/s// Writes 2 bytes as a Uint8Arrayvar array = new Uint8Array([0x6b, 0x00]);i2c0.write(array, 0x68);// Writes 5 bytes as a stringi2c0.write('Hello', 0x68);i2c0.close();
length
<number>
Data length to read.
address
<number>
I2C slave address. (7bit)
timeout
<number>
Timeout in milliseconds. Default: 5000
.
Returns: <Uint8Array>
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.
var I2C = require('i2c').I2C;var i2c0 = new I2C(0);// Read 14 bytes from the address 0x68.var buf = i2c0.read(14, 0x68);if (buf) {console.log(data.length); // 14console.log(data[0]); // first byte}i2c0.close();
memAddress
<number>
Memory address to write.
data
<Uint8Array|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.
var I2C = require('i2c').I2C;var i2c0 = new I2C(0);// Writes 2 bytes at memory address 0x10 of slave 0x68var array = new Uint8Array([0x6b, 0x00]);i2c0.memWrite(0x10, array, 0x68);// Writes 5 bytes as a stringi2c0.memWrite('Hello', 0x68);i2c0.close();
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: <Uint8Array>
A 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.
var I2C = require('i2c').I2C;var i2c0 = new I2C(0);// Read 14 bytes at memory address 0x0100 from slave 0x68var buf = i2c0.memRead(0x0100, 14, 0x68, 1);if (buf) {console.log(data.length); // 14console.log(data[0]); // first byte}i2c0.close();
This method closes the I2C bus.
var I2C = require('i2c').I2C;var i2c0 = new I2C(0);// Write or read from I2C ...i2c0.close(); // Close i2c device