Python function calculates SHT1x humidity sensor check sum

When reading the measured humidity or temperature value from the SHT1x Sensirion humidity sensor one may also read the optional check sum. This check sum can then be used to validate the read value. This python function does this operation. See also a C-source alternaltive.

The check sum calculation procedure is given in the Sensirion application note CRC Calculation Humidity Sensors SHT1x/SHT7x.

First, some constant definitions. revbits is an array of four bit constants which are bit reversed of the index. The crc table is a list of pre calculated crc values, taken from the application note.

revbits= (0, 8, 4, 0xc,2, 0xa, 6, 0xe, 1, 9, 5, 0xd, 3, 0xb, 7, 0xf) 

crctable= (0, 49, 
98, 83, 196, 245, 166, 151, 185, 136, 219, 
234, 125, 76, 31, 46, 67, 114, 33, 16, 135, 
182, 229, 212, 250, 203, 152, 169, 62, 15, 
92, 109, 134, 183, 228, 213, 66, 115, 32, 
17, 63, 14, 93,108, 251, 202, 153, 168, 
197, 244, 167, 150, 1, 48, 99, 82, 124, 77, 
30, 47, 184, 137, 218, 235, 61, 12, 95, 
110, 249, 200, 155, 170, 132, 181, 230, 
215, 64, 113, 34, 19, 126, 79, 28, 45, 186, 
139, 216, 233, 199, 246, 165, 148, 3, 50, 
97, 80, 187, 138, 217, 232, 127, 78, 29, 
44, 2, 51, 96, 81, 198, 247, 164, 149, 248, 
201, 154, 171, 60, 13, 94, 111, 65, 112, 
35, 18, 133, 180, 231, 214, 122, 75, 24,
41, 190, 143, 220, 237, 195, 242, 161, 144,
7, 54, 101, 84, 57, 8, 91, 106, 253, 204,
159, 174, 128, 177, 226, 211, 68, 117, 38,
23, 252, 205, 158, 175, 56, 9, 90, 107, 69,
116, 39, 22, 129, 176, 227, 210, 191, 142,
221, 236, 123, 74, 25, 40, 6, 55, 100, 85,
194, 243, 160, 145, 71, 118, 37, 20, 131,
178, 225, 208, 254, 207, 156, 173, 58, 11,
88, 105, 4, 53, 102, 87, 192, 241, 162,
147, 189, 140, 223, 238, 121, 72, 27, 42,
193, 240, 163, 146, 5, 52, 103, 86, 120,
73, 26, 43, 188, 141, 222, 239, 130, 179,
224, 209, 70, 119, 36, 21, 59, 10, 89, 104,
255, 206, 157, 172)

The checkCrc function gets the command used to read the value from the sensor, 5 for humidity and 3 for temperature, the 16 bit value and the 8 bit crc read from the sensor. The function returns 1 if the value is correct, that is the calculated and given crcs are equal.

The rev8 function reverses the bit order of a byte by looking up the reversed value of each 4 bit nibble.

def rev8 (b):
    r= 0
    for i in range(0,2):
        r= r << 4         r= r | revbits[b & 0xf]         b= b >> 4
    return r

def checkCrc(cmd, v, crc):
    msb= (v>>8) & 0xff
    lsb= v & 0xff
    
    r= cmd
    r= crctable[r]
    r= r ^ msb
    r= crctable[r]
    r= r ^ lsb
    r= crctable[r]
    s= rev8(r)
    if crc==s: return 1
    return 0

This assumes that the status byte used during measurement is the one set at power on default, where the lower four bits are zero. If this is not the case, include the status value in the calculation as described in the application note.

0 Responses to “Python function calculates SHT1x humidity sensor check sum”


  • No Comments

Leave a Reply