Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sensor types and modes

SetInputMode configures an input port with a type (which electrical interface and which sensor) and a mode (how the firmware converts the reading into the scaled value). The high-level sensor types pick these for you. With RawSensor and the low-level API you choose them yourself.

Types

SensorTypeCodeSensorHigh-level type
NoSensor0x00Nothing—
Switch0x01Touch sensor (NXT or RCX)Touch
Temperature0x02RCX temperatureRawSensor
Reflection0x03RCX lightRawSensor
Angle0x04RCX rotationRawSensor
LightActive0x05NXT light, floodlight onLight<_, _, Reflected>
LightInactive0x06NXT light, floodlight offLight<_, _, Ambient>
SoundDb0x07NXT sound, dBSound<_, _, Db>
SoundDba0x08NXT sound, dBASound<_, _, Dba>
Custom0x09Custom analogRawSensor
LowSpeed0x0AI²C, no 9 VI2cBus, I2cDevice
LowSpeed9V0x0BI²C with 9 V supplyUltrasonic, I2cBus
HighSpeed0x0CRS-485 (port 4)—
ColorFull0x0DNXT 2.0 colour, detectionColor<_, _, Detect>
ColorRed0x0EColour sensor, red lampColor<_, _, Lamp<Red>>
ColorGreen0x0FColour sensor, green lampColor<_, _, Lamp<Green>>
ColorBlue0x10Colour sensor, blue lampColor<_, _, Lamp<Blue>>
ColorNone0x11Colour sensor, lamp offColor<_, _, Ambient>

Types 0x0C and up need NXT 2.0 firmware (1.28 or newer).

Modes

SensorModeCodescaled value
Raw0x00Raw A/D value, 0–1023
Boolean0x200 or 1
TransitionCount0x40Number of 0↔1 transitions
PeriodCount0x60Number of full 0→1→0 periods
PercentFullScale0x800–100
Celsius0xA0Tenths of a degree Celsius (RCX temperature)
Fahrenheit0xC0Tenths of a degree Fahrenheit
AngleSteps0xE0RCX rotation steps (16 per turn)

The low five bits of the mode byte are the slope used for boolean conversion (0 uses fixed thresholds):

use nxt_rs::protocol::{SensorMode, Slope};

let mode = SensorMode::Boolean.with_slope(Slope::new(10).unwrap());
assert_eq!(mode.to_wire(), 0x2A);

Colour numbers

In ColorFull mode, scaled holds the detected colour:

ValueColorName
1Black
2Blue
3Green
4Yellow
5Red
6White

Ultrasonic sensor registers

The LEGO ultrasonic sensor is an I²C device at address 0x02:

RegisterContent
0x00Version (8 bytes, e.g. V1.0)
0x08Product ID (LEGO)
0x10Sensor type (Sonar)
0x41Command: 0 off, 1 single shot, 2 continuous, 3 event capture, 4 warm reset
0x42–0x49Measurements 0–7 in cm (255 = no echo)