本教程是的一部分: 数字式相对湿度和温度传感器 HTU21D
本文下方提供了与数字式相对湿度和温度传感器 HTU21D 相关的视频链接。
在LCD上显示HTU21D的温度
在本教程中,我们将学习如何在LCD上显示HTU21D温湿度传感器的温度读数。最终结果将是一个功能完整的设置,温度可以以摄氏度、华氏度和开尔文显示,同时相对湿度也在同一屏幕上显示。该项目将为您提供I2C通信和基本传感器数据处理的实际经验。要获得详细的视觉指南,请务必观看视频(视频在00:00)。

硬件解析
该项目的主要组件包括HTU21D传感器和LCD显示屏。HTU21D是一个数字湿度和温度传感器,通过I2C协议进行通信。它提供以摄氏度或华氏度为单位的精确温度测量以及以百分比表示的相对湿度。该传感器通常在3.3V的电压下工作,但也可以与5V系统兼容。 我们将使用的LCD显示屏是一个带有I2C接口的16x2 LCD。这种类型的显示屏仅需要两根数据通信线,这使得与像Arduino这样的微控制器连接更为简单。I2C接口允许多个设备连接在同一总线,简化了布线并减少了使用的引脚数量。
数据表详情
| 制造商 | Adafruit |
|---|---|
| 零件编号 | HTU21D-F |
| 逻辑/输入输出电压 | 3.3 V(典型值) |
| 供电电压 | 1.5 伏特 到 3.6 伏特 |
| 输出电流 | 0.5 毫安 (典型值) |
| 峰值电流 | 1.5 mA(最大) |
| 温度范围 | -40 到 125 °C |
| 湿度范围 | 0 到 100 %RH |
| 包裹 | DFN-6 |
| 备注 / 变体 | None |
- 确保电压水平正确,以避免传感器损坏。
- 如果有必要,在SDA和SCL线路上使用上拉电阻。
- 使用扫描仪检查I2C地址以确保正确配置。
- 小心处理传感器的输出,以避免读取错误。
- 在进行测量时,请将传感器远离热源。
接线说明

