File Exchange

image thumbnail

HC-SR04 Add-On Library for Arduino

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

426 Downloads

Updated

View License

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.

MATLAB R2016a or higher and MATLAB Support Package for Arduino Hardware are required.

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

Comments and Ratings (16)

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

Paul B (view profile)

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

Paul B (view profile)

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 Wing

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

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

Updated description

1.1

Added license addendum and updated MathWorks http references to https

MATLAB Release
MATLAB 9.0 (R2016a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today

+arduinoioaddons/+JRodrigoTech/