import machine, onewire, ds18x20, time
from machine import I2C, Pin
from time import sleep
from ssd1306 import SSD1306_I2C
# Inisialisasi I2C untuk OLED (SCL=21, SDA=22)
i2c = I2C(0, scl=Pin(21), sda=Pin(20), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# Inisialisasi sensor suhu DS18B20 (pin 28)
ds_pin = machine.Pin(28)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()
print('Found DS devices:', roms)
# Inisialisasi relay (pin 16)
relay = Pin(16, Pin.OUT)
relay.value(0) # Awal relay mati
# Loop utama
while True:
ds_sensor.convert_temp()
time.sleep_ms(750)
# Baca suhu
for rom in roms:
suhu = ds_sensor.read_temp(rom)
print("Suhu: {:.1f} C".format(suhu))
# Logika relay
if suhu < 25:
relay.value(1)
status = "Relay ON"
elif 25 <= suhu <= 30:
relay.value(0)
status = "Normal"
else:
relay.value(0)
status = "Normal"
# Tampilkan di OLED
oled.fill(0)
oled.text("Monitoring Suhu", 0, 0)
oled.text("Suhu: {:.1f} C".format(suhu), 0, 16)
oled.text("Status: {}".format(status), 0, 32)
oled.show()
sleep(1)