Example Code for Arduino-Read Temperature and Humidity
Last revision 2026/01/07
Record program and read the current temperature and humidity data. The program measures temperature and humidity every 1 second, prints the data to the serial port, and activates the sensor's heater if the humidity exceeds 80%RH. Users can learn how to interface the SHT40 sensor with Arduino, read sensor data, and use the heater function for extreme humidity conditions.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- SHTC3 Digital Temperature and Humudity Sensor x 1
- M-M/F-M/F-F Jumper wires
Software Preparation
- Arduino IDE
- Download and install the SHT Library. (About how to install the library?)
Wiring Diagram

Other Preparation Work
- Install the Arduino IDE and the SHT Library as per the Software Preparation section.
- Connect the hardware according to the Wiring Diagram.
- Open the Arduino IDE and create a new sketch.
- Include the
DFRobot_SHT40library in your sketch. - Ensure the sensor is connected correctly; the program will retrieve the sensor's device ID during setup—if retrieval fails, recheck the connection.
Sample Code
/*!
* @file temperatureAndHumidity.ino
* @brief Measurement of temperature and humidity
* @n 本传感器可测量温湿度数据,温度的测量范围在-40~125 ℃ ,湿度的测量范围在 0~100 %RH。
* @n 本传感器提供高、中、低,三种测量精度,还提供了高、中、低,三种加热功率对片上加热器进行加热。
* @n 使用加热器的主要情景:
* @n 1、Removal of condensed / spray water on the sensor surface. Although condensed water is not a reliability / quality problem to the sensor, it will however make the sensor nonresponsive to RH changes in the air as long as there is liquid water on the surface.
* @n 2、Creep-free operation in high humid environments. Periodic heating pulses allow for creepSTXfree high-humidity measurements for extended times.
* @n 实验现象 每次1秒钟测量一次温湿度数据并且通过串口打印,当湿度超过80%RH时,启动一次加热器。
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [yangfeng]<[email protected]>
* @version V1.0
* @date 2021-03-19
* @get from https://www.dfrobot.com
* @url https://github.com/DFRobot/DFRobot_SHT
*/
#include"DFRobot_SHT40.h"
/**
* 对于SHT40传感器这里支持两种型号,第一种是SHT40_AD1B, 第二种是SHT40_BD1B,这两种型号对应不同的IIC设备地址,这里对它们进行了封装,用户可传入如下两种参数
* 型号为SHT40_AD1B的传感器使用: SHT40_AD1B_IIC_ADDR
* 型号为SHT40_AD1B的传感器使用: SHT40_BD1B_IIC_ADDR
*/
DFRobot_SHT40 SHT40(SHT40_AD1B_IIC_ADDR);
uint32_t id = 0;
float temperature, humidity;
void setup() {
Serial.begin(9600);
SHT40.begin();
while((id = SHT40.getDeviceID()) == 0){
Serial.println("ID retrieval error, please check whether the device is connected correctly!!!");
delay(1000);
}
delay(1000);
Serial.print("id :0x"); Serial.println(id, HEX);
}
void loop() {
/**
* mode 用来配置传感器的工作模式
* PRECISION_HIGH measure T & RH with high precision (high repeatability)
* PRECISION_MID measure T & RH with medium precision (medium repeatability)
* PRECISION_LOW measure T & RH with lowest precision (low repeatability)
*/
temperature = SHT40.getTemperature(/*mode = */PRECISION_HIGH);
/**
* mode 用来配置传感器的工作模式
* PRECISION_HIGH measure T & RH with high precision (high repeatability)
* PRECISION_MID measure T & RH with medium precision (medium repeatability)
* PRECISION_LOW measure T & RH with lowest precision (low repeatability)
*/
humidity = SHT40.getHumidity(/*mode = */PRECISION_HIGH);
if(temperature == MODE_ERR){
Serial.println("Incorrect mode configuration to get temperature");
} else{
Serial.print("Temperature :"); Serial.print(temperature); Serial.println(" C");
}
if(humidity == MODE_ERR){
Serial.println("The mode for getting humidity was misconfigured");
} else{
Serial.print("Humidity :"); Serial.print(humidity); Serial.println(" %RH");
}
/**
* mode 用来配置传感器的工作模式
* PRECISION_HIGH measure T & RH with high precision (high repeatability)
* PRECISION_MID measure T & RH with medium precision (medium repeatability)
* PRECISION_LOW measure T & RH with lowest precision (low repeatability)
*/
//if(SHT40.getTemHum(temperature,humidity,PRECISION_HIGH)){
// Serial.print("Temperature :"); Serial.print(temperature); Serial.println(" C");
// Serial.print("Humidity :"); Serial.print(humidity); Serial.println(" %RH");
//} else{
// Serial.println("Pattern configuration error");
//}
if(humidity > 80){
/**
* mode 用来配置传感器的工作模式
* POWER_CONSUMPTION_H_HEATER_1S activate highest heater power & high precis. meas. for 1s
* POWER_CONSUMPTION_H_HEATER_100MS activate highest heater power & high precis. meas. for 0.1s
* POWER_CONSUMPTION_M_HEATER_1S activate medium heater power & high precis. meas. for 1s
* POWER_CONSUMPTION_M_HEATER_100MS activate medium heater power & high precis. meas. for 0.1s
* POWER_CONSUMPTION_L_HEATER_1S activate lowest heater power & high precis. meas. for 1s
* POWER_CONSUMPTION_L_HEATER_100MS activate lowest heater power & high precis. meas. for 0.1s
*/
SHT40.enHeater(/*mode = */POWER_CONSUMPTION_H_HEATER_1S);
}
delay(1000);
Serial.println("----------------------------------------");
}
Result
Open the serial port to display the current temperature and humidity data.

Additional Information
- The sensor supports two I2C addresses:
SHT40_AD1B_IIC_ADDR(for SHT40-AD1B) andSHT40_BD1B_IIC_ADDR(for SHT40-BD1B). Ensure you use the correct address for your sensor model. - The heater function is used to remove condensed water from the sensor surface or enable creep-free operation in high-humidity environments. Refer to the sensor datasheet for more details on heater usage.
Was this article helpful?
