SBOA631 August 2025 HDC1010 , HDC1080 , HDC2010 , HDC2021 , HDC2022 , HDC2080 , HDC3020 , HDC3020-Q1 , HDC3021 , HDC3021-Q1 , HDC3022 , HDC3022-Q1 , HDC3120 , HDC3120-Q1
Performing CRC checks on the HDC302x's measurement output can be an important step to ensure data integrity and prevent false readings, particularly in critical applications such as medical devices, cold chain, or weather stations. This section provides a simple code example that can be readily implemented into the existing code for the HDC302x. This example specifically focuses on performing a CRC check on the temperature and humidity readings.
The HDC3020 follows the CRC-8 standard as it outputs a unique 8-bit CRC value for temperature and humidity measurements. This is illustrated above in Figure 2-8. Depending on whether the user wants to check humidity or temperature data, the algorithm below accepts a total of three bytes—MSB, LSB and CRC as a parameter and the total number of bytes sent. It then performs a calculation using the specified polynomial value from the datasheet, 0x31. Equation 1 illustrates the polynomial used for the CRC check calculation.
Once both measurement data and the CRC byte are processed, if the final value is 0x00, the CRC check has passed and the program proceeds to output the measurement readings; else, it outputs an error message in the console.
// function for checking CRC for HDC measurements
uint8_t checkMeasurementCRC(uint8_t data[], uint8_t dataLength){
uint8_t crc = 0xFF; // initial value per HDC302x datasheet
uint8_t byte;
uint8_t bit;
for (byte =0; byte < dataLength; byte++){ // loops through each byte of input data
crc ^= data[byte]; // XOR next data byte into current CRC value
for (bit = 0; bit < 8; bit++){ // process each bit from the data byte
if (crc & 0x80) // if MSB of CRC is 1
crc = (crc << 1) ^ 0x31; // shift left and apply polynomial
else
crc = (crc << 1); // else shift left, but no polynomial application
}
}
Serial.print("CRC Check Result: "); // optional; prints CRC value for debugging
Serial.println(crc);
return crc; // return final CRC value
} This function can then be called while temperature or humidity is read to check data integrity.
Humidity Read Example w/ CRC Check:
uint8_thumCheck[3] = {HDC_DATA_BUFF[3], HDC_DATA_BUFF[4], HDC_DATA_BUFF[5]};
// if algorithm output equals final byte value of 0x00, CRC check passes, else output error message
if ((checkMeasurementCRC(humCheck, 3)) == 0x00){
Serial.println("Humidity CRC check passed.");
humidity = getHum(HDC_DATA_BUFF);
Serial.print("Humidity (RH): ");
Serial.print(humidity);
Serial.println("%");
} else {
Serial.println("Error: Humidity CRC Check Failed.");
}A sample output is provided below in Figure 2-9. The same method can be implemented for checking temperature data integrity.
Figure 2-9 CRC Check Example
OutputAn example for checking Alert CRC in C can be found in TI's GUI-based code generator, ASC Studio.