개발자/Raspberry Pi

라즈베리파이 DHT11 온도 습도 센서 사용하기, LCD 에 온도와 습도를 출력

지구빵집 2016. 10. 24. 14:24
반응형

 

 

 

라즈베리 파이로 DHT11 온습도 데이터를 읽어오는 방법을 설명한다.

DHT11 온습도 센서는 아래와 같은 모양이다. PCB 마운트 되어 있거나 그냥 4핀이 나와있는 형태다. 

온습도 출력을 보기 위해 PuTTY 와 같은 SSH 지원 터미널을 사용한다.

 

DHT11 온습도 센서의 간단한 사양을 살펴보면

 

DHT11 MODULE

Power: DC 3~5V

Pins: G (GND) – V (VCC) – D (Data)

Humidity Measurement: 20~80% humidity reading with 5% accuracy

Temperature Measurement: 0~50°C temperature reading with ±2°C accuracy

Should not measure more than once per second

 

 

센서 연결은 아래와 같다. signal 핀은 데이터가 나오는 핀으로 어떤 GPIO 핀을 사용하여도 된다. 

 

 

 

 

아래 그림은 LCD 디스플레이를 사용해서 결과값을 보여줄때 사용되는 연결도다.

 

 

 

 

 

프로그램을 하기전에 wiringPi 라이브러리가 설치되어 있어야 한다. 아래 링크를 참고한다.

 

http://fishpoint.tistory.com/1518  라즈베리파이의 GPIO를 제어할 수 있는 wiringPi라이브러리의 설치 방법

 

http://fishpoint.tistory.com/1814 Raspberry Pi GPIO

 

 

소스코드를 나노 에디터를 사용하여 입력한다.

 

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
$nano dht11temp.c
 
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS    85
#define DHTPIN        7  //wPi pin. physical num 7
int dht11_dat[5= { 00000 };
 
void read_dht11_dat()
{
    uint8_t laststate    = HIGH;
    uint8_t counter        = 0;
    uint8_t j        = 0, i;
    float    f; 
 
    dht11_dat[0= dht11_dat[1= dht11_dat[2= dht11_dat[3= dht11_dat[4= 0;
 
    pinMode( DHTPIN, OUTPUT );
    digitalWrite( DHTPIN, LOW );
    delay( 18 );
    digitalWrite( DHTPIN, HIGH );
    delayMicroseconds( 40 );
    pinMode( DHTPIN, INPUT );
 
    for ( i = 0; i < MAXTIMINGS; i++ )
    {
        counter = 0;
        while ( digitalRead( DHTPIN ) == laststate )
        {
            counter++;
            delayMicroseconds( 1 );
            if ( counter == 255 )
            {
                break;
            }
        }
        laststate = digitalRead( DHTPIN );
 
        if ( counter == 255 )
            break;
 
        if ( (i >= 4&& (i % 2 == 0) )
        {
            dht11_dat[j / 8<<= 1;
            if ( counter > 16 )
                dht11_dat[j / 8|= 1;
            j++;
        }
    }
 
    if ( (j >= 40&&
         (dht11_dat[4== ( (dht11_dat[0+ dht11_dat[1+ dht11_dat[2+ dht11_dat[3]) & 0xFF) ) )
    {
        f = dht11_dat[2* 9. / 5. + 32;
        printf"Humidity = %d.%d %% Temperature = %d.%d C (%.1f F)\n",
            dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
    }else  {
        printf"Data not good, skip\n" );
    }
}
 
int main( void )
{
    printf"Raspberry Pi wiringPi DHT11 Temperature test program\n" );
 
    if ( wiringPiSetup() == -1 )
        exit( 1 );
 
    while ( 1 )
    {
        read_dht11_dat();
        delay( 1000 ); 
    }
 
    return(0);
}
 
cs

 

컴파일 명령은 아래와 같다.

 

$ gcc -o dht11temp dht11temp.c -lwiringPi -lwiringPiDev

 
실행을 하면 온도와 습도가 잘 표시됨을 확인한다.
 
1
2
3
4
5
6
7
8
9
10
11
12
$ sudo ./dht11temp
 
Raspberry Pi wiringPi DHT11 Temperature test program
Humidity = 36.0 % Temperature = 30.0 C (86.0 F)
Humidity = 36.0 % Temperature = 30.0 C (86.0 F)
Humidity = 36.0 % Temperature = 30.0 C (86.0 F)
Data not good, skip
Humidity = 36.0 % Temperature = 30.0 C (86.0 F)
Data not good, skip
cs

 

아래는 LCD 에 온도와 습도를 출력하는 코드이다. 1602 LCD를 사용하는 방법은 여기 - http://fishpoint.tistory.com/1953  

라즈베리 파이로 16*2 LCD 1602 사용하기 16x2 LCD Hello World using C -

에 상세하게 소개되어 있다. 참고하기 바란다.

 

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

 

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <wiringPi.h>
#include <lcd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
#define MAXTIMINGS 85
#define DHTPIN 7
 
int lcd;
int dht11_dat[5= {00000};
 
void read_dht11_dat()
{
    uint8_t laststate = HIGH;
    uint8_t counter = 0;
    uint8_t j = 0, i;
    float f; 
 
    dht11_dat[0= dht11_dat[1= dht11_dat[2= dht11_dat[3= dht11_dat[4= 0;
 
    pinMode(DHTPIN, OUTPUT);
    digitalWrite(DHTPIN, LOW);
    delay(18);
        
    digitalWrite(DHTPIN, HIGH);
    delayMicroseconds(40);
        
    pinMode(DHTPIN, INPUT);
 
    for (i = 0; i < MAXTIMINGS; i++)
    {
        counter = 0;
        while (digitalRead(DHTPIN) == laststate)
        {
            counter++;
            delayMicroseconds(1);
            if (counter == 255)
            {
                break;
            }
        }
        laststate = digitalRead(DHTPIN);
 
        if (counter == 255)
            break;
        if ((i >= 4&& (i % 2 == 0))
        {
            dht11_dat[j / 8<<= 1;
            if (counter > 16)
                dht11_dat[j / 8|= 1;
            j++;
        }
    }
 
    if ((j >= 40&& (dht11_dat[4== ((dht11_dat[0+ dht11_dat[1+ dht11_dat[2+ dht11_dat[3]) & 0xFF)))
    {
        f = dht11_dat[2* 9. / 5. + 32;
 
        lcdPosition(lcd, 00);
        lcdPrintf(lcd, "Humidity: %d.%d %%\n", dht11_dat[0], dht11_dat[1]);
 
        lcdPosition(lcd, 01);
        //lcdPrintf(lcd, "Temp: %d.0 C", dht11_dat[2]); //Uncomment for Celcuis
        lcdPrintf(lcd, "Temp: %.1f F", f); //Comment out for Celcuis
    }
}
 
int main(void)
{
    int lcd;
    wiringPiSetup();
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
        
    while (1)
    {
        read_dht11_dat();
        delay(1000); 
    }
 
    return(0);
}
 
 
 
 
cs

 

참고자료 와 이미지 출처 http://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-the-raspberry-pi/

 

 

 

 

 

반응형