RAW I2C Access help with SHT30 (ENVIII) please.



  • Hello most senior members of the forum, I beg for you to help me with an I2C communication using the SHT30 temperature and humidity sensor.

    0_1675504110806_bowing-asian-business-person-260nw-1249507468.jpg
    I'm trying to write a tutorial on I2C communications but struggling with the SHT30. So far I am using the following functions to test :

    from umachine import Pin, I2C,
    import utime
    i2c=I2C(0,scl=Pin(0), sda=Pin(1), freq=400000)
    i2c.scan()# looks for the address 68 (0x44) Primary and 69 (0x45) secondary. and 112 (0x70)
    [hex(x) for x in i2c.scan()]
    i2c.writeto(0x44, b'\x2C\x06') # High repeatability with clock stretching.
    utime.sleep(1)
    
    
    result = bytearray(2)
    i2c.readfrom_into(0x44, result)
    result #= 0x87 aka 135
    # 0xFFFF = 65535
    # 0xFFF = 4095
    def temp_c(data):
        # t_celsius = (((data[0] << 8 |  data[1]) * 175) / 0xFFFF) - 45 + self.delta_temp;
        value = data[0] << 8 | data[1]
        temp = (value & 0xFFF) / 16.0
        if value & 0x1000:
                temp -= 256.0
        return temp
        
    temp_c(result)
    

    But I'm getting bad results from the sensor. I have a feeling it could be to do with the conversion formula but maths is not my strong point. can someone please help me?



  • @ajb2k3
    You can do everything in UIFlow !!
    The first part of the sketch uses the M5Stack ENVII library and displays the values on a M5Stack core..
    The second part uses pure I2C commands setting the SHT30 registers (0x2c,0x06) and then reads the 6 Raw data bytes into the "Result" list (temperature MSB, temperature LSB, temperature CRC, humidity MSB, humidity LSB, humidity CRC).
    The the MSB and LSB temperature data are then converted to degrees Celsius accorfung to the SHT data sheet.

    0_1675813790190_bfccd30f-3df4-4205-998d-f79680b18da5-image.png

    The corresponding Python Code looks like this:

    0_1675813969356_38b070f0-918c-4472-98eb-c6ca67041d29-image.png



  • @crami25 Normally yes but, the M5Stamp C3 and C3U are not supported buy UIFlow hence why I’m using mainstream MP



  • @ajb2k3 Hi Adam, just an "aside" from a lurker...I could not get the M5Stack ENV to work with C3, using their library. It seems the "get" command in the library may be deprecated or Espressif did not bring it forward for risc-v.
    So, I moved the i2c logic into my main loop:
    (This is Arduino C, but looks very similar to the Python version!!!)

    Wire.beginTransmission(0x44); //0x44 for M5Stack ENV (0x45 is DFRobot)
    Wire.write(0x2C); //show all the "wheels and gears"
    Wire.write(0x06);
    Wire.endTransmission();
    delay(50);
    Wire.requestFrom(0x44, 2);
    data[0] = Wire.read();
    data[1] = Wire.read();
    delay(50);
    cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45;
    Serial.println(cTemp);
    delay(1000);

    Cheers, Terry



  • @teastain I have had a few issues with the SHT30 sensor.
    For a start, Hardware I2C is broken in micropython but SoftI2C works fine.
    On a core2 I had to use the PaHub to get the SHT30 to read.

    I have solved my initial problem in that it was the conversion formula I had wrong.