Main Content

readEchoTime

Read echo time of ultrasound waves

Description

example

time= readEchoTime(ultrasonicObj)measures the time taken by the ultrasound waves reflected from the object to reach the sensor, represented by the connection objectultrasonicObj. The distance to the object is computed from thetimemeasured, using the equationdistance = (time x speedOfSound)/2.

Examples

collapse all

Create anarduinoobject with theUltrasoniclibrary.

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.

时间= readEchoTime (ultrasonicObj);

Calculate the distance to the object, by assuming the speed of sound to be 344 m/s.

distance = 344*time/2;

Input Arguments

collapse all

Ultrasonic sensor connection object, specified as anultrasonicobject.

Output Arguments

collapse all

The time taken for the ultrasound waves reflected from the object to reach the sensor, measured in seconds. If the name-value pairOutputFormatis specified while creating theultrasonicobject, 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 returnsInf.

Data Types:double|duration

Version History

Introduced in R2019a