Advanced Motors, Sensors and Third Party Hardware

Advanced Motors, Sensors and Third Party Hardware

Motor classes


All leJOS NXJ Motor classes extend the abstract BasicMotor class. BasicMotor defines the methods that any type of leJOS NXJ motor must support and provides implementations for some of them. The methods are:

  • public void setPower(int power)
  • public int getPower()
  • public void forward()
  • public boolean isForward()
  • public void backward()
  • public boolean isBackward()
  • public void reverseDirection()
  • public boolean isMoving()
  • public void flt()
  • public boolean isFloating()
  • public void stop()
  • public boolean isStopped()
  • public int getMode()

The RCX motors do not have an in-built tachometer and so cannot support the advanced functions of the NXT motors such as the rotate and rotateTo methods and the speed regulation.

A simpler class is used to support the RCX motors. It has similar methods to the Motor class in the RCX version of leJOS (which are the same as those supported by the BasicMotor class).

To use an RCX motor you create an instance of the RCXMotor class using the constructor:

  • public RCXMotor(BasicMotorPort port)


RCXMotor rcxMotor = new RCXMotor(MotorPort.A);				

You can use RCX Motors with leJOS NXJ by connecting them with the conversion cables that can be purchased from LEGO (and are bundled with the LEGO MINDSTORMS NXT educational kits). You can also use the RCXMotor class to control RCX motors connected to a remote RCX - see the "Communications" tutorial.


This class controls the NXT motors which have a built in tachometer, and is described in Controlling the Motors

MotorPort classes

To use the NXT motors it is not necessary to explicitly use the MotorPort class: you can just use the variables Motor.A, Motor.B and Motor.C. So if you are only using NXT motors, you can skip this section.

However it is useful to understand how motor ports work as they are used by:

  • The Motor class

  • The RCXMotor class

  • The RemoteNXT class

  • The RCXMotorMultiplexer class

  • Remote RCX motors accessed via the RCXLink class

There is a hierarchy of interfaces defined for motor ports:

  • BasicMotorPort

  • Tachometer

  • TachoMotorPort

All motor ports support the BasicMotorPort interface which allows control of a motors power and mode (forward, backward, stop, float).

Ports that supports this include:

  • NXT ports connected to NXT motors

  • NXT ports connected via the RCX conversion cable to RCX motors

  • Ports on the RCXMotorMultiplexer adapter

  • Ports on remote NXTs accessed via the RemoteNXT class

  • Ports on remote RCXs accessed via the RCXLink class

The implementations of BasicMotorPort include:

  • MotorPort

  • RemoteMotorPort

  • RCXPlexedMotorPort

  • RCXRemoteMotorPort

The tachometers that are built in to the NXT motors support the Tachometer interface.

NXT motor ports support the TachoMotorPort interface which includes the BasicMotorPort and Tachometer interfaces.

Implementations of TachoMotorPort include:

  • MotorPort

  • RemoteMotorPort

All this sounds rather complicated, but is simple to use:

  • For NXT motors, you use Motor.A, Motor.B and Motor.C

  • For RCX motors connected by the conversion cable you use RCXMotor(MotorPort.A), RCXMotor(MotorPort.B) or RCXMotor(MotorPort.C)

  • For NXT motors on a remote NXT, you use remoteNXT.Motor.A, remoteNXT.Motor.B or remoteNXT.Motor.C where remoteNXT is an instance of RemoteNXT.

  • For RCX motors connected by the RCX Motor Multiplexer, you use rcxMotorMultiplexer.A, rcxMotorMultiplexer.B, rcxMotorMultiPlexer.C or rcxMotorMultiplexer.D, where rcxMotorMultiplexer is an instance of RCXMotorMultiPlexer.

  • For RCX motors connected t remote RCXs via the RCXLink class you use rcxLink.A, rcxLink.B or rcxLink.C where rcxLink is an instance of the RCXLink class.

Back to top

Sensor Ports

If you are using a sensor connected directly to a NXT sensor port, you can use the SensorPort class and you can probably skip this section.

But if you are using a port splitter, or a remote NXT or RCX, it may be of interest.

The NXT sensor ports support three different types of sensor:

  • NXT Analog/Digital Sensors

  • I2C Sensors

  • Legacy RCX sensors

Corresponding to each of the different types of sensor, there is a corresponding interface:

  • ADSensorPort which extends BasicSensorPort

  • I2CPort extends BasicSensorPort

  • LegacySensorPort extends ADSensorPort

At the top of the interface hierarchy is the BasicSensorPort. All sensor port classes implement this interface. This interface allows the type and mode of a sensor to be set. These type and mode constants are defined by the interface SensorConstants, which is inherited by the BasicSensorPort interface.

The types of sensors are:

Sensor Type


























and the modes are:



















The BasicSensorPort interface defines the methods:

  • public int getMode();

  • public int getType();

  • public void setMode(int mode);

  • public void setType(int type);

  • public void setTypeAndMode(int type, int mode);

