Master Read/Write

Initial Settings

To set master mode when starting I2C by i2c.start(), specify master for mode and specify communication frequency. The frequency is generally 100kHz or 400kHz in I2C.
The pull-up with a “small” resistor is needed to communicate at high speed. The setting is as shown below to use I2C with only internal pull-up without an external resistor, although this is not recommended.

obniz.i2c0.start({mode:"master", sda:0, scl:1, clock:400000, pull:"5v"}); 

Write

i2c.write() sends data to device, using addresses and array data. In the program below, the address is 0x50. If writing fails, such as when the connection is not established with the other party, or when other party refuses (NAK), the function set in onerror.

obniz.i2c0.start({mode: "master",sda:2, scl:3, clock:400000, pull:null});
obniz.i2c0.onerror = function(err) {
  console.error(err)
}
obniz.i2c0.write(0x50, [0x00, 0x00, 0x12]);

Read

i2c.readWait() reads data from the device. Set the length of data to read along with the address of the other device. When reading successfully done, it returns data, of which the length is as specified. In case of communication error, onerror is called as well as writing.

obniz.i2c0.start({mode: "master",sda:2, scl:3, clock:400000, pull:null});
obniz.i2c0.onerror = function(err) {
  console.error(err)
}
var ret = await obniz.i2c0.readWait(0x50, 1);
console.log("read "+ret);