# DHT sensor simulation with display on Pico running CircuitPython
#
# DHT sensor:
# https://learn.adafruit.com/dht/dht-circuitpython-code
#
# Use circuitpython in wokwi simulation: https://docs.wokwi.com/guides/circuitpython
#
# Learn more:
# https://www.cytron.io/tutorial/read-and-display-environment-sensor-data-using-raspberry-pi-pico-and-circuitpython
# https://learn.adafruit.com/adafruit-oled-featherwing/python-circuitpython-wiring
# https://learn.adafruit.com/adafruit-oled-featherwing/python-usage
# https://docs.circuitpython.org/projects/ssd1306/en/latest/
#
import board
import time
import busio
import displayio
import terminalio
import adafruit_dht
from adafruit_display_text import label
import adafruit_displayio_ssd1306

# (re)initialize the display bus
displayio.release_displays()

# define the pins to be used for i2c communication:
i2c = busio.I2C(scl=board.GP3, sda=board.GP2)
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

# declare the display:
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64)

# Make the display context
display_context = displayio.Group()
display.show(display_context)

# declare DHT sensor object:
dht = adafruit_dht.DHT22(board.GP16)

# draw label for temperature:
ttext_label = label.Label(terminalio.FONT, text="Temperature:", color=0xFFFF00, x=20, y=5)
display_context.append(ttext_label)

# draw temperature label:
temperature = "XX.X"
temperature_label = label.Label(terminalio.FONT, text=temperature, color=0xFFFF00, x=20, y=18)
display_context.append(temperature_label)
temperature_C_label = label.Label(terminalio.FONT, text="C", color=0xFFFF00, x=47, y=18)
display_context.append(temperature_C_label)

# draw label for humidity:
htext_label = label.Label(terminalio.FONT, text="Humidity:", color=0xFFFF00, x=20, y=30)
display_context.append(htext_label)

# draw humidity label:
humidity = "XX"
humidity_label = label.Label(terminalio.FONT, text=humidity, color=0xFFFF00, x=20, y=43)
display_context.append(humidity_label)
humidity_perc_label = label.Label(terminalio.FONT, text="%", color=0xFFFF00, x=33, y=43)
display_context.append(humidity_perc_label)

while True:
    try:
        # get temperature and format it:
        temperature = "{:.1f}".format(dht.temperature)
        # update temperature in label:
        temperature_label.text = temperature

        # get humidity and format it:
        humidity = "{:.0f}".format(dht.humidity)
        # update humidity in label:
        humidity_label.text = humidity

    except RuntimeError as e:
        # Reading doesn't always work! Just print error and we'll try again
        print("Reading from DHT failure: ", e.args)

    # refresh every 5 seconds:
    time.sleep(5)
$abcdeabcde151015202530354045505560fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
r1:1
r1:2