Example Code for Arduino-CHT8305

Last revision 2026/01/24

This article offers an in-depth look at using an Arduino to interface with the CHT8305 temperature and humidity sensor, including necessary hardware, software setup, wiring instructions, and sample code for effective environmental data collection.

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • I2C Temperature & Humidity Sensor (Stainless Steel Shell) x 1

Software Preparation

Wiring Diagram

CHT8305

UNO R3 Pin Sensor Pin
UNO R3 VCC Sensor VCC(RED)
UNO R3 GND Sensor GND(BLACK)
UNO R3 SDA Sensor SDA(YELLOW)
UNO R3 SCL Sensor SCL(WHITE)

Sample Code

#include "Wire.h"
#define address 0x40
char dtaUart[15];
char dtaLen = 0;
uint8_t Data[100] = {0};
uint8_t buff[100] = {0};
void setup()
{
  Serial.begin(9600);
  Wire.begin();
}
uint8_t buf[4] = {0};
uint16_t data, data1;
float temp;
float hum;
void loop()
{
  readReg(0x00, buf, 4);
  data = buf[0] << 8 | buf[1];
  data1 = buf[2] << 8 | buf[3];
  temp = ((float)data * 165 / 65535.0) - 40.0;
  hum =  ((float)data1 / 65535.0) * 100;
  Serial.print("temp(C):");
  Serial.print(temp);
  Serial.print("\t");
  Serial.print("hum(%RH):");
  Serial.println(hum);
  delay(500);
}
uint8_t readReg(uint8_t reg, const void* pBuf, size_t size)
{
  if (pBuf == NULL) {
    Serial.println("pBuf ERROR!! : null pointer");
  }
  uint8_t * _pBuf = (uint8_t *)pBuf;
  Wire.beginTransmission(address);
  Wire.write(&reg, 1);
  if ( Wire.endTransmission() != 0) {
    return 0;
  }
  delay(20);
  Wire.requestFrom(address, (uint8_t) size);
  for (uint16_t i = 0; i < size; i++) {
    _pBuf[i] = Wire.read();
  }
  return size;
}
 

Result

Was this article helpful?

TOP