Main Content

readDistance

Read distance from ultrasonic sensor to object

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

Description

example

distance = readDistance(mysonicsensor) measures the distance from the ultrasonic sensor to an object, and returns the measurement in meters.

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

Input Arguments

collapse all

Connection to ultrasonic sensor, specified as a string that represents the object created using sonicSensor.

Example: mysonicsensor

Data Types: char

Output Arguments

collapse all

Distance to object in meters, returned as a double.

Version History

Introduced in R2015a