File Exchange

image thumbnail

Legacy HC-SR04 Add-On Library for Arduino

Interface with HC-SR04 ultrasonic sensors connected to Arduino hardware in MATLAB.

207 Downloads

Updated 08 May 2019

View License

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 (2019). 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 (44)

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."

what´s the sample time in this library?

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

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!

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.

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".

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' }

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

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?

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

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?

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++.

Yasir Jamal

Hi Chris,
Try this manual addon custom library. I am able to get it working now, however the original libraries written for this sensor only restricts to measuring distances within 50 cm range and i am looking into it now.

https://www.mathworks.com/help/supportpkg/arduinoio/ug/create-ultrasonic-add-on-to-continuously-stream-distance.html

Does this library works for Genuino MKR1000?

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?

can I install the Ultrasonic Arduino library with MATLAB2015b?

Xinke Wang

how about windows?

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

why only measure it 51 cm

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\" .

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


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

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.

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.

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

Updates

1.2.1

Added Legacy to title

1.2

Updated description and release compatibility to specify that the library is only supported for releases through R2018b.

1.1

Updated description

1.1.0.0

Added license addendum and updated MathWorks http references to https

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

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor

+arduinoioaddons/+JRodrigoTech