0

Error reading HCSR04 on Raspberry Pi Pico
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Error reading HCSR04 on Raspberry Pi Pico

by Tzarls on Sun Feb 14, 2021 8:00 pm

Hi. I'm getting incorrect readings when using an HCSR04 Ultrasonic sensor with a Raspberry Pi Pico, using the latest Circuitpython. I'm using this code:

Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
from time import sleep, monotonic, monotonic_ns

trigger = digitalio.DigitalInOut(board.GP2)
trigger.direction = digitalio.Direction.OUTPUT

echo = digitalio.DigitalInOut(board.GP3)
echo.direction = digitalio.Direction.INPUT

while True:
    trigger.value = True
    sleep(0.00001)
    trigger.value = False

    while not echo.value:
        pass

    start_time = monotonic_ns()

    while echo.value:
        pass

    end_time = monotonic_ns()
    print ("Distancia")
    print (((end_time - start_time)/1000)/58)

    sleep(0.2)


(I know there's a library that can be used with this sensor but it also gives me the incorrect readings, and besides it mainly uses this same method for the readings.)

The code I posted gives me a distance of 0 when really close to the sensor. Then distance changes to 16.8373 from about 5cm to about 20cm. After that I get 33,6746, and after that around 50....

Not being an expert at using CIrcuitpython I tried the same with Micropython, using the following code:

Code: Select all | TOGGLE FULL SIZE
from machine import Pin
from time import sleep, ticks_us

trigger = Pin(2, Pin.OUT)
echo = Pin(3, Pin.IN)

print ("Start")

while True:
    trigger.value(True)
    sleep(0.00001)
    trigger.value(False)

    while not echo.value():
        pass

    start_time = ticks_us()

    while echo.value():
        pass

    end_time = ticks_us()

    print ("Distancia")
    print ((end_time - start_time)/58)

    sleep(0.2)


This code gives me reasonably accurate readings. So, is there anything wrong in my Circuitpython code that I'm not seeing? Or is this a problem with the Pico port of CP? Any ideas will be appreciated.

Thanks!

Tzarls
 
Posts: 5
Joined: Fri Mar 22, 2019 10:15 am

Re: Error reading HCSR04 on Raspberry Pi Pico

by kattni on Tue Feb 16, 2021 5:15 pm

time.sleep() is not good for a 10 microsecond delay in CircuitPython. You could consider including the following in your code:
Code: Select all | TOGGLE FULL SIZE
import microcontroller
microcontroller.delay_us(10)

However that might not work well either.

Something like this might be better suited to pulsein - https://circuitpython.readthedocs.io/en ... io.PulseIn

kattni
 
Posts: 91
Joined: Fri Aug 18, 2017 6:33 pm

Re: Error reading HCSR04 on Raspberry Pi Pico

by Tzarls on Tue Feb 16, 2021 6:19 pm

Thanks for the idea. Using delay_us(10) works just like with sleep, in the sense that it triggers the HCSR04 to send an ultrasonic pulse. It still reads the wrong echo time value though.

Using PulseIn doesn't work because apparently there's no pulseio module included in the Circuitpython version for Pico. In fact I see the HCSR04 library tries to use that module if available and if it isn't then it uses the same polling method I'm using in the code I posted. Can pulseio be added manually?

On the other hand, if the code should wait until my echo pin goes Low after getting High, why is the pin assigned to echo getting Low instantly for certain distances? Because for distances from 0 to around 16cm it always reads 0, which means that the echo pulse is instanteneous (the moment it goes High and the moment it goes Low are the same). Maybe there's some timing problem with CircuitPython on the Pico?

Tzarls
 
Posts: 5
Joined: Fri Mar 22, 2019 10:15 am

Re: Error reading HCSR04 on Raspberry Pi Pico

by tannewt on Wed Feb 17, 2021 12:27 pm

pulseio will be enabled once it's implemented. It hasn't been implemented. :-)

It certainly sounds like a timing problem. I don't think it's worth digging too much into when we should spend that time implementing pulseio. Unfortunately, there are higher priority tasks at the moment.

tannewt
 
Posts: 2189
Joined: Thu Oct 06, 2016 8:48 pm

Re: Error reading HCSR04 on Raspberry Pi Pico

by Tzarls on Wed Feb 17, 2021 2:20 pm

Ok. So, just to recap: whenever one needs to track Pin state changes (High to Low or the other way around) and since there's no interrupt support, pulseio (once implemented) is the way to go because polling might not work (at least on the Pico), right?

I totally understand your time might be invested in some other things right now. I'm just worried thinking about what other interactions with external hardware modules might be affected by this kind of problem.

Tzarls
 
Posts: 5
Joined: Fri Mar 22, 2019 10:15 am

Re: Error reading HCSR04 on Raspberry Pi Pico

by tannewt on Thu Feb 18, 2021 3:13 pm

Tzarls wrote:Ok. So, just to recap: whenever one needs to track Pin state changes (High to Low or the other way around) and since there's no interrupt support, pulseio (once implemented) is the way to go because polling might not work (at least on the Pico), right?


It all depends on the timing resolution you want. pulseio can usually get within a few microseconds IIRC because it generally uses a C interrupt or timer hardware to function. Polling can likely do hundreds of microsecond resolution but I haven't tried it. Polling is usually adequate if you are just measuring human interaction because humans are slow.

Tzarls wrote:I totally understand your time might be invested in some other things right now. I'm just worried thinking about what other interactions with external hardware modules might be affected by this kind of problem.


HCSR04 and DHT tend to fall into that bucket along with infrared. We have a few folks doing RC control signals as well. Generally for distance measuring and temp sensing we recommend using an I2C based solution because it's much easier to manage.

If you are curious, to measure broad impact you can grep the libraries in the bundle repo to find uses of different CP modules. You'll find most use I2C.

tannewt
 
Posts: 2189
Joined: Thu Oct 06, 2016 8:48 pm

Re: Error reading HCSR04 on Raspberry Pi Pico

by kevinjwalters on Fri Feb 19, 2021 7:50 am

@kattni The CircuitPython library for this uses time.sleep(0.00001): https://github.com/adafruit/Adafruit_Ci ... 04.py#L133

kevinjwalters
 
Posts: 883
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.