from machine import Pin, ADC
import time
# Setup for analog sensor (potentiometer connected to GPIO 34)
analog_pin = ADC(Pin(34)) # Set pin 34 as ADC input
analog_pin.atten(ADC.ATTN_11DB) # 11dB attenuation gives full 0-3.3V range
led = Pin(2, Pin.OUT)
while True:
# Reading the analog value from potentiometer (0-4095)
analog_value = analog_pin.read()
# Convert the analog value to a voltage (0-3.3V)
voltage = analog_value * (3.3 / 4095.0)
print("Analog Value:", analog_value, " Voltage:", voltage, "V")
if analog_value > 1300:
led.on()
print("LED ON")
else:
led.off()
print("LED OFF")
# Small delay between readings
time.sleep(2)