Most of the time, with leJOS NXJ, these types and modes do not need to be set explicitly as it is done by the constructor for the sensor class being used, e.g. TouchSensor, LightSensor and UltrasonicSensor.

The implementation of the NXT sensor port – SensorPort – supports all these interfaces. The reason for separating out the different interfaces is that there are other implementations of sensor ports that only support a subset of these interfaces, and different types of sensors only require particular interfaces to be implemented:

  • I2C Sensors just require I2CPort

  • NXT Analog/Digital sensors just require ADSensorPort

  • RCX sensors such as the RCX Light sensor require LegacySensorPort

Port splitters like the Mindsensors Split-Nx only support I2C sensors and thus, effectively, only support the I2CPort interface.

There are other implementations that only support the other interfaces. For example the current implementation of remote sensor ports – RemoteSensorPort – currently only supports the ADSensorPort interface.

The classes for RCX Sensors multiplexers – such as the forthcoming Mindsensors version – will only support the LegacySensorPort interface.

Back to top


Each sensor supported by leJOS NXJ has a specific class that is used to access the sensor. Each of these sensor classes has, as a parameter, a sensor port that supports the required interface. Any sensor port class that implements the interface can be specified as the parameter. As the SensorPort class supports all the interfaces, if the sensor being accessed is directly connected to the NXT, the parameter should be one of SensorPort.S1, SensorPort.S2, SensorPort.S3 or SensorPort.S4.

If a port splitter is used the parameter again should be one of SensorPort.S1, SensorPort.S2, SensorPort.S3 or SensorPort.S4. This specifies the port that the splitter is connected to. If multiple sensors are connected to the splitter they must each have different I2C addresses. Most I2C sensors can have their address changed – see the manufacturers instructions. To specify the address that a sensor uses, if it is not the default, the setAddress method of I2CSensor should be used.

The sensor ports supported by leJOS NXJ together with the class that supports them and the type of sensor port they require is given in the following table:

RCX Sensors

RCX sensors, other than the touch sensor, are active sensors that have voltage applied for all but the short period every three milliseconds when the measurement is taken.

RCX Light Sensor

The RCX light sensor is supported by the RCXLightSensor class.

The constructor is:

  • public RCXLightSensor(LegacySensorPort port)

For example:

RCXLightSensor light = new RCXLightSensor(SensorPort.S1);

The RCX light sensor is automatically activated, so current is applied to it and the LED comes on. It can be passivated and activated explicitly.

The methods are:

  • public int readValue()

  • public void activate()

  • public void passivate()

RCX Touch Sensor

As the RCX touch sensor is a passive sensor similar to the NXT version, it is supported by the standard TouchSensor class.

RCX Rotation Sensor

The RCX rotation sensor is not currently supported by leJOS NXJ.

RCX Temperature Sensor

The constructor is:

  • public RCXTemperatureSensor(LegacySensorPort port)

  • The methods are:

  • int readValue()

    returns raw value

  • float getCelcius()

    return the temperature in degrees Celcius

  • float getFarenheit()

    return the temperature in degrees Farenheit

Third party sensors and other devices

leJOS NXJ supports many third party sensors. The two main vendors of third party sensors are Mindsensors and HiTechnic.

Most of the third party sensors and I2C sensors and extend the I2CSensor class but there are also Analog/Digital sensors such as the HiTechnic Gyro sensor and the IR Seeker.

There are also other I2C devices supplied by the third parties, that are not sensors, but are multiplexers or adapters.

The RCX Motor Multiplexer from Mindsensors is an example of a multiplexer. It allows up to 4 RCX motors to be connected to a NXT sensor port and to be independently controlled.

The Mindstorms NRLink-Nx infra-red communications adapter is an example of an adapter. It allows two-way communication between the NXT and RCXs. It also allows control of Power Function motors.


The I2CSensor class implements the basic methods for accessing I2C sensors including getData and SendData.

It also includes methods that are implemented by all the I2C sensors, including getVersion, getProductID and getSensorType.

The method signatures are:

  • public int getData(int register, byte [] buf, int len)

  • public int sendData(int register, byte [] buf, int len)

  • public int sendData(int register, byte value)

  • public String getVersion()

  • public String getProductID()

  • public String getSensorType()

leJOS NXJ uses 7-bit I2C addresses, whereas I2C device specification often give them as 8-bit addresses with the least significant bit set to zero. Most I2C sensors use a default address of 0x01, which such specifications give as 0x02. Most I2C devices allow the address to be changed.

The setAddress method can be used to set the address used to talk to the I2C device – it defaults to 0x01.

Individual I2C devices have registers that can be read and written and registers that can be used to execute commands. Each I2C device has a class that extends I2CSensor and has methods to access the registers and execute the commands specific to that sensor.

The I2CSensor class can be used to implement an I2C device explorer that reports what devices are connected to which sensor sing which address – see the I2CDevices sample. This is possible as all the NXT I2C sensors and other devices support the getVersion, getProductID and getSensorType methods.

See the table above for the complete list of sensors and other third party devices.

Back to top