3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ArduinoでGroveデジタル温度・湿度センサDHT11と7セグディスプレイを試す

Last updated at Posted at 2015-02-28

2021/08/11 追記

  • Seeed社が新しい部品番号を割り当てたようなのでリンクと共に修正しました。
  • 【注意】デジタル温度・湿度センサは、デジタル入出力ピンに接続するのが妥当なようです。この記事では「アナログ入力ピンはデジタル入出力ピンとしても利用可能」というArduino UNOの仕様により上手くいっています。

準備

適宜、ご自身の環境に読み替えてくださいませ。

CASE1:デジタル温度・湿度センサ値をシリアルモニタで確認する

取得値を確認します。

1. ベースシールドにデジタル温度・湿度センサを接続する

使用するテストスケッチの関係上、 A0 端子に接続します。

001-01-light.jpg

ピンアサインは、下表です。

Arduino Sensor Color
GND GND Black
VCC VCC Red
A1 NC White
A0 SIG Yellow

USBケーブルでPCに接続しておく。

2. デジタル温度・湿度センサ用ライブラリをインストールする

  1. ライブラリをダウンロード
    https://github.com/Seeed-Studio/Grove_Temperature_And_Humidity_Sensor
    右の【Download ZIP】を押す。
  2. ZIP を解凍する
  3. ライブラリを Arduino IDE にインストール
    Arduino IDE の [スケッチ] → [ライブラリを使用] → [ライブラリをインストール…] から解凍したフォルダ内の Humidity_Temperature_Sensor フォルダを選択し、インストールする。
  • \ドキュメント\Arduino\libraries\ 以下にコピーされる

3. スケッチを作成する

テストスケッチを参考にしました。

  • [ファイル] → [スケッチの例] → [Humidity_Temperature_Sensor] → [DHTtester]
Grove_Temp_Humi_Sensor.ino
# include "DHT.h"

# define DHTPIN A0
# define DHTTYPE DHT11

DHT dht( DHTPIN, DHTTYPE );

void setup() {
    Serial.begin( 9600 );
    dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if( isnan(t) || isnan(h) ) {
    Serial.println( "Failed to read from DHT" );
  }
  else {
    Serial.print( "Humidity: " );
    Serial.print( h );
    Serial.print( " %\t" );
    Serial.print( "Temperature: " );
    Serial.print( t );
    Serial.println( " *C" );
  }
}

4. 動作を確認する

  1. Arduino IDE にてコンパイル&書き込み
  2. シリアルモニタを起動し、ログを確認する
    [ツール] → [シリアルモニタ]
    001-02.png

5. 感想

  • とても簡単でした。
  • 温湿度センサを使って何をするか次第ですが、アイデアなし…。
  • 以下のサイトによると、このセンサは精度が悪いとのことなので、精度で困ったら高級なセンサに替えるべきかも。
  • Arduino温湿度測定ロガー・湿度センサーの比較

6. リファレンス

CASE2:温湿度センサ値を7セグディスプレイで表示する

温湿度センサの仕様を確認すると、以下であり、小数点以下は意味を成していない…。

  • 温度
  • 精度:+- 2 ℃
  • 感度: 1 ℃
  • 湿度
  • 精度:+- 5 %
  • 感度: 1 ℃

そこで、 7 セグ 4 桁ディスプレイの左 2 桁で温度を、右 2 桁で湿度を表示することとする。

1. 温湿度センサと7セグをベースシールドのA0端子D7端子に接続する

ピンアサインは、下表です。

Arduino 温湿度センサ Color
GND GND Black
VCC VCC Red
A1 NC White
A0 SIG Yellow
Arduino 7セグディスプレイ Color
GND GND Black
VCC VCC Red
D8 DIO White
D7 CLK Yellow

2. スケッチを作成する

test
# include "DHT.h"
# include "TM1637.h"

# define DHTPIN A0
# define DHTTYPE DHT11
# define NANA_SEG_CLK 7
# define NANA_SEG_DIO 8

DHT dht( DHTPIN, DHTTYPE );
TM1637 tm1637( NANA_SEG_CLK, NANA_SEG_DIO );

int8_t temp_digits[2] = {0};
int8_t humi_digits[2] = {0};

void setup() {
//  Serial.begin( 9600 );
  dht.begin();
  tm1637.init();
  tm1637.set( BRIGHT_DARKEST );
}

void loop() {
  int temp = dht.readTemperature();
  int humi = dht.readHumidity();
  
//  Serial.print( "Temp: " );
//  Serial.print( temp );
//  Serial.print( " *C " );
//  Serial.print( "Humi: " );
//  Serial.print( humi );
//  Serial.println( " %" );
  
  memset( temp_digits, 0, 2 );
  memset( humi_digits, 0, 2 );
  
  temp_digits[1] = temp % 10;  // the one's place of temperature
  temp /= 10;
  temp_digits[0] = temp % 10;  // the ten's place of temperature
  humi_digits[1] = humi % 10;  // the one's place of humidity
  humi /= 10;
  humi_digits[0] = humi % 10;  // the ten's place of humidity
  
  tm1637.display( 0, temp_digits[0] );
  tm1637.display( 1, temp_digits[1] );
  tm1637.display( 2, humi_digits[0] );
  tm1637.display( 3, humi_digits[1] );

  delay( 3000 );
}

3. 動作を確認する

  1. Arduino IDE にてコンパイル&書き込み
  2. 7セグ

4. 感想

5. リファレンス

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?