Reference
Last revision 2026/01/07
This article provides detailed guidance on installing the SHT library for Arduino, utilizing the I2C communication protocol, and employing the sensor API to obtain temperature and humidity data, set sensor modes, and perform software resets for efficient sensor management.
Library
- SHT Library for Arduino. Installation instructions: Download the library from GitHub, extract the folder to your Arduino Libraries directory, and restart the Arduino IDE. (About how to install the library?)
Communication Protocol Description
The sensor uses the I2C communication interface. The default I2C addresses are 0x44 (SHT40-AD1B) and 0x45 (SHT40-BD1B).
API Description
/**
* @brief 对主控板的IIC进行了初始化
*/
void begin();
/**
* @brief 获取温度数据
* @return 温度值,单位:摄氏度
*/
float getTemperature();
/**
* @brief 获取湿度数据
* @return 湿度值,单位:%RH
*/
float getHumidity();
/**
* @brief 获取温湿度数据
* @param tem 存放温度数据的引用
* @param hum 存放湿度数据的引用
*/
void getTemHum(float &tem, float &hum);
/**
* @brief 设置传感器工作模式
* @param mode 传感器的工作模式
* @n SHTC3:
* @n PRECISION_HIGH_CLKSTRETCH_ON Clock Stretching Enabled
* @n PRECISION_HIGH_CLKSTRETCH_OFF Clock Stretching Disabled
* @n PRECISION_LOW_CLKSTRETCH_ON Clock Stretching Enabled & Low Power
* @n PRECISION_LOW_CLKSTRETCH_OFF Clock Stretching Disabled & Low Power
* @n SHT40:
* @n PRECISION_HIGH_HEATER_OFF measure T & RH with high precision (high repeatability)
* @n PRECISION_MID_HEATER_OFF measure T & RH with medium precision (medium repeatability)
* @n PRECISION_LOW_HEATER_OFF measure T & RH with lowest precision (low repeatability)
* @n PRECISION_HIGH_HEATER_1S activate highest heater power & high precis. meas. (typ. 200mW @ 3.3V) for 1s
* @n PRECISION_HIGH_HEATER_100MS activate highest heater power & high precis. meas. (typ. 200mW @ 3.3V) for 0.1s
* @n PRECISION_MID_HEATER_1S activate medium heater power & high precis. meas. (typ. 110mW @ 3.3V) for 1s
* @n PRECISION_MID_HEATER_100MS activate medium heater power & high precis. meas. (typ. 110mW @ 3.3V) for 0.1s
* @n PRECISION_LOW_HEATER_1S activate lowest heater power & high precis. meas. (typ. 20mW @ 3.3V) for 1s
* @n PRECISION_LOW_HEATER_100MS activate lowest heater power & high precis. meas. (typ. 20mW @ 3.3V) for 0.1s
*/
void setMode(uint16_t mode) ;
/**
* @brief 获取传感器的唯一标识符
* @return 获取成功返回传感器的唯一标识符,失败返回0
*/
uint32_t getDeviceID();
/**
* @brief software reset
*/
void softwareReset() ;
/**
* @brief Obtain raw data of temperature and humidity
* @param temp Pointer to the address of the original value of the temperature
* @param hun Pointer to the address of the original value of the humidity
* @return Is the data obtained correct? return true The data is correct ; return false The data is incorrect
*/
bool getTandRHRawData(uint16_t *temp, uint16_t *hum);
Was this article helpful?
