搜索代码

第36课:使用HTU21D温度传感器与LCD Arduino的逐步课程

第36课:使用HTU21D温度传感器与LCD Arduino的逐步课程

HTU21D 温湿度传感器因其易用性和准确性而成为许多 Arduino 项目的热门选择。在本课中,我们将构建一个简单的项目,从传感器读取温度和湿度数据并在液晶屏上显示。到本教程结束时,您将有一个工作设置,持续显示摄氏度、华氏度和开尔文的温度,以及相对湿度百分比。如需进一步澄清,请参考视频(视频时长 mm:ss)。

硬件解读

该项目的主要组成部分是 HTU21D 温湿度传感器,它通过 I2C 进行通信。该传感器在 1.5 至 3.6 伏的电压范围内工作,非常适合 3.3V 和 5V 系统。它的功耗非常低,空闲时通常仅为 0.02 微安,而在测量时为 450 微安。 除了 HTU21D,我们还将使用 LCD 显示器,具体为带 I2C 的 LCD1602。该显示器便于输出文本数据,只需两个引脚进行通信:SDA(数据线)和 SCL(时钟线)。这些组件的组合将使我们能够创建一个用于监测温度和湿度水平的信息显示器。

数据表详情

制造商TE 连接器
零件编号HTU21D-F
逻辑/IO 电压1.5 - 3.6 V
供电电压3.3 V(典型值)
输出电流(典型)0.02 微安 (待机), 450 微安 (测量)
温度范围-40到+125°C
湿度范围0到100 %RH
分辨率(温度)0.01 °C
分辨率(湿度)0.04 %RH
包裹DFN-6

  • 如果未集成,请对SDA和SCL线路使用上拉电阻。
  • 确保供电电压正确,以避免损坏传感器。
  • 保持适当的布线以防止通信错误。
  • 如果传感器没有响应,请检查I2C地址。
  • 在读取之间使用延迟以防止传感器过载。
  • 确保LCD与I2C通信兼容。

接线说明

要连接HTU21D传感器和LCD显示屏,首先处理电源连接。将HTU21D的左侧引脚连接到3.3V电源,将第二个引脚(通常为红色)连接到地。接下来,将HTU21D的SDA引脚连接到Arduino的模拟引脚A4,将SCL引脚连接到模拟引脚A5。 对于LCD1602显示屏,将VCC引脚连接到同一3.3V电源,将GND引脚连接到地。LCD上的SDA引脚也应该连接到A4,SCL引脚应该连接到A5,以便两个组件共享I2C总线。确保所有连接都稳固,以便Arduino、传感器和显示器之间能正常通信。

代码示例与教程

以下代码初始化HTU21D传感器和LCD显示器。在setup函数中,准备好LCD以供使用,并检查传感器的连接状态:

void setup() {
  lcd.begin();
  lcd.backlight();
  if (!htu.begin()) {
      lcd.print("Robojax HTUD1DF");
      lcd.setCursor(0,1);
      lcd.print("sensor missing"); 
      while (1);
  } else {
      lcd.print("Robojax HTUD1DF");
      lcd.setCursor(0,1);
      lcd.print("Demo"); 
  }
  delay(2000);   
}

此摘录检查传感器是否正确连接。如果没有,它会在LCD上显示错误信息并停止程序。如果传感器正常工作,它会显示一个演示信息,持续两秒钟。 循环功能是主要读取和显示的地方。在这里,我们调用`lcdDisplay`函数以不同单位显示温度:

void loop() {
   lcd.clear(); // clear previous values from screen
   lcdDisplay(0, 0, "Celsius: ", 10, 0, getHTU('C'), 'd');  
   lcdDisplay(0, 1, "Fahrenheit: ", 10, 1, getHTU('F'), 'd');     
   delay(5000);
}

在这个循环中,LCD被清除,并显示摄氏度和华氏度的温度读数。`getHTU`函数分别使用字符'C'表示摄氏度和'F'表示华氏度。 最后,定义了`getHTU`函数,以根据输入字符读取温度或湿度:

float getHTU(char type) {
   float temp = htu.readTemperature();
   float rel_hum = htu.readHumidity();
   if(type =='F') {
       return temp * 9/5 + 32; // convert to Fahrenheit 
   } else if(type =='K') {
       return temp + 273.15; // convert to Kelvin
   } else if(type =='H') {
       return rel_hum; // return relative humidity
   } else {
       return temp; // return Celsius
   }
}

此功能从传感器读取温度和湿度,并将温度转换为请求的单位。请务必查看文章下方加载的完整代码以获取更多详细信息。

演示 / 期待什么

完成接线和上传代码后,您应该会看到温度和湿度值显示在 LCD 上。读数每隔几秒钟更新一次,反映当前的条件。如果您对传感器加热,您应该会注意到温度相应上升,而湿度则略微降低。请注意传感器的最高温度限制;超过此限制可能会导致读数不准确或传感器故障(视频中的 mm:ss)。

视频时间戳

  • 00:00- 项目介绍
  • 01:15- wiring instructions
  • 03:30- 代码演示
  • 10:00- 设置演示
511-Lesson 36: Using the HTU21D Temperature Sensor with an LCD and Arduino: A Step-by-Step Course
语言: C++
/*
 * Robojax Arduino Step-by-Step Course
 * Part 4: Temperature Sensors
 * Lesson 36: HTU21D Temperature Sensor with LCD1602 and LCD2004 Display
 


 * Display Temperature from HTU21DF on LCD1602-I2C or LCD2004
 * Updated by Ahmad Shamshiri on July 13, 2019



  Please watch video instructions here https://youtu.be/SrFJKbmiaPM
 This code is available at http://robojax.com/course1/?vid=lecture36
 
with over 100 lectures free on YouTube. Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339  

If you found this tutorial helpful, please support me so I can continue creating 
and make a donation using PayPal http://robojax.com/L/?id=64



*
  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 a 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, and 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() {
	//Get the code for the course: http://robojax.com/L/?id=339  
  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() {
//Get the code for the course: http://robojax.com/L/?id=339  	
   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)
{
	//Get the code for the course: http://robojax.com/L/?id=339  
  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
}

|||您可能需要的东西

文件📁

数据手册 (pdf)