# Experiment A: Analog Sensor Reading
# Reads a voltage from a potentiometer (simulating an analog sensor)
# and prints the value to the Serial Monitor.
import machine
import utime
# 1. Initialize the ADC pin (GP26 is ADC0 on Pico)
adc = machine.ADC(26)
# 2. (Optional) Initialize an LED pin for visual feedback
led = machine.PWM(machine.Pin(16))
led.freq(1000) # Set PWM frequency to 1kHz
print("š Experiment A: Analog Sensor Started!")
print("š Turn the potentiometer knob in Wokwi and watch the values change.")
print("------------------------------------------------")
try:
while True:
# 3. Read the raw ADC value (0-65535 for Pico's 16-bit ADC)
adc_value = adc.read_u16()
# 4. Convert the raw value to a voltage (0.0 - 3.3V)
voltage = (adc_value / 65535) * 3.3
# 5. (Optional) Use the value to control LED brightness
led.duty_u16(adc_value) # Direct mapping
# 6. Print the values to the Serial Monitor
print(f"ADC: {adc_value:5d} | Voltage: {voltage:.2f}V")
# 7. Short delay for readable output
utime.sleep_ms(200)
except KeyboardInterrupt:
# 8. Safe exit: turn off LED and print message if code is stopped
led.duty_u16(0)
print("\nā
Experiment safely stopped.")