Read value of ultrasonic distance sensor.

Namespace:  AForge.Robotics.Lego
Assembly:  AForge.Robotics.Lego (in AForge.Robotics.Lego.dll) Version: (


public bool GetUltrasonicSensorsValue(
	NXTBrick..::.Sensor sensor,
	out int value


Type: AForge.Robotics.Lego..::.NXTBrick..::.Sensor
Sensor to read value from.
Type: System..::.Int32 %
Distance value obtained from ultrasonic sensor, [0..255] cm.

Return Value

Returns true if command was executed successfully or false otherwise.


The method retrieves value of ultrasonic distance sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite(NXTBrick..::.Sensor, array<Byte>[]()[], Int32) method. Then it waits until there is something available to read using LsGetStatus(NXTBrick..::.Sensor, Int32%) method. Finally it reads sensor's value using LsRead(NXTBrick..::.Sensor, array<Byte>[]()[], Int32%) device. See this page for details.

Note:Before using this method it is required to use SetSensorMode(NXTBrick..::.Sensor, NXTBrick..::.SensorType, NXTBrick..::.SensorMode, Boolean) method to set sensor's type to Lowspeed9V mode. It should be done once after NXT brick is powered on. If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

See Also