Use MartyPy to read the distance sensor add-on on your Marty V2.
Before you begin:
Import martypy and connect to your robot. Choose USB or WiFi:
import martypy
# Connect by USB.
my_marty = martypy.Marty("USB")
# Alternatively, connect by WiFi using your Marty's IP address.
# my_marty = martypy.Marty("wifi", "192.168.0.5")
In the Python shell, run:
my_marty.get_distance_sensor()
For Marty V2, get_distance_sensor() returns an integer distance reading in millimetres (mm) for the closest detected object. It returns 0 if no distance sensor is found.