File Exchange

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.

191 Downloads

Updated 08 May 2019

View Version History

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 (2020). 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 .

Comments and Ratings (50)

Ogheneovo Idolor

You could simply use the built-in functions using this procedure :
1. Download and unzip contents. Copy 'ultrasonic' folder from the contents folder to your 'documents/arduino/libraries/' folder
2. Re-program arduino board and ensure you upload 'ultrasonic' library in 'Adruino Support Hardware for Matlab' setup
3. Use the following code to measure distance:

% Create arduino object with the add-on library, here port is 'COM6' and board is 'MKR1010'
a = arduino('COM6', 'MKR1010', 'Libraries', 'Ultrasonic');
% Create ultrasonic object, here trigger pin is 'D3' and echo pin is 'D4'
sensor = ultrasonic(a, 'D3', 'D4');
% Obtain sensed distance
dist = readDistance(sensor)

For more information see link below:
https://www.mathworks.com/help/supportpkg/arduinoio/ultrasonic-sensor.html?s_tid=CRUX_lftnav

Tuqa Ahmed

The block of ultrasonic sensor don't add to the simulink, how i can add it ?

Haniyeh Slmz

Hi How can I plot the sensor in a long time?

le chinh

Hi,

Kindly, use Ultrasonic library shipping with MATLAB Support Package for Arduino starting MATLAB R2019a to connect to the ultrasonic sensor. The documentation link for the ultrasonic sensor is available here: https://in.mathworks.com/help/supportpkg/arduinoio/ref/arduinoio.ultrasonic.html.

Thanks
MATLAB Hardware Team
MathWorks

Gustavo Hernandez

Hi. Can anyone help me? When I run the program this warning appears:
"Warning: The Pins property will be removed for Addons. Add the property in object class definition and Inherit class arduinoioaddons.JRodrigoTech.HCSR04 from matlabshared.addon.LibraryBase instead. For more information, see Arduino Add-On document. Click here to turn off this warning."
I don't get what to do.

Aidel Fahmi

CORRECTION TO PREVIOUS SUBMISSION.
This is the error

"Cannot program board Due (COM6). Please make sure the board is supported and the port and board type are correct. For more information,
see Arduino Hardware Troubleshooting."

Rishab Shekar

Luis Guajardo

what´s the sample time in this library?

Abhijeet Sharma

Hi
I was able to add the Ultrasonic library. Follow these steps:
1)install arduino support package for matlab and simulink
2)download the hcsro4 arduino library from the link below
https://github.com/JRodrigoTech/Ultrasonic-HC-SR04/archive/master.zip
and add it to arduino libraries folder inside Ultrasonic folder
3)Download add-on library for hcsro4 in matlab .
4)Now connect your arduino board and when you are intializing arduino your current folder should be the folder where your ultrasonic sensor add-on for matlab is present.
for eg. if your ultrasonic sensor add-on for matlab is present in Document/MATLAB/ Add-on/Toolboxes/Ultrasonic/code
so that should be your current folder
and when you will write command listArduinoLibraries you will se that 'JRodrigoTech/HCSR04' has been added to you libraries.One thing to remember that after download ultrasonic sensor add-on for matlab you should change its floder name to 'Ultrasonic'.

tinh ngo vu

Naman Shukla

Ben Ronayne

Hello
I have tried to include this Library now many times and with no success. I have followed the instructions step by step and when i check to see if the library is included via 'listArduinoLibraries' I get 'ans =

6×1 cell array

{'Adafruit/MotorShieldV2'}
{'I2C' }
{'RotaryEncoder' }
{'SPI' }
{'Servo' }
{'ShiftRegister' }

If someone figures out how to fix this could you let us all know!

Lucas Ivankio

