Skip to main content, registerAddress, numberOfBytes)

Initiates an I²C read from a specific register within a specific device




Name Type Description
deviceAddress Integer The 8-bit I²C base address
registerAddress String The I²C sub-address, or "" for none
numberOfBytes Integer The number of bytes to read from the bus


String — the characters read from the I²C bus, or null on error


This method initiates a read of numberOfBytes of data from the device identified by the base address and the device register you wish to access (sub-address). An empty string, "", may be passed to skip the sub-address write phase; otherwise, the first two characters of the string are used as the most-significant byte and the least-significant byte, respectively.

On successful completion, the data read is returned as a string. If there is a read error, the method returns null, not the empty string "". In the event of an error, you can call i2c.readerror() to diagnose the fault. i2c.readerror() is only available with impOS™ release 30 onwards.

As with any Squirrel string, the result can be indexed using [] as if it were an array of bytes, or by using Squirrel’s string.slice() delegate method.

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 bit (ie. multiply it by 2) to

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.