Main Content

ultrasonic

Connection to ultrasonic sensor on Arduino hardware

Description

This object represents a connection to an ultrasonic sensor connected to the Arduino® hardware. To measure the distance to an object with the ultrasonic sensor, use this object with the functions listed in Object Functions.

Creation

Description

example

ultrasonicObj = ultrasonic(arduinoObj,triggerPin) creates an ultrasonic sensor connection for a three-pin sensor connected to the specified trigger pin on the Arduino hardware.

example

ultrasonicObj = ultrasonic(arduinoObj,triggerPin,echoPin) creates an ultrasonic sensor connection for a four-pin sensor connected to the specified pins on the Arduino hardware.

example

ultrasonicObj = ultrasonic(arduinoObj,triggerPin,echoPin,Name,Value) creates an ultrasonic sensor connection for a four-pin sensor with the specified pins on Arduino hardware. For a three-pin sensor,the echo pin is the same as the trigger pin.

Input Arguments

expand all

Arduino hardware connection created using arduino, specified as an arduino object.

Digital pin number on the Arduino hardware that is connected to the trigger pin on the ultrasonic sensor, specified as a scalar.

Example: ultrasonicObj = ultrasonic(arduinoObj,'D2')

Data Types: char | string

Digital pin number on the Arduino hardware that is connected to the echo pin on the ultrasonic sensor, specified as a scalar. If echoPin isn't specified, the connection object sets the echo pin to the same value as triggerPin.

Data Types: char | string

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Format of the echo time output specified as a double.

Example: ultrasonicObj = ultrasonic(arduinoObj,'D2','D3','OutputFormat','double')

Object Functions

readDistanceDistance to object in front of ultrasonic sensor
readEchoTimeRead echo time of ultrasound waves

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.
arduinoObj = 
  arduino with properties:

                  Port: 'COM9'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: [0]
             Libraries: {'Ultrasonic'}
Show all properties

Create an ultrasonic sensor connection object with the trigger pin and echo pin set to D2.

ultrasonicObj = ultrasonic(arduinoObj,'D2')
ultrasonicObj = 
  Ultrasonic with properties:

    TriggerPin: 'D2'
       EchoPin: 'D2'

Create an arduino object with the Ultrasonic library.

arduinoObj = arduino('COM9','Uno','Libraries','Ultrasonic')
arduinoObj = 
  arduino with properties:

                  Port: 'COM9'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: 0
    AvailableSPIBusIDs: 1
             Libraries: {'Ultrasonic'}

Create an ultrasonic sensor connection object with the trigger pin set to D2 and the echo pin set to D3.

ultrasonicObj = ultrasonic(arduinoObj,'D2','D3')
ultrasonicObj = 
  Ultrasonic with properties:

    TriggerPin: 'D2'
       EchoPin: 'D3'

Create an arduino object with the Ultrasonic library.

arduinoObj = arduino('COM9','Uno',"Libraries","Ultrasonic")
arduinoObj = 
  arduino with properties:

                  Port: 'COM9'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: [0]
             Libraries: {'Ultrasonic'}
Show all properties

Create an ultrasonic sensor connection object with the trigger pin set to D2 and echo pin set to D3 and the OutputFormat set to 'double'

ultrasonicObj = ultrasonic(arduinoObj,'D2','D3','OutputFormat','double')
ultrasonicObj = 
  Ultrasonic with properties:

    TriggerPin: 'D2'
       EchoPin: 'D3'

Version History

Introduced in R2019a