目录
关于树莓派pico和circuitpython的更多玩法,请看树莓派pico专栏
用到的库adafruit_dht,需要导入pico才能使用,在这里下载
样例程序
进阶玩法,显示信息的温湿度计
屏幕使用见树莓派pico专栏的ssd1306oled屏幕使用
代码
效果
关于树莓派pico和circuitpython的更多玩法,请看树莓派pico专栏
使用circuitpython8.x环境,硬件dht11,电压范围3~5v
用到的库adafruit_dht,需要导入pico才能使用,在这里下载
样例程序
import time
import board
import adafruit_dhtdhtDevice = adafruit_dht.DHT11(board.GP28)while True:try:# Print the values to the serial porttemperature_c = dhtDevice.temperaturetemperature_f = temperature_c * (9 / 5) + 32humidity = dhtDevice.humidityprint("Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(temperature_f, temperature_c, humidity))except RuntimeError as error:# Errors happen fairly often, DHT's are hard to read, just keep goingprint(error.args[0])time.sleep(2.0)continueexcept Exception as error:dhtDevice.exit()raise errortime.sleep(2.0)
dhtDevice = adafruit_dht.DHT11(board.GP28)选择一个引脚作为数据读取引脚
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity默认读出的时摄氏度和湿度百分比
其他的是异常处理,输出异常信息的,可以不用加
进阶玩法,显示信息的温湿度计
dht11支持3~5v供电,所以可以使用vout引脚对它供电,3.3v引脚用来给屏幕供电,不需要额外拓展引脚
屏幕使用见树莓派pico专栏的ssd1306oled屏幕使用
代码
import time
import board
import adafruit_dht
import busio
import adafruit_ssd1306dhtDevice = adafruit_dht.DHT11(board.GP28)i2c = busio.I2C(board.GP1,board.GP0)
display_width = 128
display_height = 64
display = adafruit_ssd1306.SSD1306_I2C(display_width, display_height, i2c)
display.fill(0)while True:try:# Print the values to the serial porttemperature_c = dhtDevice.temperaturetemperature_f = temperature_c * (9 / 5) + 32humidity = dhtDevice.humidity
#清空上一次的屏幕内容display.fill(0)display.text("Temp: {:.1f} F / {:.1f} C\nHumidity: {}% ".format(temperature_f, temperature_c, humidity),0,0,1,size=1)display.show()except RuntimeError as error:# Errors happen fairly often, DHT's are hard to read, just keep goingprint(error.args[0])time.sleep(2.0)continueexcept Exception as error:dhtDevice.exit()raise errortime.sleep(.5)
显示时注意每次刷新需要清空上一次的内容,否则会出现像素糊在一起的情况