Skip to main content

Section 4.1 SHARP distance sensors

  • Connect the red wire to 5V ("VIN" on the ESP32)

  • Connect the black wire to ground

  • Connect the signal wire (yellow) to any analog input pin

from machine import Pin, ADC

# This can be connected to any of the analog-capable input pins
distancesensor = ADC(Pin(35))

# The ADC reads 0-1V by default, but the SHARP IR sensor returns
# a voltage between 0 and 2.5V-ish.  Configure the internal voltage
# divider so that we can read 0 to 2.5V.
distancesensor.atten(ADC.ATTN_11DB)

# Get a single reading from the sensor
raw = distancesensor.read()

# You can use this directly, or look at the sensor datasheet
# to see how to convert it to an actual distance

print(f"Read distance sensor {raw}")