このチュートリアルはの一部です: デジタル相対湿度・温度センサー HTU21D
デジタル相対湿度・温度センサー HTU21D に関する動画。他の動画へのリンクはこの記事の下にあります。
レッスン36:LCDとHTU21D温度センサーを使ったArduinoのステップバイステップコース
HTU21D温湿度センサーは、その使いやすさと精度から、多くのArduinoプロジェクトで人気の選択肢です。このレッスンでは、センサーから温度と湿度データを読み取り、LCD画面に表示するシンプルなプロジェクトを構築します。このチュートリアルの終わりまでに、摂氏、華氏、ケルビンで温度を継続的に表示し、相対湿度のパーセンテージも表示する動作セットアップが完成します。詳細については、動画を参照してください(動画のmm:ssで)。
ハードウェアの解説
このプロジェクトの主なコンポーネントは、I2C経由で通信するHTU21D温度湿度センサーです。このセンサーは1.5ボルトから3.6ボルトの電圧範囲内で動作し、3.3Vおよび5Vシステムの両方に適しています。アイドル時には通常わずか0.02マイクロアンペア、測定中には450マイクロアンペアという非常に少ない電力を消費します。 HTU21Dに加えて、LCD1602をI2Cで使用するLCDディスプレイも使用します。このディスプレイはテキストデータの出力を簡単に行うことができ、通信にはSDA(データライン)とSCL(クロックライン)の2つのピンのみを必要とします。これらのコンポーネントの組み合わせにより、温度と湿度レベルを監視するための情報表示を作成することが可能になります。
データシートの詳細
| 製造業者 | TEコネクティビティ |
|---|---|
| 部品番号 | HTU21D-F |
| 論理/IO電圧 | 1.5 - 3.6 V |
| 供給電圧 | 3.3 V(典型) |
| 出力電流(典型値) | 0.02 µA(アイドル)、450 µA(測定) |
| 温度範囲 | -40から+125℃ |
| 湿度範囲 | 0から100 %RH |
| 解像度 (温度) | 0.01 °C |
| 解像度(湿度) | 0.04 %RH |
| パッケージ | DFN-6 |
- SDAおよびSCLラインが統合されていない場合は、プルアップ抵抗を使用してください。
- センサーが損傷しないように、正しい電源電圧を確保してください。
- 適切な配線を維持して、通信エラーを防ぎます。
- センサーが応答しない場合は、I2Cアドレスを確認してください。
- 読み取りの間に遅延を使用して、センサーの過負荷を防ぎます。
- LCDがI2C通信に対応していることを確認してください。
配線指示
HTU21DセンサーとLCDディスプレイを配線するには、まず電源接続から始めます。HTU21Dの左端子を3.3V電源に接続し、2番目の端子(通常は赤)をグラウンドに接続します。次に、HTU21DのSDA端子をArduinoのアナログ端子A4に接続し、SCL端子をアナログ端子A5に接続します。 LCD1602ディスプレイについては、VCC端子を同じ3.3V電源に接続し、GND端子をグラウンドに接続します。LCDのSDA端子もA4に接続し、SCL端子はA5に接続して、両方のコンポーネントがI2Cバスを共有できるようにします。すべての接続がしっかりと固定されていることを確認して、Arduino、センサー、およびディスプレイ間の適切な通信を促進します。
コード例とウォークスルー
次のコードはHTU21DセンサーとLCDディスプレイを初期化します。セットアップ関数では、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にエラーメッセージを表示し、プログラムを停止します。センサーが機能している場合は、2秒間デモメッセージを表示します。 ループ関数は、主要な読み取りと表示が行われる場所です。ここでは、温度を異なる単位で表示するために `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- 配線指示
- 03:30- コードウォークスルー
- 10:00- セットアップのデモ
このチュートリアルはの一部です: デジタル相対湿度・温度センサー HTU21D
/*
* 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
}
必要かもしれないもの
-
アマゾンAmazonでHTU21Dモジュールを購入するamzn.to
-
イーベイeBayからHTU21Dを購入してください。ebay.us
-
アリエクスプレスAliExpressでHTU21DまたはSHT21を購入してください。s.click.aliexpress.com
リソースと参考文献
-
外部Adafruit HTU21Dライブラリ(GitHub)github.com
-
外部Arduinoを使用してHTU21DからLCDに温度をバーグラフとして表示するrobojax.com
-
外部HTU21DFとHTU21Dの違い(画像)robojax.com
ファイル📁
データシート(pdf)
-
HTU21D温度・湿度データシート
HTU21D_temerature_humidity_datasheet.pdf0.96 MB