Ultrasonic sensor help

Beginner on pi-top, I am executing a very simple program that endlessly prints ultrasonicsensor.distance, the numbers are coming out, but it is giving me random numbers, between 0-2, no correlation between what actual distance it is facing even though it is moving, is it a common problem with solutions?

also is there a code reference library for pi-top, or does it share one with another language

thank you

Hi tedh.
What port are you using? And what code have you used to create the Ultrasonic ‘object’ in python?

Hi

thank you for the response, I have used this code: (from the further website and has worked for me before)
from pitop.pma import UltrasonicSensor
from time import sleep

name the component(s) and state which port(s) they are connected to

ultrasonic_sensor = UltrasonicSensor(“D7”)

while True:
print (ultrasonic_sensor.distance)
sleep(0.1)

i have previous kowlegde to coding and know this should work, port i just use D7 as instructed

numbers are random doubles , so its working, just randomly

There are a few things to try:

  1. Swap out the grove leads, one may be flaky
  2. Try another port (D1), and change “D7” to “D1”
  3. Buy a groove Ultrasonic sensor and check if it is a missing component. They are not expensive but you will definitely know then.

Thank you, what do people usually use to import code to pi-top so it doesnt have to be connected w vnc viewer to a laptop with a wire when the robot is racing away?