I'm having some issues reading flow sensor data (Sensirion LPG10, to be precise) using the RPi Zero's I2C bus.
I think I have set the bus and wiring up correctly as running 'i2cdetect -y 1' returns the correct device address (0x40) as specified in the sensor's datasheet.
The sensor datasheet specifies that I first write a byte (value 0xf1) to trigger a sensor measurement, then read a byte word (so, 2 bytes for the data, a 3rd for a checksum if I desire) immediately after. This information is in section 4.4 and is specified as the first thing to try.
I manage to detect that the sensor is there on the I2C bus, and I manage to write the first byte - at least, no I/O errors are returned with either the smbus or pigpio modules at this stage.
However, when attempting to read the bytes - whether individually in order, or reading all at once - I get errors. With smbus it's a little complex as read_word_data specifies a register, which isn't provided in the datasheet - pigpio lets me use i2c_read_device(0x40, 2) which should theoretically work, but doesn't.
The code snippets I tried:
# pigpio
addr = 0x40 # Sensor address
pi = pigpio.pi()
handle = pi.i2c_open(1, addr)
pi.i2c_write_byte(handle, 0xf1)
# Here I have tried on and off various time.sleep values
(len, bytes) = pi.i2c_read_device(handle, 2)
# Error handling
if len < 0:
print(pigpio.error_text(len))
pi.i2c_close(handle)
pi.stop()
#smbus
from smbus import SMBus
bus = SMBus(1)
i2cBus.write_byte(0x40, 0xf1)
#Trying to return even a single byte, let alone the 2-byte word, fails
byte = i2cBus.read_byte(0x40) # Fails with I/O error
# I can't find a way to read the 2-byte word with smbus either as it specifies a register/channel which the sensor does not seem to specify
It is worth noting that I have not changed any of the other Raspberry Pi I2C interface settings, other than to ensure it is enabled in raspi-config and rebooting afterwards.
I don't see anything particularly different in their example code (Arduino, though): Example code The only difference I see is the baud rate set to 9600, but their datasheet specified that 100kbps baud rate (I believe the RPi default) is workable so I don't see how this would be the issue, if the datasheet is correct (and it likely is!)
Can anyone figure out where I'm going wrong? Thank you so much in advance, any help is greatly appreciated!
pigs i2co 1 0x40 0
get a handle for the device. Thenpigs i2cwd 0 0xf1
thenpigs i2crd 0 3
.