Ultrasonic Sensor HC-SR04 in Matlab

Hello everybody,

ive started to play a little bit around with my arduino and some sensors. With the help of the supportpackages for arduino i was able to connect the arduino mega 2560 to matlab an to simulink.

I want to turn off/on 2 leds in dependency of the distance of my hand. I know the physical theory how the ultrasonic waves work but i am not able to read a pin. I dont want to code anything in arduino sketcher. I trie to use only matlab/simulink.

I tried to to a simple sketch in simulink to see if i get a signal but the Display doesnt shows any reaction.
I uploaded a screenshot of my simulink model and the wiring.

I hope someone can help me with my problem. Sorry if my english is not so good, im no native speaker.

There was also a similiar statement by the first answer in this topic. He also couldnt receive or send a ping.
http://forum.arduino.cc/index.php?topic=124450.0

Look at the steps that need to be taken in order to use a ping sensor to measure distance.

The trigger pin needs to be turned on and then off, with a specific interval between turning the pin on and off.

Then, the echo pin is read continuously, using pulseIn(), until the state changes, indicating that an echo has been received.

To do that from Matlab is going to require some fancy coding, not just turning the trigger pin on and expecting to read anything meaningful from the state of the echo pin.

I think this is excactly what i did in that example in simulink....or am i wrong?
With the pulse generator i set the input to high an then to low. While the input measures all the time.
But the problem is that the display always sticks at 0, so it seems like i cant really measure anything, although it works in arduino sketcher.

I think this is excactly what i did in that example in simulink....or am i wrong?

I can't see where the element of time is a factor in the simulink code.

It appears to turn pin 2 (the trigger pin?) on permanently. It then appears to read the state of pin 3 (the echo pin) continuously. It does not seem to matter how long it takes for the echo pin to go high, and that is critical for measuring distance.

Yes pin 2 is trigger and pin 3 is echo.

As you can see in the attached file the pulse generator creates a pulse with the time period of 15 samples and a pulse width of 10 samples and an amplitude of 1. That means during 5 samples the pulse is low an during 10 samples the pulse is high. Because i set a sample time of 1/100 s ...we have a time duration of about 0.15 seconds per time period.

As you can see in the attached file

No, I can't. All I see is a big white area.

So, you think you are toggling the trigger pin, causing it to send a series of pulses.

How are you measuring how long it takes for the echo pin to go HIGH?

I uploaded the File again, hope it works. :confused:
As you can see in the description this is just a try to see if the functions of the ultrasonic sensors work because it didnt work in matlab. If i could measure at least a signal, i would do the rest in matlab or think about a solution in simulink.
But at this point i even cant measure any signal.

But at this point i even cant measure any signal.

Load the standard ping sensor code on the Arduino, to see if the sensor is working.

PaulS:
Load the standard ping sensor code on the Arduino, to see if the sensor is working.

I tried this already and it works. So the sensor itself should be fine.

You will have to build your own block to interface with this sensor. This submission on MATLAB Central http://www.mathworks.com/matlabcentral/fileexchange/51956-auto-flush has the block you are looking for. Hope that helps.