Hello Everybody!!
I was able to resolve the problem " The Arduino font 'Ultrasonic / Ultrasonic.h' for the libraries 'JRodrigoTech / HCSR04' can not be found. Install the 3P library source first and try again."
On the file "HCSR04.m" we don't need add the Ultrasonic/Ultrasonic.h cause we are adding the file "HCSR04.h" and he's responsible to call "Ultrasonic.h" from the arduino library.
So on the file "HCSR04.m", line ArduinoLibraryHeaderFiles = { } We can leave this parameter empty.
Tell me if this solution works for you guys.

elhadj diallo

Hi Jerod
You can try this:
sensorR = addon(a, 'JRodrigoTech/HCSR04', 'D13', 'D12');
DistR = readDistance(sensorR);
Clear sensorR;
sensorM = addon(a, 'JRodrigoTech/HCSR04', 'D4', 'D3');
DistM = readDistance(sensorM);
Clear sensorM;
sensorL = addon(a, 'JRodrigoTech/HCSR04', 'D7', 'D6');
DistL = readDistance(sensorL);
Clear sensorL;

Jerod Chou

Hello i want to extract value from multiple sensors
i used how the sample shown to set up the sensors
sensorR = addon(a, 'JRodrigoTech/HCSR04', 'D13', 'D12');
sensorM = addon(a, 'JRodrigoTech/HCSR04', 'D4', 'D3');
sensorL = addon(a, 'JRodrigoTech/HCSR04', 'D7', 'D6');
but the result is that sensorL's value will overwrite all other 2 sensor's value
how can i set up so that i can extract value from each sensor separately?

Jerod Chou

Separately* i mean

Wen Yan

Why does it only display 51 cm?

Mbakwa Ndi

Hi!
For all those who have downloaded and installed all the libraries but still cannot get their arduino board to work, follow the steps below. Skip those you've done already.

-install arduino support package for matlab.
-install arduino support package for simulink.
-download the hcsro4 arduino library from the link below
https://github.com/JRodrigoTech/Ultrasonic-HC-SR04/archive/master.zip
-extract the zip to Documents\Arduino\libraries\" and rename it to 'Ultrasonic'.
Note: if the exracted folder has two folder levels then you must delete one. see the paths below;

Correct path:
"Documents\Arduino\libraries\Ultrasonic". then you see the ". h" file,...

Wrong path:
Documents\Arduino\libraries\Ultrasonic\Ultrasonic".

Khee Lim Low

Khee Lim Low

Help!
I have gone through the process of installing the library but when i used listArduinoLibraries functions
all that appeared was

{'Adafruit/MotorShieldV2'}
{'I2C' }
{'RotaryEncoder' }
{'SPI' }
{'Servo' }
{'ShiftRegister' }

Devansh Mittal

Devansh Mittal

Hi Leharika, I am having the same issue. Can you please tell me if you got it rectified?

Leharika Naidu

Hello!
I have installed this library as well as the JRodrigoTech file which I pasted in the Arduino library. But the program isn't running.It says:
Updating server code on board Uno (COM3). Please wait (might take a few minutes).
Error using HCSR04Example (line 25)
Cannot program board Uno (COM3). Please make sure the board is supported and the port and board type are correct. For more
information, see Arduino Hardware Troubleshooting.
MATLAB is detecting my board when I create an arduino object. It displays the port and type of the board. Can someone tell me what's wrong? How do I rectify this?

Armen Abadjian

Is it possible to use two ultrasonic sensors? If so how?

ABIMELEC BAENA

Does anyone have the solution for this?

The Arduino font 'Ultrasonic / Ultrasonic.h' for the libraries 'JRodrigoTech / HCSR04' can not be found. Install the 3P library
source first and try again.

vladimir so

HELP!!!
query

Did the unzipped folder copy it inside the library folder of the matlab installation or in arduino?

I have copied everywhere but when writing: listArduinoLibraries in matlab I only get it:
'Adafruit / MotorShieldV2'
     'I2C'
     'SPI'
     'Servo'

antonio A

HELP!!!!!! sombody knows if it works only arduino original board? im using a generic board and doesn't work, I aready installed the library and when I type listArduinoLibraries: 'Adafruit/MotorShieldV2'
'I2C'
'JRodrigoTech/HCSR04'
'RotaryEncoder'
'SPI'
'Servo'
'ShiftRegister'

