from machine import Pin, ADC
import time
# Setup for analog sensor (potentimeter 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) # Connect LED to pin 2
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 to prevent rapid reads
time.sleep(0.1)