from machine import Pin, SoftI2C
import ssd1306
import onewire, ds18x20
import time
# Configuração do display OLED usando SoftI2C
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Configuração do sensor DS18B20 com resistor de pull-up
ds_pin = Pin(14)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
# Configuração do relé
relay = Pin(12, Pin.OUT)
# Encontrar o endereço do sensor
roms = ds_sensor.scan()
print('Found DS devices: ', roms)
# Configuração dos botões com pull-up interno
button_up = Pin(16, Pin.IN, Pin.PULL_UP)
button_down = Pin(15, Pin.IN, Pin.PULL_UP)
# Definição das temperaturas mínima e máxima
min_temp = -2.0
max_temp = 0.0
def check_buttons():
global min_temp, max_temp
if not button_up.value(): # Botão pressionado
min_temp += 0.5
max_temp += 0.5
time.sleep(0.2) # Debounce delay
if not button_down.value(): # Botão pressionado
min_temp -= 0.5
max_temp -= 0.5
time.sleep(0.2) # Debounce delay
while True:
check_buttons()
ds_sensor.convert_temp()
time.sleep_ms(750)
temp = ds_sensor.read_temp(roms[0])
if temp > max_temp:
relay.on() # Liga o compressor
elif temp < min_temp:
relay.off() # Desliga o compressor
oled.fill(0)
# Exibe a temperatura atual na primeira coluna
oled.text('{:.1f}'.format(temp), 0, 0, 2) # Temperatura atual com fonte maior
oled.text('Cº', 110, 2) # 'Cº' na parte superior direita
oled.text('Temp:', 0, 30) # Rótulo "Temp:"
# Exibe as temperaturas mínima e máxima na segunda coluna
oled.text('Min: {:.1f}C'.format(min_temp), 65, 20)
oled.text('Max: {:.1f}C'.format(max_temp), 65, 40)
oled.show()
time.sleep(1)