[Arduino|아두이노] 온도 습도 센서 (KY-015, DHT11) 사용방법

아두이노 온도, 습도 센서 (KY-015) 사용방법


온도, 습도 센서 KY-015DHT11 센서에 기판 및 저항이 부착되어 더 편하게 사용할 수 있다.

소스코드는 동일하게 사용가능 하다. 비용은 조금더 비싼듯하지만 인터넷에서 쉽게 구할 수 있다.

아래의 사진과 같이 3개의 핀으로 이루어져 있으며 2가지 소스코드 예제로 테스트를 해 보았다.




DHT11 라이버리를 사용하지 않고 센서의 값을 가져오는 예제

//KY015 DHT11 Temperature and humidity sensor //https://tkkrlab.nl/wiki/Arduino_KY-015_Temperature_and_humidity_sensor_module int DHpin = 4; byte dat [5]; byte read_data () { byte data; for (int i = 0; i < 8; i ++) { if (digitalRead (DHpin) == LOW) { while (digitalRead (DHpin) == LOW); // wait for 50us delayMicroseconds (30); // determine the duration of the high level to determine the data is '0 'or '1' if (digitalRead (DHpin) == HIGH) data |= (1 << (7-i)); // high front and low in the post while (digitalRead (DHpin) == HIGH); // data '1 ', wait for the next one receiver } } return data; } void start_test () { digitalWrite (DHpin, LOW); // bus down, send start signal delay (30); // delay greater than 18ms, so DHT11 start signal can be detected digitalWrite (DHpin, HIGH); delayMicroseconds (40); // Wait for DHT11 response pinMode (DHpin, INPUT); while (digitalRead (DHpin) == HIGH); delayMicroseconds (80); // DHT11 response, pulled the bus 80us if (digitalRead (DHpin) == LOW); delayMicroseconds (80); // DHT11 80us after the bus pulled to start sending data for (int i = 0; i < 4; i ++) // receive temperature and humidity data, the parity bit is not considered dat[i] = read_data (); pinMode (DHpin, OUTPUT); digitalWrite (DHpin, HIGH); // send data once after releasing the bus, wait for the host to open the next Start signal } void setup () { Serial.begin (9600); pinMode (DHpin, OUTPUT); } void loop () { start_test (); Serial.print ("Current humdity ="); Serial.print (dat [0], DEC); // display the humidity-bit integer; Serial.print ('.'); Serial.print (dat [1], DEC); // display the humidity decimal places; Serial.println ('%'); Serial.print ("Current temperature ="); Serial.print (dat [2], DEC); // display the temperature of integer bits; Serial.print ('.'); Serial.print (dat [3], DEC); // display the temperature of decimal places; Serial.println ('C'); delay (700); }


<결과값>


3개의 핀을 순서대로 연결 한다



아두이노에도 5V, GND, 4번핀에 점퍼선을 연결한다



라이버러리를 사용하지 않으면 소스코드가 조금 복잡해지므로 인터넷에 공개되어 있는

DHT11 라이버러리를 다운받아서 아래의 아두이노 설치 경로에 복사하여 추가한다


DHT11.zip



폴더에 복사를 하면 아두이노 예제 및 라이버러리에 추가 되어있다

해당 예제를 선택하여 보자


DHT11 라이버리를 사용하여 센서의 값을 가져오는 예제

#include <DHT11.h>
int pin=4;
DHT11 dht11(pin); 
void setup()
{
   Serial.begin(9600);
  while (!Serial) {
      ; // wait for serial port to connect. Needed for Leonardo only
    }
}

void loop()
{
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
    Serial.print("temperature:");
    Serial.print(temp);
    Serial.print(" humidity:");
    Serial.print(humi);
    Serial.println();
  }
  else
  {
    Serial.println();
    Serial.print("Error No :");
    Serial.print(err);
    Serial.println();    
  }
  delay(DHT11_RETRY_DELAY); //delay for reread
}


<결과값>



두가지 예제 모두 비슷한 수치의 결과값을 보여준다

 

댓글

Designed by JB FACTORY