from machine import Pin, ADC, PWM
from time import sleep
# Configure Indicator LEDs
LED_5 = Pin(5, Pin.OUT)
LED_18 = Pin(18, Pin.OUT)
LED_19 = Pin(19, Pin.OUT)
LED_21 = Pin(21, Pin.OUT)
# Configure the ADC on Pin 35
adc = ADC(Pin(35))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT)
# Configure PWM on Pin 12
pwm0 = PWM(Pin(12))
while True:
adc_value = adc.read()
# Convert raw 12-bit value (0-4095) to voltage (0-3.3V)
volts = (3.3 * adc_value) / 4095
# Live telemetry output
print("Freq Mode: {:0.2f}V | Raw: {:4d}".format(volts, adc_value), end="\r")
if volts <= 1.0:
# Mode 1: Low Frequency, Low Duty
pwm0.freq(2000)
pwm0.duty(256)
LED_21.on(); LED_18.off(); LED_19.off(); LED_5.off()
elif 1.0 < volts <= 2.0:
# Mode 2: Medium Frequency, High Duty
pwm0.freq(5000)
pwm0.duty(767)
LED_21.off(); LED_18.off(); LED_19.on(); LED_5.off()
elif 2.0 < volts <= 3.0:
# Mode 3: High Frequency (Ultrasonic), 50% Duty
pwm0.freq(20000) # Reduced from 60k for better ESP32 stability
pwm0.duty(511)
LED_21.off(); LED_18.off(); LED_19.on(); LED_5.on()
else:
# Mode 4: "Heartbeat" / Over-voltage Alert
pwm0.freq(5)
pwm0.duty(205)
# Flash LED 18 specifically for this mode
LED_21.off(); LED_18.on(); LED_19.off(); LED_5.off()
sleep(0.1) # Small delay to prevent CPU saturation