LEGO NXT sensors

With sensors, the robot can detect its environment. There are many different sensors available for LEGO NXT robots such as the ultrasonic sensor that is used in the example program below. The way how sensors are used in Pocket Code applies to all sensors. They differ only in the way they communicate with Pocket Code. The touch sensor can communicate to Pocket Code only two values 0 and 1 respectively touched or not touched. The ultrasonic sensor tells Pocket Code its distance to an obstacle by using the values 0 to 255. Around 0 means the obstacle is directly in front of the sensor. Instead 255 means that there is no obstacle in the range of the sensor.

This table shows the data range of the NXT sensors. You can use these values within an if brick to control the behaviour of the robot depending on its environmental.

NXT sensor Data Range Description
touch sensor 0 or 1 0 ... not touched ; 1 ... touched
ultrasonic sensor 0 to 255 0 ... obstacle directly in front ; 255 ... obstacle out of range
sound sensor 0 to 100 0 ... silence ; 100 ... maximum environmental noise
light sensor 0 to 100
(not precise)
0 ... dark ; 100 ... light
Hint for c
olor differentiation: Test the light sensor values of your color by using the brick combination below. Then use the values in your if bricks.


Show sensor value on stage:
To display the values of a sensor on the stage for debugging etc., use this brick combination. Choose the right sensor slot, depending on what you have plugged into your robot. 


To use the following example program, plug the engines as follows: 

A: Engine for the arm of the robot / B: Engine for the right wheel. / C: Engine for the left wheel.
An assignment of the sensors to the slots on the robot is displayed when opening the program.



1) As a starting point, this program is used. In the additional object "obstacle detection" is checked if there is an obstacle in front of the sensor.

2) This image shows the needed bricks for the "obstacle detection" object. Within a loop, the ultrasonic sensor value is permanently checked. If the value respectively the distance to an obstacle is too small, both motors are stopped. In addition, the color of the object changes on stage to red. Once the obstacle has been removed, the robot moves further in the previous direction.

3) Everywhere you can enter numbers, you can also use the values of the sensors. Tap simply in the formula editor under sensors to the required sensor value to us the sensor value in your program.