Main Content

Ultrasonic Sensor

Measure distance between ultrasonic sensor connected to EV3 brick and nearest object

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

  • Ultrasonic Sensor block

Libraries:
Simulink Support Package for LEGO MINDSTORMS EV3 Hardware

Description

Use the Ultrasonic Sensor block to measure the distance between the ultrasonic sensor connected to the EV3 brick and the nearest object in front of the sensor. The sensor can detect objects from approximately 5 to 255 centimeters away. The block measures approximate distances from the object. For greater precision, calibrate the sensor output values against physical measurements.

The distance from which the sensor first detects an approaching object depends on these factors.

  • The ultrasonic reflectance of the object, which is a function of the object’s size and composition. The sensor detects large hard objects from a greater distance than small soft ones. For example, the sensor might detect a pane of glass at 255 cm and a hand puppet at 150 cm.

  • The angle of incidence of the object relative to the sensor. The sensor detects objects directly in front of it at greater distances than objects off to the sides.

During simulations without hardware, this block emits zeros. See Block Produces Zeros or Does Nothing in Simulation.

Examples

expand all

This example show how to measure distance between the NXT ultrasonic sensor connected to the EV3 brick and the nearest object.

Open the legoev3lib block library and copy the Ultrasonic Sensor block as described in Open Block Library for LEGO MINDSTORMS EV3 Hardware.

Open the legoev3lib block library and copy the Motor block to the same model and connect the blocks.

Connect the ultrasonic sensor to port 1 and a servo motor to port A of the EV3 brick.

Ultrasonic Sensor model

Prepare and run the model on the EV3 brick as described in Run Model on EV3 Hardware. Hold the ultrasonic sensor closer to an object and observe that the speed of the motor decreases.

Ports

Output

expand all

The block outputs the distance between the ultrasonic sensor connected to the EV3 brick and the nearest object in front of the sensor. The block outputs the distance in centimeters.

When the nearest object is beyond the maximum range of the ultrasonic sensor, the block outputs the maximum value, 255. When the object is placed beyond the range of sensor detection, the port outputs 0.

Data Types: uint8

Parameters

expand all

Select the EV3 brick input port to which the ultrasonic sensor is connected. Avoid assigning multiple devices to the same input port.

Specify the frequency of how often the block reads sensor values connected to the EV3 brick. Shorter sample times, such as 0.01, may produce unreliable measurements.

Smaller sample times require the processor to complete the same number of instructions in less time, which can cause task overruns.

Version History

Introduced in R2012a