Interfacing Ranger Ultrasonic Sensor to Rapberry Pi

I am trying to connect the Ranger Ultrasonic sensor directly to a Raspberry Pi4. My hardware connections are; 3.3v pin, gnd pin and gpio pin 5. I have checked the voltage and pin status, and all are good. I ran the following code;

#!/usr/bin/env python
import sys
sys.path.append('/home/hank/grove.py')
import time
from grove.grove_ultrasonic_ranger import GroveUltrasonicRanger
sensor = GroveUltrasonicRanger(5)
   
while True:
    distance = sensor.get_distance()
    print('{} cm'.format(distance))        
    time.sleep(1)


Nothing happens, except 1 time I did get readings when nothing was in front of the sensor. All other times no results or errors.

Hello, From your description you can check by these list:

  1. use arduino or other board to check whether your sensor is work well
  2. make sure you are running at the root permission
  3. use the termial to check whether the pin of GPIO you are using is input mode, if not use the code to set it: gpio -g mode <pin> input

I insured that GPIO 5 is input.
I did get some strange results(which I think you will find hard to believe).
If I execute the script nothing happens but if I disconnect the 3.3v supply while it is running I get some output (nothing in front of sensor).

150.35991010994746 cm
71.36525778934873 cm
178.98658226276265 cm
76.42137593236463 cm
103.82306986841662 cm
1.3195235153724407 cm
1.3236341805293643 cm
1.3359661760001347 cm
1.3236341805293643 cm

Eventually it stops responding

Are you using this one? Grove - Ultrasonic Ranger | Seeed Studio Wiki
If yes, I think you must cross-check with an Arduino.