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 object ultrasonicObj. The distance to the object is computed from the time measured, using the equation distance = (time x speedOfSound)/2.

Examples

collapse all

Create an arduino object with the Ultrasonic library.

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;

Input Arguments

collapse all

Ultrasonic sensor connection object, specified as an ultrasonic object.

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 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 returns Inf.

Data Types: double | duration

Introduced in R2019a