from machine import Pin, ADC, PWM
import time
import dht
# Definição dos pinos
verde = Pin(15, Pin.OUT) # LED verde
azul = Pin(14, Pin.OUT) # LED azul
bot = Pin(13, Pin.IN, Pin.PULL_DOWN) # Botão com pull-down
buzzer = PWM(Pin(12)) # Buzzer
pot = ADC(Pin(26)) # Potenciômetro
sensor_dht = dht.DHT22(Pin(16)) # Sensor DHT22
while True:
if bot.value(): # Se o botão for pressionado
resp = input("Digite P para potenciômetro e D para DHT22: ").lower()
if resp == "p":
valor_pot = pot.read_u16()
tensao = (valor_pot / 65535) * 3.3
print(f"Potenciômetro: {tensao:.2f}V")
verde.on()
azul.off()
buzzer.freq(1000)
buzzer.duty_u16(30000)
time.sleep(0.5)
buzzer.duty_u16(0)
elif resp == "d":
try:
sensor_dht.measure()
temp = sensor_dht.temperature()
umid = sensor_dht.humidity()
print(f"Temperatura: {temp:.1f}°C, Umidade: {umid:.1f}%")
azul.on()
verde.off()
buzzer.freq(1000)
buzzer.duty_u16(30000)
time.sleep(0.5)
buzzer.duty_u16(0)
except OSError:
print("Erro ao ler o sensor DHT22")
else:
print("Opção inválida")
time.sleep(0.1) # Pequeno delay para evitar leitura contínua