image thumbnail

Legacy HC-SR04 Add-On Library for Arduino

version 1.2.1 (85.7 KB) by MathWorks MATLAB Hardware Team
Interface with HC-SR04 ultrasonic sensors connected to Arduino hardware in MATLAB.

17.2K Downloads

Updated 08 May 2019

View License

Editor's Note:

For R2019a and later, this functionality is included with the MATLAB Support Package for Arduino Hardware. Users of MATLAB R2019a and later versions should not use this add-on library.

This add-on library is only for use in MATLAB R2016a through R2018b.

This add-on is supported for MATLAB R2016a - R2018b. For R2019a and later, this functionality is included with the support package.

This add-on extends MATLAB Support Package for Arduino Hardware to read distance measurements from HC-SR04 ultrasonic sensors. With this add-on, you can read distance in meters or travel time in seconds to calculate the distance more precisely.

Sample usage:
-------------------
% Create arduino object with the add-on library
a = arduino('COM38', 'Uno', 'Libraries', 'JRodrigoTech/HCSR04');
% Create ultrasonic object
sensor = addon(a, 'JRodrigoTech/HCSR04', 'D12', 'D13');
% Obtain sensed distance
dist = readDistance(sensor);

It includes documentation and an example that demonstrates the use with HC-SR04 sensor to get sensed distance.

Important: Before using this add-on library in MATLAB, you need to install the Ultrasonic Arduino library. Here are the instructions:
1. Download the zip file from https://github.com/JRodrigoTech/Ultrasonic-HC-SR04/archive/master.zip
2. Unpackage the zip into local directory and rename the folder to ‘Ultrasonic’.
3. Move the ‘Ultrasonic’ folder into the "libraries" folder inside your Arduino sketchbook folder:
On Windows, the default path is "My Documents\Arduino\libraries\"
On Mac, the default path is "~/Documents/Arduino/libraries/"
On Linux, the default path is "/home/<username>/Arduino/libraries"

Troubleshooting tips:
1. After installing this add-on library and the required Arduino library, type "listArduinoLibraries" in MATLAB to see if the add-on is properly installed.
2. Wire up the Arduino board and the add-on device properly before creating the object to avoid a connection error in MATLAB.
3. Refer to the documentation for details on usage and syntax. It can be found under Supplemental Software in the product documentation.

Feel free to contact the MATLAB Hardware Team if you have questions about this add-on library:
http://www.mathworks.com/matlabcentral/profile/contact/4922363-mathworks-matlab-hardware-team

Cite As

MathWorks MATLAB Hardware Team (2021). Legacy HC-SR04 Add-On Library for Arduino (https://www.mathworks.com/matlabcentral/fileexchange/57898-legacy-hc-sr04-add-on-library-for-arduino), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2016a
Compatible with R2016a to R2018b
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

+arduinoioaddons/+JRodrigoTech