###########################
### Code in MicroPython ###
###########################

# GP2Y0A21
# Sharp reflective optical sensor to measure distance.

from machine import ADC # For hardware functions
import time # For time functions

distance = ADC(26) # Initialize ADC on pin GP26 (ADC0)

while True: # Infinite loop
    raw_value = distance.read_u16() # Reads value of ADC0 in 16 bits (0–65535)
    voltage = raw_value / 65535 * 3.3 # Convert to volts
    print("ADC raw:", raw_value, "Voltage:", round(voltage,2)) # Prints in shell
    time.sleep(0.25) # Delay