Groveで広がるArduinoの世界-Senser Kit-③ Grove TEMPERATURE SENSOR
半導体は、温度変化によって電流などが大きく変化する特性があって、温度係数と呼びます。昔から、ダイオードやトランジスタの温度変化を温度センサに利用していました。
手軽に使える湿度センサは約30年くらい前に実用化された、比較的新しいセンサです。
湿度は、通常、相対湿度のことを指し、空気中の水分の絶対量が同じでも温度によって大きく値が変化します。したがって、湿度センサは温度を測る必要から、二つの情報が同時に得られます。
●センサDHT11のおもなスペック(データシート)
- 動作電圧 3.3~5.5V
- 湿度 有機ポリマ、測定範囲;5~95%、確度;±5%(25℃時)
- 温度 NTCサーミスタ、測定範囲;-20~60℃、確度 ±2℃
- データ・フォーマット 独自の1-wireの通信方式40ビットで構成され、最初の2バイトが湿度、後半の2バイトが温度、最後に8ビットのパリティ
- 出力 オープン・ドレイン
センサの原理もしくは外形が大きいのが理由かもしれませんが、SHT31より反応速度はゆっくりです。SHT31やBME280の湿度センサは、詳しい解説は見受けられませんが、新しい世代で、レスポンスや確度は大きく向上しています。
●接続
電源の配置は、ほかのGroveコネクタと同じです。
| Groveピン番号 | DHT11ピン |
|---|---|
| 4 GND | GND ④ |
| 3 Vcc | Vcc ① |
| 2 NC | NC ③ |
| 1 SIG | SIG ② |
今回、切り離していないので、配線はありません。切り離したときは、D3と書かれたGroveコネクタ同士を付属のケーブルでつなぎます。
●プログラム
「THE TEMPERATURE SENSOR」に掲載されているサンプル・プログラムです。
#define DHTPIN 3 // By default the module connected to pin D3, it can be changed, define it before the #include of the library
#include "Arduino_SensorKit.h"
void setup() {
Serial.begin(9600);
Environment.begin();
}
void loop() {
Serial.print("Temperature = ");
Serial.print(Environment.readTemperature()); //print temperature
Serial.println(" C");
Serial.print("Humidity = ");
Serial.print(Environment.readHumidity()); //print humidity
Serial.println(" %");
delay(2000);
}
実行中の様子です。温度と湿度を表示しています。
温度、湿度共に16ビット・データですが、確度が荒いので、小数点第2位まで表示する意味がありません。湿度は整数だけ、温度は小数点第1位までの表示に変更しました。
#define DHTPIN 3 // By default the module connected to pin D3, it can be changed, define it before the #include of the library
#include "Arduino_SensorKit.h"
void setup() {
Serial.begin(9600);
Environment.begin();
}
void loop() {
Serial.print("Temperature = ");
Serial.print(Environment.readTemperature(),1); //print temperature
Serial.println(" C");
Serial.print("Humidity = ");
Serial.print(Environment.readHumidity(),0); //print humidity
Serial.println(" %");
delay(2000);
}
実行中の様子です。表示桁を変更して温度と湿度を表示しています。
シリアルモニタを終了し、シリアルプロッタを起動します。複数の結果をプロットするときは、","で区切ります。
#define DHTPIN 3 // By default the module connected to pin D3, it can be changed, define it before the #include of the library
#include "Arduino_SensorKit.h"
void setup() {
Serial.begin(9600);
Environment.begin();
}
void loop() {
Serial.print("Temperature = ");
float t = Environment.readTemperature();
Serial.println(t,1); //print temperature
Serial.print(", Humidity = ");
float h = Environment.readHumidity();
Serial.println(h,0); //print humidity
delay(2000);
}
実行中の様子です。温度と湿度を表示しています。
●OLEDディスプレイに表示
光センサの値、温度と湿度を表示しました。
#include "Arduino_SensorKit.h"
#define DHTPIN 3
#define light_sensor A3
void setup() {
Serial.begin(9600); //begin Serial Communication
Environment.begin();
Oled.begin();
Oled.setFlipMode(true); // Sets the rotation of the screen
}
void loop() {
int raw_light;
raw_light= analogRead(light_sensor); // read the raw value from light_sensor pin (A3)
int light = map(raw_light, 0, 1023, 0, 100); // map the value from 0, 1023 to 0, 100
Oled.clear();
Oled.setFont(u8x8_font_chroma48medium8_r);
Oled.setCursor(1, 0);
Oled.print("light:"); Oled.print(light); // Print the Values
Oled.setCursor(1, 1);
Oled.print("Temp:"); Oled.print(Environment.readTemperature(),1);
Oled.setCursor(1, 2);
Oled.print("Humi:"); Oled.print(Environment.readHumidity(),0);
Oled.refreshDisplay(); // Update the Display
delay(3000); // add a delay to only read and print every 3 second
}