Jump to content
  • 0

USB1208LS Simple Ultrasonic Sensor HC-SR04 Not Working (No Echo Response)


chepox

Question

I have a simple connection to an ultrasonic sensor (HC-SR04) connected to an USB 1208LS, I have tried 5 different sensors from different manufacturers and have had no success on getting them to work so I can mostly rule out the sensors. 

The connection diagram is very straightforward and is as follows:

 

PortA Channel 0 -> Trigger pin#21

PortB Channel 8 (or per documentation PortA Channel 15) -> Echo pin#39

Vcc = 5V coming from pin #30

Gnd coming from pin #40

 

Here is my python script to run a the sensor. I took mostly from this site and adjusted for the USB-1208LS.

 

import time
from mcculw import ul
from mcculw.enums import DigitalPortType, DigitalIODirection
from mcculw.device_info import DaqDeviceInfo
from console.console_examples_util import config_first_detected_device

board_num = 0
dev_id_list = []
config_first_detected_device(board_num, dev_id_list)

bit_num_out = 0
bit_num_in = 15
bit_value = 0

daq_dev_info = DaqDeviceInfo(board_num)
dio_info = daq_dev_info.get_dio_info()
port_out = dio_info.port_info[0]
port_in = dio_info.port_info[1]
ul.d_config_port(board_num, DigitalPortType.FIRSTPORTA, DigitalIODirection.OUT)
ul.d_config_port(board_num, DigitalPortType.FIRSTPORTB, DigitalIODirection.IN)


def distance():
    # set Trigger to HIGH
    bit_value_low = 0
    bit_value_high = 1

    StartTime = 0
    StopTime = 0

    ul.d_bit_out(board_num, port_out.type, bit_num_out, bit_value_high)
    time.sleep(0.00001)
    ul.d_bit_out(board_num, port_out.type, bit_num_out, bit_value_low)

    # save StartTime
    while ul.d_bit_in(board_num, port_out.type, bit_num_in) == 0:
        StartTime = time.time()
        print(f'Echoe = 0: {StartTime}')

    # save time of arrival
    while ul.d_bit_in(board_num, port_out.type, bit_num_in) == 1:
        StopTime = time.time()
        print(f'Echo = 1: {StopTime}')

    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance_traveled = (TimeElapsed * 34300) / 2

    return distance_traveled

if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            print (f"Measured Distance = {dist:0.1f}")
            time.sleep(1)

        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")

The script endlessly loops on "while ul.d_bit_in(board_num, port_out.type, bit_num_in) == 0:" so it seems that the echo never receives the HIGH value back to exit the loop. 
Output:
fxppyGxBbhLF6DAn.png.13f8c12108b940528965851e0a879351.png

I have no idea what could be wrong. Does anyone have an idea on what I might be doing wrong? I have already tried to troubleshoot this over and over with no success. 

I would greatly appreciate any help. 

 


 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 1

The USB-1208LS is not compatible with the HC-SR04 ultrasonic sensor. The digital IO needs to be faster to generate the 10uS pulse. If you had another way of generating the 10uS trigger pulse, you could use our USB-CTR04 can accurately measure the echo pulse width.

Link to comment
Share on other sites

  • 0

So the issue is the trigger is not fast enough? I also have a raspberry pi 3b that had the sensor connected to it and was working fine when all of the sudden it stopped working. I used the USB 1208LS to try and see if the raspberry was the problem. 

Now, I do not need to measure the echo pulse width. Just to time the response from trigger pulse until I get a high signal incoming from the sensors echo pin. 

So there is no way of getting a 10uS pulse out of the USB1208LS? I am changing the bit to high and then low to create the pulse using a timer:

    ul.d_bit_out(board_num, port_out.type, bit_num_out, bit_value_high)
    time.sleep(0.00001)
    ul.d_bit_out(board_num, port_out.type, bit_num_out, bit_value_low)

So its not changing fast enought??

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...