Example Code

LabVIEW & DAQ: Ultrasonic Distance Measurement with HC-SR04 4 Terminal Sensor

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Hardware

  • Data Acquisition (DAQ)

    Software

  • LabVIEW

    Driver

  • NI DAQmx

Code and Documents

Attachment

Overview
The example demonstrates how interface with HC-SR04 sensor.

Description:
The HC-SR04 sensor uses sound waves to help compute the distance of an object by sending out ultrasonic sound waves to an object and measuring the time it takes to come back to the sensor.
The sensor sends out about eight 40 kHz pulses and waits for an object to bounce the waves back to the receiver.  It then outputs a high pulse proportional to the time it takes for the sound waves to be sent and received. To use the sensor, a continuous pulse train is generated by a counter output task with a set high time and low time.  A 100 uS pulse send on the TRIG line triggers the HC-SR04 module to start sending the ultrasonic pulses followed by a 100 ms low time.  A voltage high is sent on the ECHO line until the sound waves bounce back to the receiver. Since the speed of sound at sea level is known to be about 13397 inches per second we can use the high time of the ECHO line and the ratio of inches per second to get the distance traveled by the sound wave.  To get the object distance we need to divide by this by two.  This can be simplified into a single multiplication since the only unknown variable is the high time.

Steps to implement or execute code
To implement this example:

  1. Define the trigger and Echo terminal
  2. Run the VI


Requirements
Software
LabVIEW 2012 or compatible
NI-DAQmx 16.0 or compatible

Hardware
cDAQ with C series that have counter feature module or compatible DAQ devices.

 

2017-10-31_14-07-27.jpg2017-10-31_14-08-01.jpg

**This document has been updated to meet the current required format for the NI Code Exchange.**

Anthony F.
Staff Software Engineer
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
chikiz
Member
Member
on

Que complemento de software labview tengo que tener para que el ejemplo relacionado para el sensor ultrasonico HC SR04, pueda correr de forma correcta y no me envie errores al ejecutarlo

Anthony_F
NI Employee (retired)
on

Necesita un Multifunction DAQ como el PCI-6220.  Este ejemplo usa dos counters en el Multifunction DAQ, una para emitir un pulso y el otro para medir distancia.

Anthony F.
Staff Software Engineer
National Instruments
Toño SM
Member
Member
on

hay alguna otra forma de realizar un .VI utilizando ese mismo sensor pero sin utilizar Multifunction DAQ.

Toño SM
Member
Member
on

solo cuento con una LABJACK espero y me puedan ayudar a realizar este .VI utilizando esta targea de adquisicion de datos

animaster
Member
Member
on

como puedo mmunicar el sensor ultasonicocon con el arduino conectado al labview

fernando9090
Member
Member
on

se puede conectar con el daq

Siske
Member
Member
on

Hi Guys

I really stroggled a lot to get this module/code working on the DAQ card PCI-6220. The problem was that the DAQ assistent I used to find the terminal to achieve the echo signal misguided me. Thus, use pin 14 on the breakout board for 5V, use pin 2 (PFI12/P2.4) to generate the signal, use pin 7 as ground and finally pin 41 (PFI4/P1.4) to aquire the echo signal. Then the code works perfect.

Anpu
Member
Member
on

Hola, podrian enviarme quien tenga una programación de labview para medir la distancia del sesor ultrasonico hc-sr04 conectado al arduino, ya tengo el LIFA cargado al arduino, ya he comunicado el arduino con labview a un sensor de humedad, pero no logo optener datos del sensor ultrasonico, mi arduino es un mega2560 y la vercion del software del arduino es 1.0.1.

Gracias.

d20ryy
Member
Member
on

Hello, this experiment on how to use myDAQmx

Asenat
Member
Member
on

Hola, tengo una NI my daq y necesito ayuda para el programa del Sensor Ultrasonico para medir distancia 

sumit.aole
Member
Member
on

Greetings,

I have used DAQ card USB-6251 for running this program and was successful. To generate a trigger pulse CTR 0 OUT/ pin 89 /PFI12.

The only change that I found was using the CTR 1 Z  i.e. PIN 77 PF4/P1.4 to capture echo signals. Then the code works perfectly.