Circuits.I2C :: reading sequentially addressed registers?

Using popular Pressure, Temperature sensor LPS25H and Circuits.I2C
Seems I either don’t understand the Circuits.I2C API or don’t understand the I2C sensor device :expressionless:

My code has to do single-byte register transfers rather than reading 3 sequentially addressed registers to get the latest Pressure value : e.g. for device at bus address 0x5d and the LSByte of the Pressure value in Register 0x28 this is what I see :

Read each byte value seperately

iex(fb2@fb2-e53f.local)24> Circuits.I2C.write_read ph_ref, 0x5d, <<0x28>>, 1
{:ok, <<241>>}
iex(fb2@fb2-e53f.local)25> Circuits.I2C.write_read ph_ref, 0x5d, <<0x29>>, 1
{:ok, <<190>>}
iex(fb2@fb2-e53f.local)26> Circuits.I2C.write_read ph_ref, 0x5d, <<0x2a>>, 1
{:ok, "?"}


Try reading the 3 registers in one go

iex(fb2@fb2-e53f.local)28> Circuits.I2C.write_read ph_ref, 0x5d, <<0x28>>, 3
{:ok, <<241, 241, 241>>}
iex(fb2@fb2-e53f.local)29>

Thanks !

1 Like

Normally what you’re doing would work since I2C devices usually auto-increment the destination register on multi-byte reads. It looks like the LPS25H does something special, though. Take a look at section 5.2.1 of the datasheet. It looks like if you set the high bit of the register that you’re interested in, it will auto-increment, so try this:

iex> Circuits.I2C.write_read ph_ref, 0x5d, <<0xa8>>, 3
7 Likes

@fhunleth, thanks for being my helpful eyes once again . . works as desired now.

      {:ok, <<pressure::size(24)-little>>} = I2C.write_read state.lps25hb_ref, state.lps25hb_i2c_addr, <<0xa8>>, 3
1 Like