Skip to main content

i2c.write(deviceAddress, registerPlusData)

Initiates an I²C write to the device at the specified address




Name Type Description
deviceAddress Integer The 8-bit I²C base address
registerPlusData String The I²C sub-address and data, or "" for none


Integer — 0 for success, or an I²C Error Code


The first two characters of the string to be written via the I²C bus are typically used as the most-significant byte and the least-significant byte of the sub-address — typically the device register you wish to access. A peripheral with 8-bit registers needs only a single byte for its address. An empty string, "", may be passed to initiate an empty-payload write phase.

On completion, 0 is returned for success, or non-zero for an error condition. A list of error codes can be found on the i2c.readerror() page.

Please note that many vendors’ device datasheets specify a 7-bit base I²C address. In this case, you will need to bit-shift the address left by 1 (ie. multiply it by 2) to form the 8-bit address required by the imp API:

local impReadyI2Caddress = datasheetI2Caddress << 1;

Example Code

The code below provides an example of using an I²C device. It configures an SA56004X temperature sensor — as found on the Hannah R2 reference design — by requesting the CLOCK_SPEED_400_KHZ clocking rate.