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 Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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')
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 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
    AvailableSPIBusIDs: 1
             Libraries: {'Ultrasonic'}

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'

Introduced in R2019a