This code is for use on the FPGA of a myRIO, and is used to get a time of flight reading from a 4 pin ultrasound sensor, such as the HC-SR04
Using pulse width measurements, this code measures the response of an ultrasound module to give the time to receive a response. This time directly corresponds to distance, and can be converted by dividing by 1,000,000 (to convert to seconds) then multiplying by the 340 (the speed of sound).
There are two loops in this code, one to manage the pulse width measurement of the module's ECHO line, and another to trigger the module on the TRIG line.
Steps to Implement or Execute Code
Create new project
Add this code to the FPGA target of a myRIO
Ensure that your hardware matches the connectors used in the VI (In this case DIO3 on connector C is used for ECHO and DIO7 on connector C is used for TRIG)
Compile FPGA code
Run FPGA code
(This code will not run unless under and FPGA target in a project)
Additional Images or Video
Matt H Applications Engineer National Instruments UK
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.