File Exchange

image thumbnail

HC-SR04 Add-On Library for Arduino

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

230 Downloads

Updated

View License

This add-on library 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 "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 (11)

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

MATLAB Release
MATLAB 9.0 (R2016a)

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

» Watch video

+arduinoioaddons/+JRodrigoTech/