Plüss   Aplulogo      Print Text 
© 2021, V10.4


Eighth example: NXT brick as embedded system (NxtJLibA autonomous mode)

Purpose: Use the NXT brick as a controller in a feedback control system that keeps the temperature of a heater at a constant level.

It is generally admitted that an embedded system is a special-purpose computer system designed to perform a few dedicated functions. It is usually embedded as part of a complete device including hardware and mechanical parts. The heart of an embedded system is often a microcontroller that runs a single program stored in a read-only memory (Eprom, Flash). In contrast to a general-purpose computer, the microcontroller contains digital and analog ports in order to communicate easily with external hardware devices such as relays, switches and sensors.

The NXT brick is a typical embedded system with its three microprocessors: The 32-bit controller Atmel AT91SAM7S256 runs your programs. It includes a flash memory, a RAM and the USB device interface. The 8-bit Atmel ATMEGA48 manages the pulse width modulation (PWM) of the motors and uses the feedback from the NXT motor rotation sensors to tune the power of each motor. Finally the CSR Bluecore 4 microcontroller performs the Bluetooth communication.

Input/Output of the Lego brick is somewhat dedicated to motor and sensor ports. There are no general purpose analog or digital I/O ports, but HiTechnic ( developed a Prototype Kit that connects to the brick like an I2C sensor and expands the I/O capabilities of the NXT by 5 analog input channels (0..3.3V, 10 bit) and 6 digital input/output channels (3.3V). There are two versions available, one with a solder breadboard where additional electronic components (resistors, LEDs, ICs, etc.) may be mounted, and one with a solderless experimental breadboard to add external circuitry for developments and tests.

  HiTechnic   HiTechnic

                Prototype Kit NPT1050
               (with solder breadboard)

                  Prototype Kit NPT1055
           (with solderless breadboard)

High-power outputs (switching line voltage up to several amps) can be obtained by adding a solid-state relay, such as the Crydon ASO242 (up to 2A @ 280V). Care should be taken to enclose all high-voltage parts in a separate housing, e.g. a power socket-outlet (see figures below). Because the Prototype Kit uses 3.3V logic, a driver should be used, such as the ULN2803. The extra 5V-supply of the Prototype Kit can be used to power the ULN2803. With this design all power is delivered by the NXT brick and no external power-supply is needed.

The NXT brick expanded with the HiTechnic Prototype Kit is very convenient to demonstrate many aspects of embedded systems. The following program simply reads the temperature from a Lego temperature sensor and switches a hotplate (heating lamp, soldering-iron, etc.) on and off to keep the temperature at a certain level.

// PrototypeSensor at port S1 (heater at B0)
// Temperature sensor at port S2, treated as LightSensor

import ch.aplu.nxt.*;
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;

class TemperatureSensor extends LightSensor
  public TemperatureSensor(SensorPort port)

public class TempReg implements ButtonListener
  private volatile boolean isRunning = true;
  private final int out = 1;
  private final int sollWert = 600;
  private final int hysteresis = 3;

  public TempReg()
    NxtRobot robot = new NxtRobot();
    System.out.println("ESCAPE to quit");
    PrototypeSensor ps = new PrototypeSensor(SensorPort.S1);
    TemperatureSensor ts =  new TemperatureSensor(SensorPort.S2);
    int[] ioControl = {out, out, out, out, out, out};
    while (isRunning)
      int isWert = ts.getValue();
      System.out.println("v: " + isWert);
      if (isWert > sollWert + hysteresis)
      if (isWert < sollWert - hysteresis)

  private void turnOn(PrototypeSensor ps)
     int[] dout = new int[6];
     dout[0] = 1;

  private void turnOff(PrototypeSensor ps)
     int[] dout = new int[6];
     dout[0] = 0;

  public void buttonPressed(Button b)
    isRunning = false;

  public void buttonReleased(Button b)

  public static void main(String[] args)
    new TempReg();

Discussion: Because there is no class for the temperature sensor in the NxtJLib library, we use the temperature sensor like a light sensor. We derive class TemperatureSensor from LightSensor, so we have all methods of LightSensor available. To avoid the typical fast oscillations of the control system, a hysteresis is used.


Assembly of the driver/solid state relay:

  hrelais2 hrelais3

The solid state relais is fixed on a luster terminal,
below the power outlet

The power outlet is mounted on top


  Final assembly: The ULN2803 driver is mounted on the solder breadboard
(all high voltage parts are securely enclosed)


Schematic diagram:




Distributors (for Switzerland):

HiTechnic NPT1050 (
Aufputz Einfach-Steckdose Typ 12, Aufbauhöhe 54 mm, E-Nummer 608 111 000 (Elektroinstallateur)
ULN2803 (
Crydom ASO242 (