Der Code
Hier ist ein Beispielcode für eure Programmierung in Micropython in der IDE Thonny auf dem Raspberry Pi Pico.
from machine import Pin, I2C
#
import time
import utime
import framebuf
#
import dht
from ssd1306 import SSD1306_I2C
#
# OLED pixel definition (WxH)
WIDTH = 128
HEIGHT = 32
# I2C0 pin assignments
SCL = 5
SDA = 4
# DHT22 sensor
sensor = dht.DHT22(Pin(2))
# Initialize I2C0, Scan and Debug print of SSD1306 I2C device address
i2c = I2C(0, scl=Pin(SCL), sda=Pin(SDA), freq=200000)
# Initialize OLED
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
# Initialize LED (Pin 25)
led = machine.Pin(25,machine.Pin.OUT)
# Toggle LED functionality
def BlinkLED(timer_one):
led.toggle()
# Initialize timer_one. Used for toggling the on board LED
timer_one = machine.Timer()
# timer_one initialization for on board blinking LED at 200ms interval
timer_one.init(freq=5,mode=machine.Timer.PERIODIC,callback=BlinkLED)
while True:
time.sleep_ms(250)
sensor.measure()
oled.fill(0)
temp = sensor.temperature()
hum = sensor.humidity()
oled.text("Temp. {} C".format(temp),5,5)
oled.text("Feucht. {:.0f} % ".format(hum),5,15)
#Show display
oled.show()
# Wait for Five seconds. Then proceed to collect next sensor reading.
time.sleep_ms(5000)