Close This Window

Please download official ILL logos here


For using on the web or on a screenFor printing in high resolutionWhite version, for dark backgrounds

Download PNG

Download AI

Download white PNG

Download JPG


Download white AI

LEGO (c) models of neutron scattering instruments

The Computing for Science (CS) group supports ILL scientists, students and visitors in a number of activities including data analysis, instrument simulation and sample simulation.

Back to ILL Homepage
English French Deutsch 

LEGO (c) models of neutron scattering instruments

How to build LEGO models of neutron scattering instruments, and control them from McStas

by E. Farhi and J-P Panaget ILL (2010)

In this document, we demonstrate how to build and control LEGO models of neutron scattering instruments. A triple-axis, such as IN8, and a crystal time-of-flight spectrometer such a IN6 are considered. These may be used for training, exhibition (mock-up) purposes for instance on conferences and in universities.

The models use the LEGO Mindstorm NXT module, as well as some LEGO Technic parts.

We detail below the parts to order, how to assemble them, and how to control them using McStas.

A demo movie is available below [AVI 1 min 53 sec, by E. Farhi]

LEGO parts required

LEGO parts and models assembly

To order the parts, phone LEGO shopping centre and give them the list of parts. Multiple orders may be needed.

The cost is about 350 € for the two models (TAS+TOF), including the NXT controller.

Once the parts are in your hands, follow the instruction manuals to assemble the models:

Limitations of the models (building)

The Lego Designer software constraints have prevented the alignment of gears 1 and 2 on the figure below. These gears are part of the module supporting the monochromator. The alignment problem has forced us to raise the last "stage". It is therefore necessary, during the final assembly of the bricks, to lower them down as shown in the figure of the real model aside.

Available pre-assembled models for LDD (optional)

LEGO software to generate assembly models and parts list

These steps are optional, as we already provide the parts lists, and the instruction manuals for building the models (see above).

All parts can be ordered separately from but also within the Lego Digital Designer software (LDD):

  • LDD for Windows
  • LDD for Mac OSX 
  • LDD is known to work under Linux with Wine.
  • LDView for Linux is a nice viewer for LEGO models. It requires to extract as well the LDRaw (get the package) so that the LEGO parts library is available (directories 'models', 'p' and 'parts'). If installed in /usr/local/share, make it readable by all users. Then indicate to LDView this location.
  • LeoCAD is also a good choice. Compiles for Linux, available for Windows.
  • LDCad is also a good choice for Linux.

You may as well use the simpler MLCad

We provide pre-assembled LEGO Digital Designer or LDraw/LDView model files as follows:

Generating the model assembly manual (LXF and LXD files)

Open a model, as given above, and switch to the Building guide mode by clicking the 3rd icon in the upper right corner. The software will then generate the about 110-130 pages building manual.

To generate the LEGO parts list, as given above, you just export the model into the LDraw format. Then open MLCad with this LDraw file, and select Extras>Reports>Parts. Check ignore colors option and save the list.

Gallery (assembled TAS model)

Controlling the NXT from a computer

This procedure is based upon a LEGO web help page on Ubuntu-fr

We detail how to install the NBC compiler which assembles nxc (Not Exactly C) commands for the NXT controller.

Download for Ubuntu (tested for 12.04)

Install the packages in /usr/bin or /usr/local/bin

$ cd Downloads

$ tar zxf nbc-1.2.1.r3.tgz

$ tar zxf t2n-0.2.tgz

$ sudo apt-get install fpc libusb-dev

$ cd nbc-1.2.1.r3.src

$ make

$ sudo cp NXT/nbc /usr/local/bin

$ cd ..

$ sudo cp t2n-0.2/t2n /usr/local/bin

Compile the TAS and TOF controller programmes for the NXT (nxc)

Get the NXC source files with nbc

  • nxTAS.nxc can control 3 motors and encoders to be used in the TAS model
  • nxTOF.nxc can control 3 motors and encoders to be used in the TOF model. One motor is used for the Fermi chopper rotation.


and compile them with e.g.:

$ nbc -EF nxTAS.nxc -O=nxTAS.rxe

 the RXE files are ready to be sent to the NXT controller. [ nxTAS.rxe nxTOF.rxe ]

Establish communication between the PC and the NXT controller (t2n)

Connect the NXT controller with the PC using a USB cable.

Switch on the NXT controller.

Detect the NXT controller from the PC

$ lsusb | grep Lego

Bus 004 Device 007: ID 0694:0002 Lego Group

$ t2n -i


Should return the following output:

#### NXT INFOS ###############

protocol version=1.124 firmware version=1.1


Blutooth address: XX:XX:XX:XX:XX:XX

Blutooth signal: 0

Free user flash: 57116

In case you get a message 'usbnxt: usbmisc: fails to set config', run it as super-user: sudo t2n -i

