Main Content

sonicSensor

Connection to ultrasonic sensor

Add-On Required: This feature requires the MATLAB Support Package for LEGO MINDSTORMS EV3 Hardware add-on.

Description

This object represents a connection to an EV3 Ultrasonic Sensor (item number 45504). To measure the distance from the sensor to an object in meters, use this object with the readDistance function.

Creation

Description

mysonicsensor = sonicSensor(myev3) creates a connection to an ultrasonic sensor. You can use this connection with the readDistance function to measure the distance from an ultrasonic sensor to an object.

If multiple ultrasonic sensors are attached to the EV3 brick, this function chooses the sensor that is attached to the EV3 input port with the lowest number.

example

mysonicsensor = sonicSensor(myev3,inputport) creates a connection to an ultrasonic sensor that uses a specific EV3 input port defined by the InputPort property.

example

Input Arguments

expand all

Connection to EV3 brick, specified as a string that represents the object created using legoev3.

Example: myev3

Data Types: char

Output Arguments

expand all

Connection to ultrasonic sensor, returned as an object handle.

Properties

expand all

This property is read-only.

Number of the EV3 input port that the sensor uses, returned as a double.

Example: 1

Data Types: double

Object Functions

readDistance Read distance from ultrasonic sensor to object

Examples

collapse all

Read the distance, in meters, between an ultrasonic sensor and an object.

Create a connection to the EV3 brick called myev3.

myev3 = legoev3
myev3 = 

  legoev3 with properties:

      FirmwareVersion: 'V1.03E'
           HardwareID: []
            IPAddress: []
    CommunicationType: 'USB'
         BatteryLevel: 100
     ConnectedSensors: {'touch'  'gyro'  'color'  'sonic'}

The sensor appears in the list of connected sensors.

Connect to the ultrasonic sensor.

mysonicsensor = sonicSensor(myev3)
mysonicsensor = 

  sonicSensor with properties:

    InputPort: 4

Read the distance, in meters, from the ultrasonic sensor to the nearest object.

distance = readDistance(mysonicsensor)
distance =

    0.4600