Ultrasonic Range Finder HC-SR04 with T7 | LabJack
 

Ultrasonic Range Finder HC-SR04 with T7

6 posts / 0 new
Last post
Samuel Lazerson
lazersos's picture
Ultrasonic Range Finder HC-SR04 with T7

I'm trying to read delay from an ultrasonic rangefinder (HC-SR04) using a T7.  I'm using DI05 for the TRIG signal and DI01 for the ECHO signal (PWM Input).  Now my Python code looks like this

from labjack import ljm
import time

# LabJack Stuff
labjack_type='ANY'
labjack_connection='ANY'
labjack_ident='ANY'

# Connect
print('Connecting')
labjack = ljm.openS(labjack_type,labjack_connection,labjack_ident)
print('Connected')

# Set PWMIN
ljm.eWriteName(labjack, 'DIO_EF_CLOCK0_DIVISOR',8) # 80MHz/8 = 10 MHz
ljm.eWriteName(labjack, 'DIO_EF_CLOCK0_ROLL_VALUE',360000) # 360000/10MHz = 36 ms
ljm.eWriteName(labjack, 'DIO_EF_CLOCK0_ENABLE',1)
ljm.eWriteName(labjack, 'DIO1_EF_ENABLE',   0)
ljm.eWriteName(labjack, 'DIO1_EF_INDEX',    5)
ljm.eWriteName(labjack, 'DIO1_EF_OPTIONS',  0)
ljm.eWriteName(labjack, 'DIO1_EF_CONFIG_A', 0)
ljm.eWriteName(labjack, 'DIO1_EF_ENABLE',   1)

# Loop for a while seconds
n=0
while n<50:
    ljm.eReadName(labjack,'DIO1_EF_READ_A_F_AND_RESET')
    ljm.eWriteName(labjack,'DIO5',0)
    ljm.eWriteName(labjack,'DIO5',1)
    time.sleep(0.000020) # At least 10 us
    ljm.eWriteName(labjack,'DIO5',0)
    time.sleep(0.1) # Could set to ~36 ms
    result=ljm.eReadName(labjack,'DIO1_EF_READ_A_F')
    print(result*340.0/2, result)
    n = n + 1

However, I don't seem to be getting meaningful results.

LabJack Support
labjack support's picture
I would recommend setting DIO

I would recommend setting DIO_EF_CLOCK0_ROLL_VALUE to 0 (this sets it to the maximum roll value). Limiting the roll value is limiting the maximum measurable period, and typically the only time you would lower the roll value is when you are sharing the clock with a PWM output feature. Your maximum measurable period is currently 36ms, your signal period will be longer than that under your current setup.

I would also recommend removing the 10us sleep calls. The resolution of time.sleep() is dependent on the OS, and likely much lower than the desired 10us delay. The communication overhead of the write commands (without any delay) should also be longer than 10us, so you could just run them in succession without a delay.

It should not be necessary, but if you wanted to get closer to a 10us trigger pulse you would want to use eWriteNames to send all of the DIO5 write commands in one packet and use a 10us wait command to set the pulse width. This method is described further in our waveform generation application note:
https://labjack.com/support/app-notes/waveform-generation#WaitTechnique

Samuel Lazerson
lazersos's picture
Thanks for the suggestion.  I

Thanks for the suggestion.  I tried that but I'm finding the eReadName routine is returning.  It's almost like only every third call to the function is returning a meaningful value.  Like only the thrid call is actually detecting a full period.

Connecting
Connected
0.0
0.0
0.007492499891668558
429.4967346191406
429.4967346191406
0.007928100414574146
429.4967346191406
429.4967346191406
0.007465700153261423
429.4967346191406
429.4967346191406

Samuel Lazerson
lazersos's picture
I did some more digging and

I did some more digging and it appears that every third call to result=ljm.eReadName(labjack,'DIO1_EF_READ_A_F') produces a meaningful result.  Any idea what is happening here?

LabJack Support
labjack support's picture
429.4967 is the maximum value

429.4967 is the maximum value the high time when using the max roll and 10MHz clock, so I think there is something non-ideal happening, perhaps a bug, with the feature. We will investigate this a bit more and get back to you.

Samuel Lazerson
lazersos's picture
It seems to be more related

It seems to be more related to the trigger signal than the echo.  I changed the loop a bit

from labjack import ljm
import time
# LabJack Setup
# DIO0 (PWM_INPUT) -> ECHO
# DIO2 (STATE)     -> TRIG
# LabJack Stuff
labjack_type='ANY'
labjack_connection='ANY'
labjack_ident='ANY'
# Connect
print('Connecting')
labjack = ljm.openS(labjack_type,labjack_connection,labjack_ident)
print('Connected')
# Setup Clock0
ljm.eWriteName(labjack, 'DIO_EF_CLOCK0_ENABLE',0)
ljm.eWriteName(labjack, 'DIO_EF_CLOCK0_DIVISOR',8) #80MHz/8 = 10 MHz
ljm.eWriteName(labjack, 'DIO_EF_CLOCK0_ROLL_VALUE',0) # Tmax=400000/10E6 Hz = 0.04 s = 40 ms
ljm.eWriteName(labjack, 'DIO_EF_CLOCK0_ENABLE',1)
# Setup DIO0_EF
ljm.eWriteName(labjack, 'DIO0_EF_ENABLE',  0)
ljm.eWriteName(labjack, 'DIO0_EF_INDEX',   5) # PWM_INPUT
ljm.eWriteName(labjack, 'DIO0_EF_OPTIONS', 0) # Clock0
ljm.eWriteName(labjack, 'DIO0_EF_CONFIG_A', 0) #One Shot
ljm.eWriteName(labjack, 'DIO0_EF_ENABLE',  1)
# Loop for a while seconds
n=0
ljm.eReadName(labjack,'DIO0_EF_READ_A_F_AND_RESET')
ljm.eWriteName(labjack,'DIO2',0)
while n<20:
    ljm.eWriteName(labjack,'DIO2',1)
    ljm.eWriteName(labjack,'DIO2',0)
    ljm.eWriteName(labjack,'DIO2',1)
    ljm.eWriteName(labjack,'DIO2',0)
    result=ljm.eReadName(labjack,'DIO0_EF_READ_A_F')
    print(result*340.0/2,result)
    n = n + 1
ljm.close(labjack)

Which produces the correct result.  It seems that perhaps the trigger is not being registered correctly.