라즈베리 파이로 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] = { 0, 0, 0, 0, 0 };
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] = {0, 0, 0, 0, 0};
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, 0, 0);
lcdPrintf(lcd, "Humidity: %d.%d %%\n", dht11_dat[0], dht11_dat[1]);
lcdPosition(lcd, 0, 1);
//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 (2, 16, 4, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0, 0, 0, 0);
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/
'개발자 > Raspberry Pi' 카테고리의 다른 글
| ctrl-c 프로세스 종료시 주의할 점과 SIGINT 시그널 사용법 (0) | 2016.12.08 |
|---|---|
| sudo kill -9 pid 로 발생하는 메세지 처리, kill 메세지 처리 (0) | 2016.12.08 |
| 랜케이블 하나로 가장 빠르게 라즈베리파이와 접속하는 방법 (1) | 2016.11.30 |
| 라즈베리 파이로 DHT22 온도 습도 센서 사용하기 C 언어 (6) | 2016.10.24 |
| 라즈베리파이 LCD 1602 사용하기 16x2 LCD Hello World using C (2) | 2016.10.20 |
| 파이썬 프로그래밍의 기초 (0) | 2016.10.08 |
| KT 제공 IoT Makers 에 연동시 컴파일 오류 (0) | 2016.09.27 |
| PN532 NFC RFID Module 사용자 가이드 using C (0) | 2016.09.20 |
취업, 창업의 막막함, 외주 관리, 제품 부재!
당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약,
아이디어는 있지만 구현할 기술이 없는 막막함.
우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.
이제 고민을 멈추고, 캐어랩을 만나세요!
코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.
제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!
귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.
캐어랩