import time
import analogio
import digitalio
import math
import board

def get_voltage(raw):
    return (raw * 3.3) / 65536

# Configuración de sensores y actuadores
pot = analogio.AnalogIn(board.GP26)
ldr = analogio.AnalogIn(board.GP27)
rele = digitalio.DigitalInOut(board.GP18)
rele.direction = digitalio.Direction.OUTPUT
adcpin = analogio.AnalogIn(board.GP28)

# Constantes del termistor
Vin = 3.30
Ro = 10000  # 10kΩ resistor
A = 0.001129148
B = 0.000234125
C = 0.0000000876741

def leer_potenciometro():
    for _ in range(20):
        getvolts = (pot.value * 3.30) / 65535
        print("Potenciometro - Dec= {:5d}, Volts= {:5.2f}".format(pot.value, getvolts))
        time.sleep(0.2)

def leer_ldr():
    for _ in range(20):
        raw = ldr.value
        volts = get_voltage(raw)
        luz = (volts / 3.3) * 100
        if volts > 1.65:
         rele.value = True
        else:
         rele.value = False 
        print(f"LDR - Dec: {raw}, Luz: {luz:.2f}%, Relé: {'ON' if rele.value else 'OFF'}")
        time.sleep(0.2)

def leer_termistor():
    for _ in range(20):
        Vout = (adcpin.value * Vin) / 65535  
        Rt = (Vout * Ro) / (Vin - Vout)
        TempK = 1 / (A + (B * math.log(Rt)) + C * math.pow(math.log(Rt), 3))
        TempC = TempK - 273.15
        print(f"Termistor - Vout: {Vout:.3f}V, Temp: {TempC:.2f}°C")
        time.sleep(0.2)

def leer_todos():
    for _ in range(20):
        getvolts = (pot.value * 3.30) / 65535
        raw = ldr.value
        volts = get_voltage(raw)
        luz = (volts / 3.3) * 100
        if volts > 1.65:
         rele.value = True
        else:
         rele.value = False 
        Vout = (adcpin.value * Vin) / 65535  
        Rt = (Vout * Ro) / (Vin - Vout)
        TempK = 1 / (A + (B * math.log(Rt)) + C * math.pow(math.log(Rt), 3))
        TempC = TempK - 273.15
        print(" Volts= {:5.2f}, LDR: {luz:.2f}%, Termistor: {TempC:.2f}°C".format(getvolts))
        time.sleep(0.2)

while True:
    print("\nMenú:")
    print("1. Leer Potenciometro")
    print("2. Leer LDR")
    print("3. Leer Termistor NTC")
    print("4. Leer los 3 sensores")
    print("5. Salir")
    opcion = input("Seleccione una opción: ")
    if opcion == "1":
        leer_potenciometro()
    elif opcion == "2":
        leer_ldr()
    elif opcion == "3":
        leer_termistor()
    elif opcion == "4":
        leer_todos()
    elif opcion == "5":
        print("Saliendo...")
        break
    else:
        print("Opción no válida. Intente de nuevo.")
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
NOCOMNCVCCGNDINLED1PWRRelay Module