Lego Mindstorms is a family of robotics kits from the company famous for those open-ended brick-building kits. Each Mindstorms kit includes a few motors, an assortment of sensors, plus a collection of more-regular Lego parts (from the Technics range), together with a fully-programmable microcontroller. The company includes, in the box, proprietary GUI-based programming environments for Macintosh and Microsoft Windows machines, but it has also been cooperative with the development by others of open-source programming tools.

The latest product in this range is the Mindstorms NXT 2.0 kit, available for about $500. This includes three servomotors, two touch sensors, a colour sensor (with built-in switchable light source), and an ultrasonic sensor which can be used for active ranging or passive sound detection. The microcontroller now has the option of hooking to a PC via either USB or Bluetooth.


For Linux users, the best option for programming these seems to be a pair of languages called NBC (a low-level assembler) and NXC (a higher-level C-like language). NXC compiles to the NBC bytecodes, which in turn are interpreted by firmware on-board the NXT controller. These languages are part of the open-source Bricx Command Center project, the main part of which is Windows-only. However, the nbc command-line tool can be built for Linux, and this implements both languages as well as functions for uploading compiled programs into a connected NXT microcontroller.

A useful library is NXT_Python. This provides functions that can be used to directly examine and manipulate the state of the NXT controller from the host PC. Notwithstanding its requirements specifically mentioning Python 2.4, it does seem to work fine with Python 2.5.

Another compiler is NXTGCC. Unlikes NBC/NXC, this compiles to actual machine language for the ARM CPU. It can be used to write completely new firmware for the NXT controller.

Device Permissions Setup

You don’t need any special drivers to connect the NXT controller to a Linux box (at least via USB; I haven’t tried Bluetooth); all communication is done through the userland libusb library. However, by default, udev will create the device entry as only accessible by root. To fix this, create a new group in your /etc/group file called, say legonxt. Add your own nonprivileged username to this group (you will need to logout and login again to become a member of this group). Then, create a new file in your /etc/udev/rules.d (for example, call it 70-lego.rules) containing the following line:

    SUBSYSTEM=="usb", ATTR{idVendor}=="0694", GROUP="legonxt", MODE="0660"

This gives read/write access to the device to all members of the legonxt group.

You may find versions of the above udev rule elsewhere on the Web which also create a symlink for the device with a name like /dev/legonxt-xxx. As far as I can tell, this is unnecessary. libusb certainly doesn’t need it.

NBC/NXC Building Notes

Ignore the source release tarballs linked from the NBC Beta Releases page above; they are all out of date. Instead, get the latest source out of SubVersion (note you will need Free Pascal to compile it):

    mkdir nbc nbc/beta
    svn co bricxcc/
      # above may take a few minutes
    make -C bricxcc/ -f bricxcc.mak archivenbcunix
      # above puts Unix/Linux-compilable source tarball in nbc/beta
    cd nbc
    tar xzvf beta/nbc-1.0.1.b36.unix-src.tgz
    make nbc

The instructions say to make install at this point, but as of this writing that will only install the ManPage into /usr/share/man/man1. To install the executable, do something like

    install -m 755 NXT/nbc /usr/local/bin/

See Also