How to control Raspberry Pi to measure distance using SRF05 ultrasonic sensor on MATLAB?

4 views (last 30 days)
I am trying to control my Raspberry Pi 3 Model B to use SRF05 ultrasonic sensor on MATLAB. This is my hardware connection as shown in the image - Vcc to 5V - Trig to GPIO 21 - Echo is connected to a voltage divider circuit to ensure the input is around 3.3V, the input is connected to GPIO 20 - GND to GND
Here is my code:
clear all
mypi=raspi;
pin=mypi.AvailableDigitalPins;
trig=pin(18);
echo=pin(17);
configureDigitalPin(mypi,trig,'output');
configureDigitalPin(mypi,echo,'input');
writeDigitalPin(mypi,trig,0);
while 1
writeDigitalPin(mypi,trig,0);
pause(2)
writeDigitalPin(mypi,trig,1);
pause(0.00001)
writeDigitalPin(mypi,trig,0);
while readDigitalPin(mypi,echo)==0
continue
end
tic
while readDigitalPin(mypi,echo)==1
continue
end
duration= toc
distance=duration*17150
end
I try this code, but the result is not shown, please help me to correct the code. I'm a beginner in MATLAB. Thank you everyone!

Answers (0)

Categories

Find more on MATLAB Support Package for Raspberry Pi Hardware 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!