简介
该SHT31温湿度传感器拥有一个防风雨的外壳,并且内部也做了防水处理,可以让您的户外项目更加安全,虽然传感器防风雨,但最好不要直接浸泡在水中,如果需要浸泡在水中建议DS18B20是您不错的选择。
该传感器采用了Sensirion公司推出的新一代SHT31温湿度传感器芯片,该芯片拥有极高的可靠性和出色的长期稳定性,具有功耗低、反应快、抗干扰能力强等优点。传感器采用I2C通讯,兼容3.3V/5V。
SHT31在SHT3x系列中属于标准版,相比上一代精度更高。传感器在0%RH~100%RH(25℃时)误差仅为±2%RH,传感器在0℃-90℃(典型值)误差仅为±0.2℃。
注意:虽然传感器温度测量范围为-40~125℃,但是由于传感器使用了PE材料外壳,因此我们建议不要将传感器放在温度高于80℃的地方。
温湿度偏差参考:
技术规格
- 工作电压:3.3~5V
- 工作电流:<1.5mA
- 湿度测量精度:±2%RH
- 湿度测量范围:0-100%RH
- 温度测量精度:±0.2℃
- 温度测量范围:-40-125℃
- 通信方式:I2C
- 电缆长度:大约1m
引脚说明
颜色 | 名称 | 功能描述 |
---|---|---|
红色 | VCC | 电源正极 |
黑色 | GND | 电源负极 |
绿色 | SDA | 数据线 |
黄色 | SCL | 时钟线 |
使用教程
注意:该传感器的I2C地址是0x44,在使用时请注意I2C地址是否正确
准备
- 硬件
- 1 x Arduino UNO控制板
- 1 x SHT31温湿度传感器(防风雨)
- 若干 杜邦线
- 软件
- Arduino IDE, 点击下载Arduino IDE
- SHT3x库文件和示例程序。
关于如何安装库文件,点击链接
- 主要API接口函数列表
/**
* @brief 获取测量到的温度(单位:摄氏度)
* @return 返回float类型的温度数据
*/
float getTemperatureC();
/**
* @brief 获取测量到的温度(单位:华氏度)
* @return 返回float类型的温度数据
*/
float getTemperatureF();
/**
* @brief 获取测量到的湿度(单位:%RH)
* @return 返回float类型的湿度数据
*/
float getHumidityRH();
接线图
样例代码1 - 单次测量温湿度模式
单次测量模式:控制板发送一次采集命令,传感器就去采集一次数据。
此模式可以根据需要去读取数据,功耗较低。
/*!
* @brief Construct the function
* @param pWire I2C bus pointer object and construction device, can both pass or not pass parameters, Wire in default.
* @param address Chip I2C address, two optional addresse.
*/
/*!
* @file singleMeasurement.ino
* @brief Read ambient temperature (C/F) and relative humidity (%RH) in single-read mode.
* @n Experimental phenomenon: the chip defaults in this mode, we need to send instructions to enable the chip collect data,
* which means the repeatability of the read needs to be set (the difference between the data measured by the chip under the same measurement conditions)
* then read the temperature and humidity data and print the data in the serial port.
* @n Single measure mode: read data as needed, power consumption is relatively low, the chip idle state only costs 0.5mA.
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [fengli](li.feng@dfrobot.com)
* @version V1.0
* @date 2019-08-21
* @get from https://www.dfrobot.com
* @url https://github.com/DFRobot/DFRobot_SHT3x
*/
#include <DFRobot_SHT3x.h>
/*!
* @brief Construct the function
* @param pWire I2C bus pointer object and construction device, both can pass or not pass parameters,
* Wire in default.
* @param address Chip I2C address, two optional addresses 0x44 and 0x45(0x45 in default).
* @param RST RST Chip reset pin, 4 in default.
* @n I2C address is determined by the pin addr on the chip.
* @n When the ADR is connected to VDD, the chip I2C address is 0x45.
* @n When the ADR is connected to GND, the chip I2C address is 0x44.
*/
DFRobot_SHT3x sht3x(&Wire,/*address=*/0x44,/*RST=*/4);
//DFRobot_SHT3x sht3x;
void setup() {
Serial.begin(9600);
//Initialize the chip
while (sht3x.begin() != 0) {
Serial.println("Failed to Initialize the chip, please confirm the wire connection");
delay(1000);
}
/**
* readSerialNumber Read the serial number of the chip.
* @return Return 32-digit serial number.
*/
Serial.print("Chip serial number");
Serial.println(sht3x.readSerialNumber());
/**
* softReset Send command resets via I2C, enter the chip's default mode single-measure mode,
* turn off the heater, and clear the alert of the ALERT pin.
* @return Read the register status to determine whether the command was executed successfully,
* and return true indicates success.
*/
if(!sht3x.softReset()){
Serial.println("Failed to Initialize the chip....");
}
/**
* heaterEnable(): Turn on the heater inside the chip to enable the sensor get correct humidity value in wet environments.
* @return Read the status of the register to determine whether the command was executed successfully,
* and return true indicates success.
* @note Heaters should be used in wet environments, and other cases of use will result in incorrect readings
*/
//if(!sht3x.heaterEnable()){
// Serial.println("Failed to turn on the heater....");
//}
Serial.println("------------------Read adta in single measurement mode-----------------------");
}
void loop() {
Serial.print("Ambient Temperature(°C/F):");
/**
* getTemperatureC Get the meansured temperature(℃).
* @return Return float temperature data.
*/
Serial.print(sht3x.getTemperatureC());
Serial.print(" C/");
/**
* getTemperatureF:Get the meansured temperature(℉).
* @return Return float temperature data.
*/
Serial.print(sht3x.getTemperatureF());
Serial.print(" F ");
Serial.print("Relative Humidity(%RH):");
/**
* getHumidityRH: Get the meansured humidity (%RH)
* @return Return float humidity data
*/
Serial.print(sht3x.getHumidityRH());
Serial.println(" %RH");
/**
* @brief Get temperature and humidity data in single measurement mode.
* @param repeatability Set repeatability to read temperature and humidity data with the type eRepeatability_t.
* @note Optional parameters:
eRepeatability_High /**In high repeatability mode, the humidity repeatability is 0.10%RH, the temperature repeatability is 0.06°C
eRepeatability_Medium,/**In medium repeatability mode, the humidity repeatability is 0.15%RH, the temperature repeatability is 0.12°C.
eRepeatability_Low, /**In low repeatability mode, the humidity repeatability is0.25%RH, the temperature repeatability is 0.24°C
* @return Return a structure containing celsius temperature (°C), Fahrenheit temperature (°F), relative humidity(%RH), status code.
* @n Return O indicates right data return.
DFRobot_SHT3x::sRHAndTemp_t data = sht3x.readTemperatureAndHumidity(sht3x.eRepeatability_High);
if(data.ERR == 0){
Serial.print("Ambient Temperature(°C/F):");
Serial.print(data.TemperatureC);
Serial.print(" C/");
Serial.print(data.TemperatureF);
Serial.print(" F ");
Serial.print("Relative Humidity(%RH):");
Serial.print(data.Humidity);
Serial.println(" %RH");
}
*/
delay(1000);
}
结果
串口打印出获取到的温湿度数据
样例代码2 - 周期测量温湿度模式
周期测量模式:传感器按照设定采集频率自动去采集数据。
/*!
* @file periodicDataReading.ino
* @brief Read ambient temperature (C/F) and relative humidity (%RH) in cycle read mode.
* @n Experimental phenomenon: Before we start, please set the read frequency and repeatability of the read
* (the difference between the data measured by the chip under the same measurement conditions),
* and enter the periodic read mode, and then read the temperature and humidity data.
* @n The temperature and humidity data will be printed at the serial port, after 10 seconds of operation.
* @n It will exit the cycle mode and enter 2 measurement mode: Single measurement mode and Cycle measurement mode.
* @n Single measurement mode: reflect the difference between the two modes of reading data.
* @n Cycle measurement mode: the chip periodically monitors temperature and humidity, only in this mode the ALERT pin will work.
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [fengli](li.feng@dfrobot.com)
* @version V1.0
* @date 2019-08-20
* @get from https://www.dfrobot.com
* @url https://github.com/DFRobot/DFRobot_SHT3x
*/
#include <DFRobot_SHT3x.h>
/*!
* @brief Construct the function
* @param pWire I2C bus pointer object and construction device, can both pass or not pass parameters, Wire in default.
* @param address Chip I2C address, two optional addresses 0x44 and 0x45(0x45 in default).
* @param RST Chip reset pin, 4 in default.
* @n The I2C address is determined by the pin addr on the chip.
* @n When the ADR is connected to VDD, the chip I2C address is 0x45.
* @n When the ADR is connected to GND, the chip I2C address is 0x44.
*/
DFRobot_SHT3x sht3x(&Wire,/*address=*/0x44,/*RST=*/4);
//DFRobot_SHT3x sht3x;
void setup() {
Serial.begin(9600);
//Initialize the chip to detect if it can communicate properly.
while (sht3x.begin() != 0) {
Serial.println("Failed to initialize the chip, please confirm the chip connection");
delay(1000);
}
/**
* readSerialNumber Read the serial number of the chip
* @return Return 32-digit serial number
*/
Serial.print("chip serial number: ");
Serial.println(sht3x.readSerialNumber());
/**
* softReset Send command resets via I2C, enter the chip's default mode single-measure mode,
* turn off the heater, and clear the alert of the ALERT pin.
* @return Read the status register to determine whether the command was executed successfully,
* and return true indicates success.
*/
if(!sht3x.softReset()){
Serial.println("Failed to reset the chip");
}
/**
* pinReset Reset through the chip's reset pin, enter the chip's default mode single-measure mode,
* turn off the heater, and clear the alert of the ALERT pin.
* @return The status register has a data bit that detects whether the chip has been reset,
* and return true indicates success.
* @note When using this API, the reset pin of the chip nRESET should be connected to RST (default to pin4) of arduino.
*/
//if(!sht3x.pinReset()){
//Serial.println("Failed to reset the chip");
//}
/**
* heaterEnable() Turn on the heater inside the chip so that the sensor can have accurate humidity data even in humid environment.
* @return Read the status register to determine whether the command was executed successfully, and return true indicates success.
* @NOTE Heaters should be used in wet environment, and other cases of use will result in incorrect readings.
*/
//if(!sht3x.heaterEnable()){
// Serial.println("Failed to turn on the heater");
//}
/**
* startPeriodicMode Enter cycle measurement mode and set repeatability and read frequency.
* @param measureFreq Read the eMeasureFrequency_t data frequency.
* @note Selectable parameters:
eMeasureFreq_Hz5, /**the chip collects data in every 2s
eMeasureFreq_1Hz, /**the chip collects data in every 1s
eMeasureFreq_2Hz, /**the chip collects data in every 0.5s
eMeasureFreq_4Hz, /**the chip collects data in every 0.25s
eMeasureFreq_10Hz /**the chip collects data in every 0.1s
* @param repeatability Read the repeatability of temperature and humidity data, the default parameter is eRepeatability_High.
* @note Optional parameters:
eRepeatability_High /**In high repeatability mode, the humidity repeatability is 0.10%RH, the temperature repeatability is 0.06°C
eRepeatability_Medium,/**In medium repeatability mode, the humidity repeatability is 0.15%RH, the temperature repeatability is 0.12°C.
eRepeatability_Low, /**In low repeatability mode, the humidity repeatability is0.25%RH, the temperature repeatability is 0.24°C
* @return Read the status of the register to determine whether the command was executed successfully, and return true indicates success
*/
if(!sht3x.startPeriodicMode(sht3x.eMeasureFreq_1Hz)){
Serial.println("Failed to enter the periodic mode");
}
Serial.println("------------------Read data in cycle measurement mode-----------------------");
}
void loop() {
Serial.print("Ambient temperature(°C/F):");
/**
* getTemperatureC Get the measured temperature (in degrees Celsius).
* @return Return the float temperature data.
*/
Serial.print(sht3x.getTemperatureC());
Serial.print(" C/");
/**
* getTemperatureF Get the measured temperature (in degrees Fahrenheit).
* @return Return the float temperature data.
*/
Serial.print(sht3x.getTemperatureF());
Serial.print(" F ");
Serial.print("Relative humidity(%RH):");
/**
* getHumidityRH Get measured humidity(%RH)
* @return Return the float humidity data
*/
Serial.print(sht3x.getHumidityRH());
Serial.println(" %RH");
//Please adjust the frequency of reading according to the frequency of the chip collection data.
//The frequency to read data must be greater than the frequency to collect the data, otherwise the returned data will go wrong.
delay(100);
if(millis() > 10000 && millis() < 10200){
/**
* stopPeriodicMode() Exit from the cycle read data
* @return Read the status of the register to determine whether the command was executed successfully,
* and return true indicates success.
*/
sht3x.stopPeriodicMode();
Serial.println("Exited from the cycle measurement mode, enter the single measurement mode");
}
/**
* readTemperatureAndHumidity Get temperature and humidity data in cycle measurement mode and use structures to receive data
* @return Return a structure containing celsius temperature (°C), Fahrenheit temperature (°F), relative humidity (%RH), status code.
* @n A status of 0 indicates that the right return data.
DFRobot_SHT3x::sRHAndTemp_t data = sht3x.readTemperatureAndHumidity();
if(data.ERR == 0){
Serial.print("ambient temperature(°C/F):");
Serial.print(data.TemperatureC);
Serial.print("C/");
Serial.print(data.TemperatureF);
Serial.print("F");
Serial.print("relative humidity(%RH):");
Serial.print(data.Humidity);
Serial.println("%RH");
}
*/
}
结果
串口前10S打印周期测量模式下获取的温湿度数据,10S后退出周期测量模式,进入单次测量模式,打印单次测量模式下获取的温湿度数据。
常见问题
还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!
更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。