from machine import Pin, ADC
import time
# Set up ADC
adc = ADC(Pin(34)) # GPIO34
adc.atten(ADC.ATTN_0DB) # Full-scale voltage is 3.3V
adc.width(ADC.WIDTH_12BIT) # 12-bit resolution
# Set up LED
led = Pin(2, Pin.OUT)
# Define the threshold value for turning the LED on or off
THRESHOLD = 2048 # Midpoint for 12-bit ADC (0-4095)
while True:
# Read ADC value
adc_value = adc.read()
# Control LED based on ADC value
if adc_value > THRESHOLD:
led.on() # Turn LED on
else:
led.off() # Turn LED off
# Wait for a short period
time.sleep(0.1)