DHT(数字湿度和温度)传感器是低成本数字传感器,带有电容式湿度传感器和热敏电阻,用于测量周围空气。它们具有处理模数转换并提供 1 线接口的芯片。较新的传感器还提供 I2C 接口。
DHT11(蓝色)和 DHT22(白色)传感器提供相同的 1 线接口,但 DHT22 需要一个单独的对象,因为它具有更复杂的计算。DHT22 的湿度和温度读数具有 1 位小数分辨率。DHT11 两者都有整数。
手头上只有DHT11,现在以DHT11为例子。
DHT11 连上Pin4。
在原来ssd1306显示的基础上加上dht11的显示程序,整体程序如下:
from machine import I2C,Pin
from ssd1306 import SSD1306_I2C
import machine
import time
import dht
sda_pin=Pin(15,Pin.PULL_UP)
scl_pin=Pin(16,Pin.PULL_UP)
i2c = I2C(1,sda=sda_pin, scl=scl_pin, freq=800_000)
print(i2c.scan())
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
d = dht.DHT11(machine.Pin(4))
def display():
oled.text(" !\"#$%&'()*+,-./", 0, 0)
oled.text("0123456789:;<=>?", 0, 8)
oled.text("@ABCDEFGHIJKLMNO", 0, 16)
oled.text("PQRSTUVWXYZ[\]^_", 0, 24)
oled.text("`abcdefghijklmno",0, 32)
oled.text("pqrstuvwxyz{|}~",0, 40)
oled.show()
def testAscii():
Ascii = ''
for i in range(32,127):
Ascii = Ascii + chr(i)
for i in range(128,256):
Ascii = Ascii + chr(i)
return Ascii
def display_Ascii():
oled.fill(0)
oled.text(testAscii()[0:16], 0, 0)
oled.text(testAscii()[16:32],0, 8)
oled.text(testAscii()[32:48],0, 16)
oled.text(testAscii()[48:64],0, 24)
oled.text(testAscii()[64:80],0, 32)
oled.text(testAscii()[80:95],0, 40)
oled.show()
def display_hellowold():
oled.fill(0)
oled.fill_rect(0, 0, 32, 32, 1)
oled.fill_rect(2, 2, 28, 28, 0)
oled.vline(9, 8, 22, 1)
oled.vline(16, 2, 22, 1)
oled.vline(23, 8, 22, 1)
oled.fill_rect(26, 24, 2, 4, 1)
oled.text('MicroPython', 40, 0, 1)
oled.text('SSD1306', 40, 12, 1)
oled.text('OLED 128x64', 40, 24, 1)
def dispDht():
while True:
display_hellowold()
d.measure()
temper = d.temperature()
Humi = d.humidity()
strT = "TEMP:" + str(temper)
strH = "HUMI:" + str(Humi)
oled.text(strT,20,40,1)
oled.text(strH,20,50,1)
oled.show()
time.sleep(1)
if __name__=="__main__":
time.sleep(2)
display_hellowold()
dispDht()
程序效果:
