From 35ed226edc82b387d1e61de866e69caf27a71d75 Mon Sep 17 00:00:00 2001 From: westnife3 Date: Wed, 27 Aug 2025 17:01:05 +0900 Subject: [PATCH] refs #1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DHT11 온도, 습도 센서소스 --- .gitignore | 2 +- README.md | 5 +++++ tutorial/1.dht11_sensor.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tutorial/1.dht11_sensor.py diff --git a/.gitignore b/.gitignore index 5d381cc..f295d3d 100644 --- a/.gitignore +++ b/.gitignore @@ -158,5 +158,5 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ diff --git a/README.md b/README.md index 296f1d8..b1e7de3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # raspberryPiModuleExpansionTutorial + + + +![](https://cdn.mkaidevops.xyz/easy_mod_pi/easy_module_pi_main.png) + diff --git a/tutorial/1.dht11_sensor.py b/tutorial/1.dht11_sensor.py new file mode 100644 index 0000000..94bd3fb --- /dev/null +++ b/tutorial/1.dht11_sensor.py @@ -0,0 +1,38 @@ +import time +import board +import adafruit_dht + +# DHT11 센서가 연결된 GPIO 핀 번호를 설정합니다. +# board.D4는 GPIO 4를 의미합니다. +# 다른 핀에 연결했다면 board.D{핀번호} 로 변경하세요. +dhtDevice = adafruit_dht.DHT11(board.D4) + +while True: + try: + # 센서에서 온도 (섭씨)와 습도 값을 읽어옵니다. + temperature_c = dhtDevice.temperature + humidity = dhtDevice.humidity + + # 섭씨 온도를 화씨 온도로 변환 (선택 사항) + temperature_f = temperature_c * (9 / 5) + 32 + + # 값을 출력합니다. + print( + "온도: {:.1f} C / {:.1f} F 습도: {}%".format( + temperature_c, temperature_f, humidity + ) + ) + + except RuntimeError as error: + # DHT 센서 읽기 오류는 흔하게 발생할 수 있습니다. + # 이런 오류가 발생하면 에러 메시지를 출력하고 2초 후에 다시 시도합니다. + print(error.args[0]) + time.sleep(2.0) + continue + except Exception as error: + # 다른 예외가 발생하면 센서를 정리하고 프로그램을 종료합니다. + dhtDevice.exit() + raise error + + time.sleep(2.0) # 2초마다 센서 값을 읽습니다. +