CPython or MicroPython support for PWM distance sensors

Hi, I’m considering replacing the current pair of analog Sharp Analog Distance Sensors (#2474) I’m using as they’ve been discontinued.

I was wondering if anyone had found any CPython support code or library for converting the output from the Distance Sensors with Pulse Width Output (e.g., #4064, #4071 and #4079) into a numeric value. I understand that an MCU may be more suitable than a Raspberry Pi but I’d rather prefer the latter if possible since that’s what is running my robot OS.

Thanks!

Hello.

Unfortunately, I do not have a CPython example program that will run on a Raspberry Pi computer, but here is a MicroPython example (which we know works on a Raspberry Pi Pico) written earlier this year by one of our summer interns:

# Example MicroPython program for reading the
# Pololu Distance Sensors with Pulse Width Output
import time
import machine
from machine import Pin

# Change the '17' to the digital pin number connected to the sensor's OUT pin.
sensorPin = Pin(17, Pin.IN)

while True:
    # Measure the pulse width.
    t = machine.time_pulse_us(sensorPin, 1)

    if t == 0:
        # time_pulse_us() did not detect the start of a pulse within 1 second.
        print("timeout")

    elif t > 1850:
        # No detection.
        print("Out of range")

    else:
        # Valid pulse width reading. Converts pulse width in microseconds to 
        # distance in millimeters.
        # Uncomment the appropriate line for your sensor.
        d = (t - 1000) * 3 / 4 #4064 - 50 cm max sensor
        #d = (t - 1000) * 2     #4071 - 130 cm max sensor
        #d = (t - 1000) * 4     #4079 - 300 cm max sensor

        # Limit minimum distance to zero.
        if d < 0:
            d = 0

        print(str(d) + " mm")

If you make a CPython example, please consider posting it here since that might be a useful resource for others.

- Patrick

Thanks very much! I don’t have the hardware in stock but am considering an order, so if I do end up with one to try I’ll certainly post any code I create here.

I think the tricky part in CPython (that’s clearly simple in MicroPython) would be that first line:

t = machine.time_pulse_us(sensorPin, 1)

But I’ve written a handler for motor encoders in CPython so it’s not exactly rocket science. Thanks for the basic algorithm, it’s a good guide.

1 Like

Here’s a CPython DistanceSensor class and a test script that handles this using pigpiod:

Note: Over the next few days I’ll be modifying this to better integrate it with the rest of my robot OS, so I’ll be adding configuration, logging and a few other features which will make it incompatible with other peoples’ use of the code. So if you’re in that category and don’t want to use these features of my robot OS (which I assume includes most people) you’ll probably want the first posted version of these files, not the latest version.

1 Like

@maltheim, thank you for sharing your DistanceSensor class and test script! Good luck with your project!

- Patrick