Read echo time of ultrasound waves
Calculate Distance to Object
arduino object with the
arduinoObj = arduino('COM9','Uno','Libraries','Ultrasonic');
Updating server code on board Uno (COM9). This may take a few minutes.
Create an ultrasonic sensor connection object with the trigger pin set to D2 and echo pin set to D3.
ultrasonicObj = ultrasonic(arduinoObj,'D2','D3','OutputFormat','double')
ultrasonicObj = Ultrasonic with properties: TriggerPin: 'D2' EchoPin: 'D3'
Measure the time taken by the ultrasound waves reflected from the object to reach the sensor.
time = readEchoTime(ultrasonicObj);
Calculate the distance to the object, by assuming the speed of sound to be 344 m/s.
distance = 344*time/2;
time — echo time
positive real scalar | duration
The time taken for the ultrasound waves reflected from the object to reach
the sensor, measured in seconds. If the name-value pair
OutputFormat is specified while creating the
ultrasonic object, the function returns
echo time as a double. Otherwise, the function returns the time as a
duration. If the object is not in the range of the sensor, the function