SNAU265D June   2021  – February 2025 HDC3020 , HDC3020-Q1 , HDC3021 , HDC3021-Q1 , HDC3022 , HDC3022-Q1 , HDC3120 , HDC3120-Q1

 

  1.   1
  2.   Abstract
  3.   Trademarks
  4. 1HDC3x Devices
    1. 1.1 HDC3x2x Package Comparison
    2. 1.2 HDC3020 in WSON
    3. 1.3 HDC3021 in WSON
    4. 1.4 HDC3022 in WSON
    5.     HDC3120 in WSON
  5. 2Storage and Handling Guidelines
    1. 2.1 Exposure to Contaminants
    2. 2.2 Chemical Analysis
      1. 2.2.1 Saturation and Recovery Tests
      2. 2.2.2 Long-Term Exposure
    3. 2.3 Packaging and Storing
      1. 2.3.1 Assembly
      2. 2.3.2 Application in Extreme Environment
  6. 3Programming the HDC3020
    1. 3.1 The Functional Modes
    2. 3.2 Trigger-On Demand
    3. 3.3 Auto Measurement
    4. 3.4 Programming the CRC
      1. 3.4.1 CRC C Code
    5. 3.5 Example Code
    6. 3.6 Condensation Removal
    7. 3.7 Offset Error Correction
      1. 3.7.1 Offset Error Correction Example With a Fingerboard
  7. 4References
  8. 5Revision History

Example Code

Example C code can be found on SysConfg, with ASCStudio: HDC302x Example Code.

The following code provides an example of how to perform a temperature and humidity read on HDC302x with Arduino:

//HDC302x Sample Code v0.2
//Texas Instruments
//HG

#include <Wire.h>

// Data buffer to hold data from HDC Sensor
uint8_t HDC_DATA_BUFF[4];

//Humidity Variables
uint16_t HUM_MSB;
uint16_t HUM_DEC;
uint16_t HUM_OUTPUT;

//Temperature Variables
uint16_t TEMP_MSB;
uint16_t TEMP_DEC;
uint16_t TEMP_OUTPUT;

//Device and Address Configurations

#define DEVICE_ADDR 0x45       //

//Lowest noise, highest repeatability 
#define DEVICE_CONFIG_MSB 0x24 // 
#define DEVICE_CONFIG_LSB 0x00 //

void setup() {

  // put your setup code here, to run once:
  Wire.begin();
  Serial.begin(115200);
  Serial.println("HDC302x Sample Code");

}

void loop() {

  float humidity;
  float temp;

// send device command for highest repeatability 
  Wire.beginTransmission(DEVICE_ADDR);
  Wire.write(0x24); //send MSB of command
  Wire.write(0x00); //command LSB
  Wire.endTransmission();
  delay(25); //wait 25 ms before reading

  Wire.requestFrom(DEVICE_ADDR, 6); //request 6 bytes from HDC device
  Wire.readBytes(HDC_DATA_BUFF, 6); //move 6 bytes from HDC into a temporary buffer

  temp = getTemp(HDC_DATA_BUFF);
  Serial.print("Temp (C): ");
  Serial.println(temp);

  delay(1000);

  humidity = getHum(HDC_DATA_BUFF);
  Serial.print("Humidity (RH): ");
  Serial.print(humidity);
  Serial.println("%");

  delay(1000);

}

float getTemp(uint8_t humBuff[]) {

  float tempConv;
  float celsius;

  TEMP_MSB = humBuff[0] << 8 | humBuff[1]; //shift 8 bits off data in first array index to get MSB then OR with LSB
  tempConv = (float)(TEMP_MSB);
  celsius = ((tempConv / 65535) * 175) - 45; //calculate celcius using formula in data sheet
  
  return celsius; 

}

float getHum(uint8_t humBuff[]){

  float humConv;
  float humidity;

  HUM_MSB = (humBuff[3] << 8) | humBuff[4]; //shift 8 bits off data in first array index to get MSB then OR with LSB
  humConv = (float)(HUM_MSB);
  humidity = (humConv / 65535) * 100; //calculate celcius using formula in datasheet

  return humidity;

}