Then whe I tried to create an object doesn't work:
Cannot program board Mega2560 (COM3). Please make sure the board is supported and the port and board type are correct. For
more information

antonio A

can I install the Ultrasonic Arduino library with MATLAB2015a?

alberto zani

Hi
Can I use two ultrasonic sensor (Hc-sr04) at the same time?
thanks

Yasir Jamal

Actually the way to work around the 50 cm range is to update the library files in c++.

Aishwarya M V

Does this library works for Genuino MKR1000?

Christopher Drain

Hey Paul B,

I am using an Arduino Mega2560 and running into the same problem as Yasir. Is there any chance you can post the code you created for the Mega? Any help would be greatly appreciated. Thank You!

Yasir Jamal

Hi Paul,
Can you help me out on how to get this library working? I have the Add-on in a different folder and the Arduino library with Ultrasonic folder name in a different folder (is this a problem)?
When i run the list for listArduinoLibraries, i see the library for the sensor but when i try to communicate, i get the following error.

The Arduino source 'Ultrasonic/Ultrasonic.h' for libraries 'JRodrigoTech/HCSR04' cannot be found. Install the 3P library source first and
try again.

Paul B

I've this library working. I was just trying to run the example code which was hard coded for the UNO. Whipped up my own code for a the Mega and worked like a charm.

However, I also seem to only get a range of 0-51cm. for my project I would like to be able to measure within +/-5mm in the 0-50cm range so the range isn't an issue. This library rounds to the nearest cm. Is there a way to modify for a finer resolution or is it a hardware limitation?

Paul B

Does this only work with the Uno? I'm attempting to prototype on a Mega because my Uno in currently in use.

Asad Asif

i have downloaded the library zip file where to place it and how to install it?

zhang yingpeng

can I install the Ultrasonic Arduino library with MATLAB2015b?

Xinke Wang

how about windows?

mostafa osman

Can I connect to matlab more than 1 ultrasonic using this library.
Thanks in advance

ömer Yalcin

why only measure it 51 cm

ömer Yalcin

if you get eror :'Ultrasonic/Ultrasonic.h' for
libraries 'JRodrigoTech/HCSR04'
cannot be found. Install the 3P
library source first and try
again.first name of download folder change with ‘Ultrasonic’. Second you must copy donwload file to
Documents\Arduino\libraries\" .

Riccardo Salviati

Too little range of measuring....only 51cm!!!

abhishek goswami


Sorry, I made all, that you wrote here, but I have following error:
The Arduino source
'Ultrasonic/Ultrasonic.h' for
libraries 'JRodrigoTech/HCSR04'
cannot be found. Install the 3P
library source first and try

Yasitha Rajapaksha

Could you please give clear instructions on how to install the add-on to MATLAB? Also explain how that toolbox file comes into play and how to install it.

Andrew Bolduc

Works great but only at close range.

The HC-SR04 sensor has a range of 4 meters. This package cannot read further than ~40cm. Maybe there's a setting or modification to increase the code's range? I haven't been able to find a solution.

Grigoriy Yashin

Sorry, I made all, that you wrote here, but I have following error:
The Arduino source
'Ultrasonic/Ultrasonic.h' for
libraries 'JRodrigoTech/HCSR04'
cannot be found. Install the 3P
library source first and try
again.

kamal hasan

when i wrote listArduinoLibraries
i got
ans =

'Adafruit/MotorShieldV2'
'I2C'
'JRodrigoTech/HCSR04'
'SPI'
'Servo'

but when i connect the arduino and the ultrasonic sensor and wrote
a=arduino i got

Cannot program board Mega2560 (/dev/tty.usbmodem1411). Please make sure the board is supported and the port and
board type are correct.
why the matlab cannot read the library of ultrasonic
knowing that the library is found inside the arduino folder/libraries

kishore

how to measure distance from echo signal using simulink??? plz help me

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