In case an error is returned by t2n, copy the t2n-0.2/udev/70-lego.rules into /etc/udev/rules.d and unplug/replug the NXT USB cable, or restart the PC.

$ sudo cp t2n-0.2/udev/70-lego.rules /etc/udev/rules.d

$ sudo udevadm trigger


 Send the compiled programme to the NXT with t2n

$ t2n -put nxTAS.rxe

Once done, a sharp beep is emitted by the NXT. It is possible to compile and send the NXC file directly to the NXT using

$ nbc -EF -d nxTAS.nxc

which does not require t2n. However, t2n is required when the McStas model has to communicate with the NXT controller.

Launching the programme on the NXT

Once the NXT is switched ON, navigate in the My Files>Software Files and select e.g. the nxTAS programme. The program will first request to align the motors on the reference line (all angles at 0). The NXT is then ready to recieve data from the McStas model. In practice, the McStas instruments (TAS and TOF) write a text file providing desired angle values, which is read continuously by the nxTAS. The motors are then positioned accordingly.

Using the NXT from McStas

We now explain how to control the NXT models from a McStas simulation. This way, every new instrument simulation requests a move on the NXT model, with appropriate angle values. Both TAS and TOF instrument controllers are available.


For this, you need McStas to be installed in a Linux Ubuntu machine.

In principle, the nxTAS.nxc and nxTOF.nxc NXC programmes have been compiled in the previous section above, and sent to the NXT, e.g.

  • $ nbc -EF -d nxTAS.nxc

the t2n programme should also be installed in the Linux Ubuntu machine (see above).

Connections to the model (cables)

  • plug-in the USB cable from the NXT to the PC
  • plug-in port A to the monochromator (motor 1)
  • plug-in port B to the sample table (motor2, TAS model) or the Fermi chopper (TOF model)
  • plug-in port C to the analyzer (motor 3, TAS) or the sample table (motor 3, TOF).

Assemble and run the McStas model on the PC

  • Get the nxtas.instr, NXTAS.comp, nxtof.instr and NXTOF.comp files.
  • Start McStas graphical interface McGui (click the McStas icon)
  • Open the nxtas.instr or nxtof.instr instrument description.
  • Launch the simulation by clicking the Run button.

Each McStas instrument execution triggers the instrument simulation to write a temporary file which holds the motor settings:

nxtas and nxtof temporary files sent to the NXT
#TAS Parameters for nxTAS v1: file nxtas.txt
#generated by McStas on 12/12/2011
#TOF Parameters for nxTOF v1: file nxtof.txt
#generated by McStas on 12/12/2011
Fermi speed=xx

the files are sent to the NXT using the t2n -put <file></file> command.

When the McStas simulation is executed, the NXT checks if the new configuration differs from the current one, and then moves the motors. You can change the A1-A6 angles to move the LEGO model, including with parameter scans.

Behaviour and Limitations of the models

When started the NXT program ma request a coder/positionner alignment (nxTAS model). Press [OK orange central button] when done.

Then, the Lego model, controlled by the NXC programme loaded in the NXT, continuously monitors new information files sent from t2n (see TXT file formats above). When a change is found in angle, the motors are driven to their new positions.

Motors are calibrated and moved to the actual positions, from motor 1 to motor 3.Once the whole move is achieved, a sound is heard.

The geometry of the Lego model minimizes torsion between the elements, but as moves are of large amplitude, it is needed to fix the motor 1 frame onto a solid ground. We have ourselves used an old computer metal box in which 4 holes have been drilled where Lego knots have been inserted. The solid ground should not be rough so that moves are smooth and possibly lower the friction.

As there are only 3 motors per NXT controller, the gear system has a ratio 2, so that angles (A1,A2), (A3,A4), and (A5,A6) are bound.

Before use, it is needed to check the gears used in the model, and especially the one of the motor 1 (monochromator axis) which sustains most of the effort.

Solid ground base (cm)
W x D = 45 x 45, metal surface, low friction
Lego model dimensions (cm)
W x D x H = 22.60 x 33.49 x 17.29
Response time (s)
Positioning time (s)
Positioning accuracy (deg)

We alse recommend to set the model on a flat surface (we have used a metal plate on which the monochromator block is fixed), as well as glue Teflon tape under the modules to limit movement resistance.

The assembled LEGO model and McStas running on a laptop. The model has been positioned from the TAS simulation.

Going further

You may position small mirrors on the monochromator/analyser and sample positions, and use a tiny laser pointer to figure out the beam axis. Reflections on the mirrors will then indicate the diffraction (elastic) scattering configuration. A light-scattering aerogel/plastic block can be put in place of the sample for a bright light source.

It is absolutely necessary to install Lego Viking staff around the models ! (McStas originates from Denmark).

We here demonstrate the operation of the LEGO/NXT TAS model, controlled from a McStas simulation [AVI/MPEG4 1 min 53 sec].