replicating Arduino pulseIn() on Matlab
Show older comments
Long story short I have to replicate the Arduino pulseIn() command in MatLAB reading in an acceleration X and Y component. I was doing research and found that this link:
had a similar request but the "adiosrv.pde" file is outdated and the native command (tic, toc) for Matlab is not consistent enough to give me the accurate readings I need for this project. Apparently the .pde file has changed name and/or been split up into multiple documents and I cannot find it anywhere for the life of me. any input?
Answers (1)
Nagarjuna Manchineni
on 21 Jun 2017
0 votes
I see that the solution in the link that you are referring was written for Giampiy's legacy Arduino library, which I think was the case with the pde file. It is not applicable now.
To measure distance using the new Arduino object, there is an Ultrasonic Sensor add-on library on matlabcentral, but you need to first install a 3rd party library as explained in the instructions on that page.
If this Add-on does not work with your Ultrasonic Sensor, you can use it as a template to create your own custom add-on library.
Here's the doc for creating custom add-on libraries for Arduino.
I hope this helps!
6 Comments
Welid Benchouche
on 28 Aug 2021
Hi sir, i am trying to run my ultrasonic sensor with the Raspberry pi 3 model B board, i tried mimicing the code from python but it doesnt work , it keeps increamenting nonstop.here is my code for it
clc;
a = raspi;
trig = 26; % gpio 26
echo = 16; % gpio 16
global x y
x = 0; y = 0;
configurePin(a, trig,'DigitalOutput')
configurePin(a, echo,'digitalinput')
pause(3)
disp('starting')
writeDigitalPin(a,trig,1)
disp('trig is on')
pause(0.00001)
writeDigitalPin(a,trig,0)
while 1
while readDigitalPin(a,echo) == 0
tic; x = toc;
break;
end
while readDigitalPin(a,echo) == 1
y = toc;
break;
end
dur = -(y-x)
distance = dur * 17150
% dur = y-x;
% distance = dur * 17000;
% distance = round(distance, 3);
% sprintf('Distance : %f cm',distance)
% % writeDigitalPin(a,trig,0)
% % while true
% % writeDigitalPin(a,trig,0)
% % pause(0.1)
% % writeDigitalPin(a,trig,1)
% % pause(0.001)
% % writeDigitalPin(a,trig,0)
% % while readDigitalPin(a,echo) == 0
% % tic;pulse_start = toc;
% % end
% % while readDigitalPin(a,echo) == 1
% % pulse_end = toc;
% % return
% % end
% % end
% % pulse_duration = pulse_end - pulse_start
% % distance = pulse_duration * 17000
% % if pulse_duration >=0.01746
% % disp('time out')
% %
% % elseif distance > 300 && distance==0
% % disp('out of range')
% % end
% % distance = round(distance, 3);
% % sprintf('Distance : %f cm',distance)
% %
% clear x y distance ans dur
end
Walter Roberson
on 28 Aug 2021
tic; x = toc;
I am unclear as to the reason for that?
while readDigitalPin(a,echo) == 0
tic; x = toc;
break;
end
Especially since you "break" as soon as the condition is met.
x = toc at that point would only measure the time since the tic that is in the same line, which should be a fairly minor amount of time; I don't think your code is really intended to measure MATLAB internal efficiency there ??
Welid Benchouche
on 28 Aug 2021
could you please tell me how to convert this python code to matlab ? because this is my problem
don't worry about the GPIO. .. stuff, i just need to calculate the time
def distance():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
Walter Roberson
on 28 Aug 2021
a = raspi;
trig = 26; % gpio 26
echo = 16; % gpio 16
configurePin(a, trig,'DigitalOutput')
configurePin(a, echo,'digitalinput')
pause(3)
disp('starting')
writeDigitalPin(a,trig,1)
disp('trig is on')
pause(0.00001)
writeDigitalPin(a,trig,0)
while 1
StartTime = tic;
while readDigitalPin(a,echo) == 0
StartTime = tic;
end
StopTime = toc;
while readDigitalPin(a,echo) == 1
StopTime = tic;
end
TimeElapsed = StopTime - Starttime;
distance = dur * 17150;
%at this point do something with distance
end
But this could also be written as
a = raspi;
trig = 26; % gpio 26
echo = 16; % gpio 16
configurePin(a, trig,'DigitalOutput')
configurePin(a, echo,'digitalinput')
pause(3)
disp('starting')
writeDigitalPin(a,trig,1)
disp('trig is on')
pause(0.00001)
writeDigitalPin(a,trig,0)
while 1
while readDigitalPin(a,echo) == 0
%do nothing -- that is, loop until it reads differently
end
StartTime = tic;
while readDigitalPin(a,echo) == 1
%do nothing -- that is, loop until it reads differently
end
StopTime = toc;
TimeElapsed = StopTime - Starttime;
distance = dur * 17150;
%at this point do something with distance
end
Welid Benchouche
on 28 Aug 2021
Dear @Walter Roberson
Both codes won't work, because the code is stuck in this loop and never passes it
while readDigitalPin(a,echo) == 0
%do nothing -- that is, loop until it reads differently
end
it never detects when the echo pin goes high for some reason.
Walter Roberson
on 29 Aug 2021
It would be interesting to instead use
maxtry = 25;
got_one = false;
for trynum = 1 : maxtry
value = readDigitalPin(a,echo);
if value ~= 0; got_one = true; break; end
disp('pin was 0')
end
if ~got_one
error('No 0 found in %d tries', maxtry);
end
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!