Skip to content

I2C

I2C is a standard 2 wire protocol to communicate with devices. The Raspberry Pi offers out of the box two I2C interfaces. There is SCLK and SDA signal, i.e. the clock signal and the data signal that is open-collector based input / output. Communicating over I2C works via sending first an 8bit address and read or write bit over the SDA signal and then either write or read data bits.

import { I2C } from 'miletus';

...

let i2c = new I2C();
await i2c.open(0x77);

Methods

new I2C()

Creates a new I2C connection object.

open(address)

  • address number - Address of the I2C device.

Open the connection to the device.

Returns Promise - Resolves when the connection is opened.

close()

Close the connection.

writeByte(address, byte)

  • address number - Register address.
  • byte numer - Value to write.

Write a byte to a register address.

Returns Promise - Resolves when the byte was written.

writeBuffer(buffer, length)

  • buffer ArrayBuffer - Data to be written.
  • length number - The length of the data.

Write a buffer of data to a register address.

Returns Promise - Resolves when the ArrayBuffer was written.

readByte(address)

  • address number - Register address.

Read a byte from a register address.

Returns Promise<number> - Resolves with the byte.

readInt16(address)

  • address number - Register address.

Read a 16bit integer from a register address.

Returns Promise<number> - Resolves with the 16bit integer.

readBuffer(address, length)

  • address number - Register address.
  • length number - Length of the data to read.

Read an ArrayBuffer from a register address with a given length.

Returns Promise<ArrayBuffer> - Resolves with the buffer.