14. I2C
The I2C object is a built-in object that provides I2C communication functions.
Functional overview:
- Provides I2C master mode function.
Abstracts
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
new I2C() | Creates an I2C instance. | 01.00.00+ |
{I2C} Instance
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
.open() | Starts the communication. | 01.00.00+ | |
.close() | Ends the communication. | 01.00.00+ | |
.write() | Writes the data. | 01.00.00+ | |
.read() | Reads the data. | 01.00.00+ | |
.writeRead() | Reads the data after writing the data. | 01.00.00+ |
Details
new I2C(nodeNo)
Creates an I2C instance.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
nodeNo | number | mandatory | Specifies the node number of the I2C interface to be used. Please refer to Pinout. | |
return | {I2C} | - | {I2C} : Generated {I2C} |
.open(baudrate)
Starts the communication.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
baudrate | number | mandatory | Communication speed Specify the communication baudrate [bps]. Range: 100000, 400000 | |
return | boolean | - | true: Success false: Failure |
.close()
Ends the communication.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | boolean | - | true: Success false: Failure |
.write(slaveAddr,buff)
Writes the data.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
slaveAddr | number | mandatory | Slave address of the I2C device Range: 0 - 127 (7 bit) | |
buff | ArrayBuffer | mandatory | Write data ArrayBuffer size will be written. | The size that can be written at a time is 255 bytes. |
return | boolean | - | true: Success false: Failure |
When the data length is 1 byte, use Uint8Array as follows.
var buff = new ArrayBuffer(1);
var arr = new Uint8Array(buff);
arr[0] = your_data;
var ret = i2c.write(slave_address, buff);
.read(slaveAddr,len)
Reads the data.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
slaveAddr | number | mandatory | Slave address of the I2C device Range: 0 - 127 (7 bit) | |
len | number | mandatory | Specify the read data length. | The maximum size that can be read at a time is 255 bytes. |
return | Arraybuffer | - | Read data If read function fails, the returned size will be 0. |
.writeRead(slaveAddr,buff,len)
Reads the data after writing the data.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
slaveAddr | number | mandatory | Slave address of the I2C device Range: 0 - 127 (7 bit) | |
buff | ArrayBuffer | mandatory | Write data ArrayBuffer size will be written. | The size that can be written at a time is 255 bytes. |
len | number | mandatory | Specify the read data length. | The maximum size that can be read at a time is 255 bytes. |
return | Arraybuffer | - | Read data If the reading fails, the returned size will be 0. |
Object Usage Examples
Sample 1
This sample is an I2C device with a slave address of 0x3A, which writes 0x03 to register 0x05, and then reads 1 byte from register 0x06.
var NODE_NO = 0;
var BAUDRATE = 400000;
var SLAVE_ADDR = 0x3A; //slave address
//I2C write function
var write = function(i2c, reg, data) {
var buf = new ArrayBuffer(2);
var arr = new Uint8Array(buf);
arr[0] = reg;
arr[1] = data;
var ret = i2c.write(SLAVE_ADDR, buf);
if (!ret) print('I2C write error');
return ret;
};
//I2C read function
var read = function(i2c, reg) {
var buf = new ArrayBuffer(1);
var arr = new Uint8Array(buf);
arr[0] = reg;
if(!i2c.write(SLAVE_ADDR, buf)) {
print('I2C read error');
return 0;
}
var retBuf = i2c.read(SLAVE_ADDR, 1);
if (!retBuf.byteLength) print('I2C read error2');
var ret = new Uint8Array(retBuf);
return ret[0];
};
var i2c = new I2C(NODE_NO);
i2c.open(BAUDRATE);
write(i2c, 0x05, 0x03);
read(i2c, 0x06);
i2c.close();
Sample 2
This sample is an I2C device with a slave address of 0x3A, which writes 0x03 to register 0x05, and then reads 1 byte.
var NODE_NO = 0;
var BAUDRATE = 400000;
var SLAVE_ADDR = 0x3A; //slave address
//I2C writeRead function
var writeRead = function(i2c, reg, data) {
var buf = new ArrayBuffer(2);
var arr = new Uint8Array(buf);
arr[0] = reg;
arr[1] = data;
var retBuf = i2c.writeRead(SLAVE_ADDR, buf, 1);
if (!retBuf.byteLength) print('I2C write read error');
var ret = new Uint8Array(retBuf);
return ret[0];
};
var i2c = new I2C(NODE_NO);
i2c.open(BAUDRATE);
writeRead(i2c, 0x05, 0x03);
i2c.close();
Updated: 2020-09-18