0

Crickit and HCSR04
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Crickit and HCSR04

by awaxman on Sat Sep 29, 2018 9:09 pm

Hi,
I am getting started with a Circuit Playground Express and Crickit on with CircuitPython and getting some strange results. I used the adafruit_hcsr04 library as guide, but that code expects pins so I had to rewrite for crickit. I am using a level shifter (I think BSS138) to get from 5v to 3.3v.

My serial results go back and forth to retrying and values like 49.8047 with distance of around 28 cm, then it jumps to multiples of 49.8 when I move out to other distances.

Any recommendation about how to debug?

Thanks,
Andy

Here is my code
Code: Select all | TOGGLE FULL SIZE
import time
from adafruit_crickit import crickit

def measure(trigger, echo, seesaw):
    timeout = 0.1
   
    seesaw.digital_write(trigger,True)
    time.sleep(0.00001)
    seesaw.digital_write(trigger,False)

    pulselen = None
    timestamp = time.monotonic()
    while not seesaw.digital_read(echo):
        if time.monotonic() - timestamp > timeout:
            raise RuntimeError("Timed out")
    timestamp = time.monotonic()
    while seesaw.digital_read(echo):
        if time.monotonic() - timestamp > timeout:
            raise RuntimeError("Timed out")
    pulselen = time.monotonic() - timestamp
    pulselen *= 1000000
   
    if pulselen >= 65535:
        raise RuntimeError("Timed out")
   
    return pulselen * 0.017
   
TRIGGER = crickit.SIGNAL2
ECHO = crickit.SIGNAL1
ss = crickit.seesaw

ss.pin_mode(TRIGGER,ss.OUTPUT)
ss.pin_mode(ECHO,ss.INPUT)

while True:
    try:
        print(measure(TRIGGER,ECHO,ss))
    except RuntimeError:
        print("Retrying")
    time.sleep(0.1)
awaxman
 
Posts: 3
Joined: Sun Nov 30, 2008 4:40 pm

Re: Crickit and HCSR04

by kevinjwalters on Fri Oct 19, 2018 8:30 am

Python isn't going to be as quick as compiled C code, the granularity you are seeing could be the result of the performance of the loop timing the pulse in CircuitPython on a humble SAMD21 processor. I think a primitive (either compiled code or hardware assisted) will be the answer here to read/measure the reply pulse, probably pulseio.PulseIn. I have not used the Crickit, will be interesting if that library works with Crickit pins.

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

Re: Crickit and HCSR04

by awaxman on Mon Oct 22, 2018 10:32 pm

I used the open pins in the CPE which is able to use pulseio. Thanks for responding.
awaxman
 
Posts: 3
Joined: Sun Nov 30, 2008 4:40 pm

Re: Crickit and HCSR04

by kevinjwalters on Tue Oct 30, 2018 8:40 pm

I noticed there's some example code for using adafruit_hcsr04 on https://learn.adafruit.com/jack-o-there ... ython-code

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

Please be positive and constructive with your questions and comments.