Alright I figured it out there's an Arduino library and a separate HC-SR04 library, which has included examples for using the HR04 ultrasonic sensor.
Interfacing Arduino ultrasonic sensor with Matlab/pulse timing in Matlab?
4 views (last 30 days)
Show older comments
I'm trying to make a ball on beam system both simulated in matlab, and built in real life.
So, I'm trying to turn a servo in matlab based on input from an SR04 ultrasonic sensor, but I can't measure the pulse lengths from the SR04 sensor.
clc
clear all
a = arduino('com3', 'uno', 'Libraries', 'Servo')
s = servo(a, 'D4')
pinMode = configurePin(a,'D6')
clear s;
s = servo(a, 'D4', 'MinPulseDuration', 5.44*10^-4, 'MaxPulseDuration', 2.4*10^-3);
xcv = 1/100;
for time = 0:0.05:1
writeDigitalPin(a, 'D5', 0);
tic
pause(0.000005)
writeDigitalPin(a, 'D5', 1);
pause(0.00001)
writeDigitalPin(a, 'D5', 0);
toc
duration = toc-tic
%duration = pulseIn(D5, HIGH);
cm = (duration/2) / 29.1
fprintf('%d\n', cm)
fprintf('cm');
pause(50)
end
for angle = 0:0.05:1
writePosition(s, angle);
current_pos = readPosition(s);
current_pos = current_pos*180;
fprintf('Current motor position is %d degrees\n', current_pos);
pause(0.5);
end
My code has no problem running the servo to some random angles, but pulseIn() isn't an available function in the Arduino Matlab library as far as I could find it.
Here's my working arduino code for the Arduino IDE that outputs the distance as an integer WITHOUT using the NewPing library.
int trigPin = 5; //Trig - green Jumper
int echoPin = 6; //Echo - yellow Jumper
long duration, cm, inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = (duration/2) / 29.1;
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(30);
}
Feel free to change whatever you need to change, and if you know how to make the whole function loop back so it is constantly receiving info from the sensor and causing a change in the servo angle that would be a huge help as well.
2 Comments
Hamza Abbasi
on 25 Aug 2017
can you please tell me what should i do if i am using three ultrasonic sensors and i want to plot response of trigger and echo pulse of each sensor individually
Answers (1)
Purawin Subramaniam
on 5 Nov 2017
Did you solve your problem?
1 Comment
Madhu Govindarajan
on 7 Nov 2017
I think Robert mentions that he is using this - https://www.mathworks.com/matlabcentral/fileexchange/57898-hc-sr04-add-on-library-for-arduino
HTH, Madhu
See Also
Categories
Find more on Arduino Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!