My sensor device has I2C interface. I need some help in I2C writing and reading back from the same register.
Here is my code.
import board
import busio
# Initialize
i2c = busio.I2C(board.SCL, board.SDA)
# Device Address
Device_Address = 0x40
######################################################
## Writing and Reading Alert Limit Channel-1
######################################################
# Alert Limit Channel-1 REG
ALERT_LIMIT_CH1_REG = 0x07
# Wait until the I2C bus is locked
while not i2c.try_lock():
pass
try:
# Write to the device specific register to write.
i2c.writeto(Device_Address, bytes([ALERT_LIMIT_CH1_REG]))
# Create a bytearray buffer of two bytes to store the data.
in_buf = bytearray(2)
out_buf = bytearray(2)
# Initialize in_buf
in_buf[0] = 0xEE
in_buf[1] = 0xFF
# Print the in buffer data.
print("The Alert Limit CH 1 will be set to = 0x", in_buf.hex())
i2c.writeto_then_readfrom(Device_Address, out_buf, in_buf, out_start=0, out_end=None, in_start=0, in_end=None)
# Print the out buffer data.
print("The Alert Limit CH 1 is set to = 0x", out_buf.hex())
# Unlock the bus
finally:
i2c.unlock()
I basically want to perform I2C write operation and read back from same register to confirm the data is written correctly.
The sequence will be first send device address, then register address within the device, then actual data that I would like to write to the resister. I guess there is something wrong in my code. Please have a look and let me know the issue.