ESP32 Micropython编程(Thonny)dht11温湿度

180it 2022-05-30 PM 914℃ 0条

dht11温湿度

一、硬件介绍和连接
dht11 数字式温湿度计(一线制总线通信)

DHT11温湿度计 ESP32开发板
VCC 3V3 / 5V
DATA D23
GND GND
二、示例代码
在shell终端依次输入以下程序,观察其返回值

import machine
i2c = machine.I2C(sda=machine.Pin(21),scl=machine.Pin(22))
from ssd1306 import SSD1306_I2C
OLED = SSD1306_I2C(128, 64, i2c)
import dht
d = dht.DHT11(machine.Pin(23))
d.measure()
d.temperature()
d.humidity()

三、温湿度在oled上显示

import machine
import time
import ssd1306

i2c = machine.I2C(sda=machine.Pin(21),scl=machine.Pin(22))
#i2c.scan()
from ssd1306 import SSD1306_I2C
OLED = SSD1306_I2C(128, 64, i2c)

import dht
d = dht.DHT11(machine.Pin(23))

def display_reads():
    d.measure()
    temp = d.temperature()
    humi = d.humidity()
    OLED.fill(0)
    OLED.text('TEMP:  *C',0,0)
    OLED.text(str(temp),40,0)
    OLED.text('HUMI:  %R',0,16)
    OLED.text(str(humi),40,16)
    OLED.show()
    print('TEMP:', temp, '℃', ' ', 'HUMI:', humi, '%R')

interval = 1000
start = time.ticks_ms()

while True:
    if time.ticks_ms() - start >= interval:
        display_reads()
        start = time.ticks_ms()

观察led显示即可

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

ESP32 Micropython编程(Thonny)dht11温湿度