本教程是的一部分: 数字式相对湿度和温度传感器 HTU21D
本文下方提供了与数字式相对湿度和温度传感器 HTU21D 相关的视频链接。
如何在Arduino上使用HTU21DF湿度和温度传感器(自定义代码)
此代码用于使用HTU21DF传感器测量温度和湿度。提供了完整的硬件、两种类型的代码以及演示。该代码在视频中显示的串行监视器上以摄氏度、华氏度或开尔文显示温度,并显示湿度。有关其他相关代码,请向下滚动此屏幕。
[广告]测量温度和湿度
211-Arduino code using HTU21D-F Humidity & Temperature Sensor (Advanced)
语言: C++
/*
* Written/Updated by Ahmad Shamshiri on July 13, 2019
* in Ajax, Ontario, Canada
* Watch video instructions for this sketch: https://youtu.be/Q5y18rgTAhA
**************************************************
*
This is an example for the HTU21D-F Humidity & Temperature Sensor
Designed specifically to work with the HTU21D-F sensor from Adafruit
----> https://www.adafruit.com/products/1899
These displays use I2C to communicate; 2 pins are required to
interface
***************************************************
*/
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
// Connect Vin to 3-5VDC
// Connect GND to ground
// Connect SCL to I2C clock pin (A5 on UNO)
// Connect SDA to I2C data pin (A4 on UNO)
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
void setup() {
Serial.begin(9600);
Serial.println("Robojax.com");
Serial.println("HTU21D-F test");
if (!htu.begin()) {
Serial.println("Couldn't find sensor!");
while (1);
}
}
void loop() {
// Robojax HTU21DF Code
Serial.print(getHTU('C'));
printDegree();
Serial.println("C");
Serial.print(getHTU('F'));
printDegree();
Serial.println("F");
Serial.print(getHTU('K'));
Serial.println("K");
Serial.println(" ");
Serial.print("Humidity:");
Serial.print(getHTU('H'));
Serial.println("%");
if(getHTU('C') <81)
{
//digitalWrite(5, LOW);
}
delay(1000);
}
/*
* @brief returns temperature or relative humidity
* @param "type" is a character
* C = Celsius
* K = Kelvin
* F = Fahrenheit
* H = Humidity
* @return returns one of the values above
* Usage: to get Fahrenheit type: getHTU('F')
* to print it on the serial monitor: Serial.println(getHTU('F'));
* Written by Ahmad Shamshiri on July 13, 2019
* in Ajax, Ontario, Canada
* www.Robojax.com
*/
float getHTU(char type)
{
float value;
float temp = htu.readTemperature();
float rel_hum = htu.readHumidity();
if(type =='F')
{
value = temp *9/5 + 32;//convert to Fahrenheit
}else if(type =='K')
{
value = temp + 273.15;//convert to Kelvin
}else if(type =='H')
{
value = rel_hum;//return relative humidity
}else{
value = temp;// return Celsius
}
return value;
}//
/*
* @brief prints degree symbol on serial monitor
* @param none
* @return returns nothing
* Written by Ahmad Shamshiri on July 13, 2019
* for Robojax Tutorial Robojax.com
*/
void printDegree()
{
Serial.print("\\xC2");
Serial.print("\\xB0");
}
|||您可能需要的东西
-
亚马逊从亚马逊购买HTU21D模块amzn.to
-
易趣在eBay上购买HTU21Debay.us
-
全球速卖通从AliExpress购买HTU21D或SHT21s.click.aliexpress.com
资源与参考
-
外部Adafruit HTU21D库(GitHub)github.com
文件📁
数据手册 (pdf)
-
HTU21D温湿度数据表
HTU21D_temerature_humidity_datasheet.pdf0.96 MB