1

Reading an SHT25 humidity + T sensor. On the Pi, the resolution is bad (~0.5% RH steps). Code (active section):

bus = smbus.SMBus(1)
...
# Send humidity measurement command
bus.write_byte(0x40, 0xF5)
time.sleep(0.5)
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
humidity = data0 * 256 + data1
humidity = -6 + ((humidity * 125.0) / 65536.0)

Values (unique): 40.62 40.12 39.63 39.14

... as you can see, sporadic huge gaps.

Using the same sensor on an Arduino, with the equivalent commands:

Wire.beginTransmission(0x40);
Wire.write(0xF5);
Wire.endTransmission();
delay(500);
Wire.requestFrom(Addr, 2);
data[0] = Wire.read();
data[1] = Wire.read();
humidity = (((data[0] * 256.0 + data[1]) * 125.0) / 65536.0) - 6;

I get very smooth RH data with tiny transitions between adjacent values:

35.35 35.32 35.29 35.25 35.22 35.19 35.12 35.09

...and so on. It seems that the Pi is getting bleed-though from the MSB of the answer?? I notice that the MSB seems to jump around more on the Pi, almost as if there's a framing problem?

Note this (MSB, LSB):

94  121
94  120
94  120
93  119
93  119
93  118
93  118
93  117
93  117
93  116
93  116
92  115
92  115
92  115

... as you can see, the LSB (2nd value) goes gracefully down BUT the MSB switches, probably explaining the big gaps. This does not happen with the Arduino code. The LSB has to go all the way down to near zero before the MSB decrements.

I have specified an i2c speed of 100 kbaud. Curiously, if I ask:

sudo cat /sys/module/i2c_bcm2708/parameters/baudrate

...the answer is zero. wut.

I would be most grateful for any suggestions on what I'm doing wrong. Thanks in advance!

EDIT:

Tried using the read_word variant but it has to send a command number, which confuses the SHT25.

Have now switched to using the SHT31, and communicated with it successfully using

bus.write_i2c_block_data(0x44, 0x2C, [0x06])
time.sleep(0.02)
# Temp MSB, Temp LSB, Temp CRC, Humidity MSB, Humidity LSB, Humidity CRC
data = bus.read_i2c_block_data(0x44, 0x00, 6)

etc.

HT to ericksonp for the code! The SHT25 is just a bit too "odd" in its i2c implementation to work easily with the smbus library...

1 Answer 1

1

I2C is not the same as SMBus.

Try using the SMBus read word command rather than two read byte commands.

You may need to swap the bytes in the word when received.

That will be closer to what you are doing on the Arduino.

4
  • I think part of joan's point here is you are making two separate system calls on a multi-tasking OS vs. two sequential operations on an realtime IC. Of course the latter is more predictable. You want the OS to do the read with one call to hardware.
    – goldilocks
    Jan 21, 2017 at 14:46
  • 1
    @goldilocks It's more to do that the SHT25 is an I2C device and the datasheet says it needs a start condition + control byte + read three bytes + stop condition. I think the closest you can get with an SMBus command is start condition + control byte + read two bytes + stop condition. The SHT25 expects to send the third byte but will abort when it sees the stop condition. The first two read bytes should be okay.
    – joan
    Jan 21, 2017 at 15:04
  • Excellent suggestion! The fly in the ointment is that read_word_data requires a command. I tried "temp = bus.read_word_data( 0x40, 0 )" but got "IOError: [Errno 5] Input/output error"... and the command argument is required. Any more suggestions? Thanks for the help so far! Jan 21, 2017 at 20:14
  • I'd forgotten the SMBus multi-byte commands required a register address. You may need to use I2C. I can suggest a quickish test with my pigpio. Firstly sudo pigpiod to if it is installed. If so pigs i2co 1 0x40 0 to get a handle to the sensor. Then pigs i2cwd 0 0xF5 mils 100 i2crd 0 2 to get a reading. The final two bytes printed should be the reading. If that works repeat the reading and see if it is consistent.
    – joan
    Jan 21, 2017 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.