溫濕度感測器

濕度溫度感測器電路

MicroPython 程式碼範例 運行在 Raspberry Pi Pico 2 W 微控制器上 在麵包板上連接溫濕度感測器模組 (DHT11) 和 OLED 顯示模組 (SSD1306)。 測量並顯示相對濕度和環境溫度,單位分別為攝氏和華氏度

溫濕度感測器測量環境溫度和空氣濕度. 在機器人領域,大多數電子元件都內建了溫度感測器。. 它們可用於熱保護,例如在溫度超過安全限度時自動關閉機器人。. 你知道嗎?智慧型手機內部約有8個不同的溫度感測器,分別用於測量螢幕溫度、背部溫度、電池溫度、CPU溫度等等。濕度感測器則用於監測智慧型手機或智慧手錶內部的冷凝情況。.

這是DHT11感測器. 我們導入 dht 庫並如下所示設定 GP28 引腳:

1
2
3
4
5
6
from time import sleep
from machine import Pin
from oled import OLED
from dht import DHT11

humidity_temp_sensor = DHT11(Pin(28))

然後,在主循環中,我們使用測量函數來獲取溫度和濕度。. 我們可以使用簡單的數學公式將攝氏度轉換為華氏度。.

1
2
3
4
5
6
7
while True:
    humidity_temp_sensor.measure()
    humidity = humidity_temp_sensor.humidity()
    temp_c = humidity_temp_sensor.temperature()
    temp_f = temp_c * 9/5 + 32
    oled.print(f"{humidity:.0f}% Humidity", f"{temp_c:.1f} Degrees C", f"{temp_f:.1f} Degrees F")
    sleep(0.5)

若要執行完整腳本,請在 Thonny 中建立一個名為 humidity_temp_sensor.py 的新檔案。. 將以下程式碼複製並貼上到您建立的文件中。.

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
print("\nHumidity Temp Sensor")

# DESCRIPTION

# A humidity and temperature sensor measures the ambient temperature and air humidity.
# In robotics, temperature sensors are built into most electronic components. 
# They can be used for thermal protection, such as to automatically shutdown a robot 
# when temperatures exceed safe limits. Did you know that a smartphone has about 8 different
# temperature sensors in it, measuring screen temperature, back temperature, 
# battery temperature, CPU temperature, etc.? Humidity sensors monitor condensation 
# inside a smartphone or smartwatch.

# CONNECTIONS REQUIRED

# Connect Humidity and Temperature Sensor (KY-015) module 
# (it includes a big blue waffle block)
#   Pins: + => VCC, OUT => GP28, - => GND

from time import sleep
from machine import Pin
from oled import OLED
from dht import DHT11

humidity_temp_sensor = DHT11(Pin(28))
oled = OLED(scl_pin=15, sda_pin=14)

try:
    while True:
        humidity_temp_sensor.measure()
        humidity = humidity_temp_sensor.humidity()
        temp_c = humidity_temp_sensor.temperature()
        temp_f = temp_c * 9/5 + 32
        oled.print(f"{humidity:.0f}% Humidity", f"{temp_c:.1f} Degrees C", f"{temp_f:.1f} Degrees F")
        sleep(0.5)

except KeyboardInterrupt:
    print("\nInterrupted by user!")

finally:
    oled.reset()

回到頂部

讓教師和學生更容易參與動手發明創造.
© 2026 Inventor Pi, Inc.
Inventor Pi, Inc.是一家註冊的 501(c)(3) 非營利組織