要将HTU21D传感器连接到Arduino,请将传感器的VCC引脚连接到Arduino的3.3V输出上。接下来,将传感器的GND引脚连接到Arduino的地线。用于数据传输的SDA引脚应连接到Arduino的A4引脚。同样,将用于时钟信号的SCL引脚连接到Arduino的A5引脚。 确保您的LCD也正确连接;在此设置中,LCD的I2C地址通常是0x3F。如果您使用不同类型的LCD或不同的I2C地址,请确保相应地调整代码。接线后,请检查是否有松动的连接,然后再开启Arduino电源。
代码示例和演示
在代码中,我们首先包含HTU21D传感器和LCD所需的库:
#include
#include "Adafruit_HTU21DF.h"
#include
这为传感器和显示器的使用设置了环境。`Adafruit_HTU21DF`库处理传感器的功能,而`LiquidCrystal_I2C`库管理液晶显示器。 接下来,我们为传感器和液晶显示器创建实例:
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
LiquidCrystal_I2C lcd(0x3F, 16, 2);
这里,htu是HTU21D传感器的对象,并且lcd是LCD显示器的对象。LCD的I2C地址设置为0x3F,您可能需要根据您的配置验证。setup()功能,我们初始化LCD并检查传感器是否正常运作:
void setup() {
lcd.begin();
lcd.backlight();
if (!htu.begin()) {
lcd.print("Sensor missing");
while (1);
}
lcd.print("HTU21D Ready");
}
该代码初始化LCD并检查传感器的存在性。如果未检测到传感器,它将显示“缺少传感器”并停止程序。loop()我们不断读取温度和湿度值并将其显示出来:
void loop() {
lcd.clear();
lcdDisplay(0, 0, "Celsius: ", 10, 0, getHTU('C'), 'd');
lcdDisplay(0, 1, "Humidity: ", 10, 1, getHTU('H'), '%');
delay(5000);
}
在这个摘录中,我们清除LCD,然后调用lcdDisplay()显示摄氏温度和湿度的功能。getHTU()该函数根据传递的字符获取温度或湿度。
演示 / 期待什么
当设置完成并将代码上传到Arduino后,LCD应该显示当前的摄氏温度和相对湿度。如果一切连接正常,您将看到数值每隔几秒更新一次。请注意常见的陷阱,例如连接反向或I2C地址不正确,这可能会导致传感器通信失败(视频中的05:00)。
视频时间戳
- 00:00- 介绍
- 01:30- 接线说明
- 03:15- 代码解释
- 04:50- 示范
- 05:40- 结论
/*
* Display Temperature from HTU21DF on LCD1602-I2C or LCD2004
* Updated by Ahmad Shamshiri on July 13, 2019
* in Ajax, Ontario, Canada
* Watch video instructions for this sketch:
**************************************************
*
This is an example for the HTU21D-F Humidity & Temp 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
Watch Introduction to 360 Servo video with code: https://youtu.be/b_xvu6wWafA
You can get the wiring diagram from my Arduino Course at Udemy.com
Learn Arduino step by step with all libraries, codes, wiring diagrams all in one place
visit my course now http://robojax.com/L/?id=62
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* Code is available at http://robojax.com/learn/arduino
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#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();
// start of settings for LCD1602 with I2C
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// end of settings for LCD1602 with I2C
void setup() {
lcd.begin();
lcd.backlight();
if (!htu.begin()) {
lcd.print("Robojax HTUD1DF");
lcd.setCursor(0,1);
lcd.print("sensor missing");
while (1);
}else{
// initialize the LCD
lcd.print("Robojax HTUD1DF");
lcd.setCursor(0,1);
lcd.print("Demo");
}
delay(2000);
}
void loop() {
lcd.clear();// clear previous values from screen
lcdDisplay(
// to print Celsius:
0, // character 0
0, // line 0
"Celsius: ",
// to print Celsius
10, // character 10
0, // line 0
getHTU('C'),
'd'
);
lcdDisplay(
// to print Fahrenheit:
0, // character 0
1, // line 1
"Fahrenheit: ",
// to print Fahrenheit
10, // character 9
1, // line 0
getHTU('F'),
'd'
);
delay(5000);
lcdDisplay(
// to print Kelvin:
0, // character 0
0, // line 0
"Kelvin: ",
// to print Celsius
9, // character 10
0, // line 0
getHTU('K'),
'k'
);
lcdDisplay(
// to print humidity text
0, // character 0
1, // line 1
"Humidity: ",
// to print humidity
10, // character 9
1, // line 1
getHTU('H'),
'%'
);
delay(5000);
}
/*
* @brief returns temperature or relative humidity
* @param "type" is 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 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;
}//
/*
* lcdDisplay(int tc, int tr, String title, int vc, int vr, float value)
* displays value and title on LCD1602
* How to use:
* If you want to display: "Voltage: 13.56mV" starting from the first character
* on the second row.
* use:
* lcdDisplay(0, 1, "Voltage: ", 13.56,'d')
*
* 'd' is degree symbol
* tc is character number (0)
* tr is row in the lcd (1)
* title is the text (Voltage:)
* vc value for character
* vr value for row or line
* value is the value (13.56)
*/
void lcdDisplay(int tc, int tr, String title, int vc, int vr, float value,char symbol)
{
// Robojax.com LCD1602 for HTU21D Demo
lcd.setCursor (tc,tr); //
lcd.print(title);
lcd.setCursor (vc,vr); //
lcd.print(value);
if(symbol == 'd')
{
lcd.print((char)223);
}else if(symbol =='%')
{
lcd.print("%");
}else if(symbol =='k')
{
lcd.print("K");
}
// Robojax.com LCD1602 for HTU21D Demo
}
|||您可能需要的东西
-
亚马逊从亚马逊购买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