라즈베리파이

DHT11 Interfacing with Raspberry Pi

기하 2022. 6. 21. 03:15

https://www.electronicwings.com/raspberry-pi/dht11-interfacing-with-raspberry-pi

 

DHT11 Interfacing with Raspberry Pi | Raspberry Pi

Interface a single wire DHT11 digital humidity and temperature sensor with Raspberry Pi. Then, print or display relative humidity in percentage and temperature in degree Celsius on a window.

www.electronicwings.com

 

 

 

  • DHT11 센서는 단일 와이어를 통해 직렬로 습도 및 온도 값을 측정하고 제공합니다.
  • 상대 습도(20~90% RH)를 0~50°C 범위에서 섭씨 온도로 측정할 수 있습니다.
  • 4개의 핀이 있습니다. 그 중 하나는 직렬 형식의 데이터 통신에 사용됩니다.
  • 다른 TON 및 TOFF의 펄스는 논리 1 또는 논리 0 또는 시작 펄스 또는 프레임 끝으로 디코딩됩니다.

인터페이스 다이어그램

라즈베리 파이 3와 DHT11 인터페이스

예시

  • 여기서는 DHT11 센서를 Raspberry Pi 3와 인터페이스하고 터미널에 습도 및 온도를 표시합니다.
  • GitHub의 Adafruit에서 제공하는 DHT Sensor Python 라이브러리를 사용할 것입니다. 
    Adafruit Python DHT Sensor 라이브러리는
    raspberry Pi 또는 Beaglebone Black의 습도 및 온도를 읽기 위해 만들어졌습니다. 
    DHT11, DHT22 또는 AM2302와 같은 DHT 시리즈 센서용으로 개발되었습니다.

여기에서 Adafruit DHT 센서 라이브러리를 다운로드하십시오 .

  • 다음 명령을 실행하여 라이브러리의 압축을 풀고
    다운로드한 라이브러리의 동일한 루트 디렉터리에 설치합니다.
sudo python setup.py install
  • 라이브러리와 해당 종속성이 설치되면
    examples 폴더에 보관된 라이브러리에서 simpletest라는 이름의 예제 스케치를 엽니다.
  • 이 코드에서 라즈베리 파이는 DHT11 센서에서 습도와 온도를 읽어 터미널에 출력합니다.
     그러나 값을 한 번만 읽고 표시합니다. 
    그래서 여기서 우리는 계속해서 값을 출력하도록 프로그램을 변경했습니다.

메모:

  • 이 라이브러리의 센서 변수에 적절한 센서 유형을 할당하십시오. 
    여기서는 DHT11 센서를 사용합니다.

sensor = Adafruit_DHT.DHT11

  • 누군가 센서 DHT22를 사용하는 경우 
    위에 표시된 센서 변수에 Adafruit_DHT.DHT22 를 할당해야 합니다.
  • 또한 Raspberry Pi에 대한 Beaglebone 핀 정의 및 주석 해제 핀 선언을 주석 처리합니다.
  • 그런 다음 핀 번호를 할당합니다. DHT 센서의 데이터 핀이 연결된 곳입니다. 
    여기서 DHT11 센서의 데이터는 GPIO4에 연결됩니다. 위의 인터페이스 다이어그램과 같이.

파이썬 프로그램

#!/usr/bin/python

# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import Adafruit_DHT

# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT11

# Example using a Beaglebone Black with DHT sensor
# connected to pin P8_11.
#pin = 'P8_11'

# Example using a Raspberry Pi with DHT sensor
# connected to GPIO4.
pin = 4

# Try to grab a sensor reading.  Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
'''
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None and temperature is not None:
    print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
    print('Failed to get reading. Try again!')
'''

DHT11 출력