개발자/Arduino

온도 습도센서 HTU20D, HTU21D 아두이노 라이브러리 코드

지구빵집 2018. 4. 19. 16:58
반응형




사용하는 세서는 온 습도 센서인 HTU20D 이다. 라이브러리는 HTU21D(F) 까지도 지원한다. 데이터 북은 아래 링크를 확인한다


연결 회로는 아래 그림을 참고한다.




Digital Relative Humidity sensor with Temperature output

 

http://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Data+Sheet%7FHPC202_5%7FA3%7Fpdf%7FEnglish%7FENG_DS_HPC202_5_A3.pdf


센서 라이브러리를 또 아름답게 만들어 주셨다. 라이브러리 주소는 아래와 같다.


https://github.com/TEConnectivity/HTU21D_Arduino_Library


라이브러리를 오른쪽 중간의 [clone or download] 버튼을 눌러 zip 파일로 다운로드를 선택한다. 다운 로드 받은 파일을 압축을 풀면 HTU21D_Arduino_Library-master 폴더 이름으로 압축을 풀고 폴더 통채로 아두이노가 설치된 폴더의 라이브러리 폴더로 복사한다. 보통 C:\Program Files (x86)\Arduino\libraries 폴더가 되겠다. 


아두이노 개발환경인 sketch 를 종료하고 다시 실행시킨다. 아두이노와 포트를 확인한 후 파일 - 예제 메뉴에서 모든 아두이노 보드 항목 아래를 보면 TE Connectivity Sensor Solutions HTU21D 가 보이고 그 라애 htu21d_demo 파일을 열어서 컴파일 업로드 하여 실행시키면 결과가 아름답게 나온다.


센서와 라이브러리에 대한 설명

Sensor Description

The HTU2xD(F) sensor is a self-contained humidity and temperature sensor that is fully calibrated during manufacturing. The sensor can operate from 1.5V to 3.6V, has selectable resolution, low battery detection, and checksum capability. The HTU21D has a low current stand-by mode for power-sensitive applications.

Specifications

  • Measures relative humidity from 0% to 100%
  • Measures temperature from -40°C to 125°C
  • Humidity resolution selectable from 8 to 12 bits
  • Temperature resolution selectable from 11 to 14 bits
  • 0.14 µA sleep current
  • 500 µA operating current (during measurement only)
  • ±3% accuracy

Library features

  • Connection test
  • Reset
  • Select I2C master mode
  • Aquisition resolution management
  • Built-in heater management
  • Check battery status
  • Read serial number
  • Temperature and Humidty measurement
  • Calculate compensated humidity
  • Calculate dew point

 

소스코드 htu21f_demo 를 아래에 나타낸다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <htu21d.h>
 
htu21d m_htu21d;
 
void setup() {
  htu21_battery_status battery_status;
  htu21_status status;
 
  Serial.begin(9600);
 
  Serial.println("==== TE Connectivity ====");
  Serial.println("======== HTU21D =========");
  Serial.println("======== Measure =========");
 
  m_htu21d.begin();
  status = m_htu21d.get_battery_status(&battery_status);
  Serial.println(battery_status == htu21_battery_ok ? "Battery OK"
                                                    : "Battery LOW");
 
  m_htu21d.set_i2c_master_mode(htu21_i2c_hold);
}
 
void loop() {
  htu21_status status;
  float temperature;
  float humidity;
  boolean connected;
 
  connected = m_htu21d.is_connected();
  if (connected) {
    Serial.println(connected ? "Sensor Connected" : "Sensor Disconnected");
 
    status = m_htu21d.read_temperature_and_relative_humidity(&temperature,
                                                             &humidity);
 
    Serial.print("---Temperature = ");
    Serial.print(temperature, 1);
    Serial.print((char)176);
    Serial.println("C");
 
    Serial.print("---Humidity = ");
    Serial.print(humidity, 1);
    Serial.println("%RH");
  } else {
    Serial.println(connected ? "Sensor Connected" : "Sensor Disconnected");
  }
 
  delay(1000);
}
 
cs



결과화면이 아름답게 나온다.


==== TE Connectivity ====

======== HTU21D =========

======== Measure =========

Battery OK

Sensor Connected

---Temperature = 28.0캜

---Humidity = 16.7%RH

Sensor Connected

---Temperature = 28.0캜

---Humidity = 16.7%RH

Sensor Connected

---Temperature = 28.0캜

---Humidity = 16.7%RH

Sensor Connected




HTU20D(F) RH/T SENSOR IC Digital Relative Humidity sensor with Temperature output



u3 가 그 놈이다.








반응형