検索コード

レッスン 35-1: HTU21D 温度センサーの使用

レッスン 35-1: HTU21D 温度センサーの使用

このチュートリアルでは、HTU21D温湿度センサーをArduinoと一緒に使用する方法を探ります。HTU21DはI2Cで通信するコンパクトなセンサーであり、マイクロコントローラーとのインターフェースが簡単です。レッスンの終わりまでに、温度と湿度の値を読み取り、それをArduinoのシリアルモニターに表示できるようになります。

HTU21D module

このプロジェクトでは、HTU21Dセンサー専用に設計されたAdafruitライブラリを利用します。このライブラリはセンサーデータを読み取るプロセスを簡素化し、センサーを私たちのプロジェクトに統合することに集中できるようにします。センサーの温度測定値は摂氏、華氏、ケルビンで表示されることを期待でき、また相対湿度のパーセンテージも表示されます(動画の03:15に)。

ハードウェアの解説

このプロジェクトの主なコンポーネントは、HTU21D温度湿度センサーです。このセンサーは、1.5ボルトから3.6ボルトの供給電圧で動作し、非常に少ない電力を消費するため、バッテリー駆動のデバイスに最適です。I2Cプロトコルを使用してArduinoと通信し、データ用のSDAとクロック用のSCLという2本のデータラインのみを必要とします。

センサーに電力を供給するために、使用している特定のモジュールに応じて、3.3Vまたは5Vの電源に接続します。Adafruitバージョンは電圧レギュレーターを含んでおり、両方の電圧レベルでシームレスに動作します。この柔軟性により、さまざまなアプリケーションに適した選択肢となります。

データシートの詳細

製造業者TE コネクティビティ
部品番号HTU21D-F
ロジック/IO電圧1.5 - 3.6 V
供給電圧3.3 V (典型値)
現在の消費量(アイドル)0.02 µA
現在の消費(測定)450 µA(典型値)
温度範囲-40から+125 °C
湿度解像度0.04 %
パッケージ6ピンDFN

  • センサーが損傷しないように適切な電圧レベルを維持してください。
  • モジュールに含まれていない場合は、I2Cラインにプルアップ抵抗を使用してください。
  • I2C通信の干渉を最小限に抑えるために、配線は短くしてください。
  • 高温環境で使用する場合は、ヒートシンクを考慮してください。
  • センサーの向きを確認して、正しいピン接続を確保してください。

配線指示

Arduino wiring for HTU21DF light intesity sensor
Arduino wiring for HTU21DF light intesity sensor

HTU21DセンサーをArduinoに配線するには、まずセンサーのVCCピンをArduinoの3.3V電源ピンに接続します。次に、センサーのGNDピンをArduinoの接地(GND)ピンに接続します。それから、センサーのSDAピンをArduinoのアナログピンA4に接続します。このピンはI2Cデータラインとして機能します。最後に、センサーのSCLピンをアナログピンA5に接続します。このピンはI2Cクロックラインとして動作します。接続が確実であることを確認し、不安定な読み取りを避けてください。

明確にするために、別のボードを使用している場合は、SDAおよびSCLの正しいピンを特定してください。これらは異なる場合があります。この配線構成により、ArduinoがHTU21Dセンサーと効果的に通信できるようになります(動画の05:00にて)。

コード例とウォークスルー

以下は、シリアル通信を初期化し、センサーが見つかったかどうかを確認するセットアップ関数の抜粋です。

void setup() {
  Serial.begin(9600);
  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}

このコードは9600ボーのボーレートでシリアル通信を開始し、HTU21Dセンサーの初期化を試みます。センサーが検出されない場合、エラーメッセージを表示し、さらに実行を停止します。

プログラムのメインループは、温度と湿度の値を継続的に読み取ります。

void loop() {
    Serial.print(getHTU('C'));
    Serial.println("C");
    Serial.print("Humidity:");
    Serial.print(getHTU('H'));
    Serial.println("%");
    delay(1000);
}

このループでは、関数を呼び出します。getHTU'C'を使って摂氏温度を取得し、'H'を使って湿度を取得します。結果は毎秒シリアルモニターに出力されます。これにより、センサーの読み取り値をリアルタイムで観察できます。

さらに、私たちは機能を持っていますgetHTU文字入力に基づいて温度または湿度を返す。

float getHTU(char type) {
    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    return (type == 'H') ? rel_hum : temp;
}

この機能はセンサーから温度と湿度を読み取り、指定されたタイプに基づいて適切な値を返します。このモジュラーアプローチは、コードをクリーンで保守しやすく保ちます。

デモンストレーション / 期待すること

コードのセットアップと実行が成功すると、温度と湿度の値がシリアルモニタに表示されるはずです。センサーに熱を加えると、温度の読み取り値はそれに応じて上昇し、湿度は減少する可能性があります。温度がセンサーの最大範囲を超えると、ゼロ(動画の12:00で)などの予期しない結果が表示されることがあります。

ビデオのタイムスタンプ

  • 00:00- はじめに
  • 03:15- センサーの概要
  • 05:00- 配線指示
  • 10:00- コードウォークスルー
  • 12:00- デモンストレーション

画像

Arduino wiring for HTU21DF light intesity sensor
Arduino wiring for HTU21DF light intesity sensor
HTU21D module
HTU21D module
HTU21D module-back
HTU21D module-back
512-Lesson 35: Using HTU21D Temperature Sensor
言語: C++
/*
 * Robojax Arduino Step-by-Step Course
 * Part 4: Temperature Sensors
 * Lesson 35: HTU21D Temperature Sensor
 

 * Updated by Ahmad Shamshiri on July 13, 2019
 * in Ajax, Ontario, Canada
 
  Please watch video instructions here https://youtu.be/LyA0yAKlf9E
 This code is available at http://robojax.com/course1/?vid=lecture35
 
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.
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/>. 
    
	
 */ 
/*

**************************************************
*
  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
 ****************************************************/

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

/*
 * @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");  
}

必要かもしれないもの

ファイル📁

データシート(pdf)