Category Listing For
NXT
Arduino + NXTMMX + LEGO Motors
Updated 2010-11-09. It is not much easier to install the library.
When I first learned that Mindsensors.com (who make several really neat sensors for use with the NXT) was making a motor multiplexer, I contacted them to ask if it would work with the Arduino (yes!) … and then I asked if I could buy a prototype and was thrilled when they sent me one. (Thanks!)
This video shows how well it works.
Oh man. I love the NXT, but I really like the possibilities that this affords. The NXTMMX allows you to connect two LEGO motors to a device that issues I2C commands, and gives you one more port to plug in another NXT device. There is a good tech spec documenting the commands, so there is no reason it wouldn’t work with your microcontroller of choice, by merely connecting four wires (with a cable like this, or something from here) and supplying power with a battery pack.
Better than that, though, is that there are other I2C sensors for the NXT which will work. When I run the I2CScanner example sketch, I am able to see a HiTechnic Compass Sensor and a Mindsensor’s Line Leader. Most of my other sensors do not use the I2C bus to communicate and would need to be connected differently to use with an Arduino. [For anyone who wants to try, allow me to recommend Extreme NXT: Extending the LEGO MINDSTORMS NXT to the Next Level, or download the NXT Hardware Development Kit — LEGO was kind and wise enough to offer schematics on how the parts works.] One I2C sensor that did not work was LEGO’s Ultrasonic sensor. I suspect that if I provided a 9V power signal to it, it would work.
I have started an open-source project called NXT I2C Devices for Arduino. The project contains my initial code to run the NXTMMX (ported from the Not eXactly C API for the device), and, Mindsensors has been contributing code so you can use it with other sensors that they sell!
Update: To use it, create a directory called libraries within your Arduino sketchbook directory. Then, download and extract the latest version of NXTI2CDevice into that folder. (You should end up with a /libraries/NXTI2Device folder within your sketchbook.) Quit and restart the Arduino IDE, and you are ready to go!
If you’d like to aid in development, you’ll need to install the Bazaar version control system to get the latest code. At a command prompt, change to the Arduino library directory (on my Mac it is /Applications/Arduino.app/Contents/Resources/Java/libraries/), and issue this command:
bzr branch lp:nxti2cdevice NXTI2CDevice
If you’d prefer to use a graphical tool, take a look at the first few steps of my previous post explaining the use of Bazaar Explorer with Enchanting. Just make a branch of lp:nxti2cdevice, and then copy/move/symlink the folder into your Arduino libraries folder.
Oh, and if you are interested in helping out on the project, drop me a line.
Happy Hacking!
Status of Exporting Code from Enchanting
In the far-too-brief periods of time I’ve been able to put into Enchanting of late, I’ve been working on making it so that you can export code. I wanted to put up some incomplete results of my work.
If I construct the following script:

middle click on the sprite that owns the script and choose “export code”:

a window pops up with the text converted into a Java-esque format:
public void WhenGreenFlagClicked()
{
// GoTo(-100, 0);
// ClearDisplay();
// PenDown();
// PlaySound();
// PlayDrum(48, 0.2);
// WaitSecs(RandomRange(1, 10) / 5);
// WaitSecs(Math.pow(Math.E, 10));
}
public void WhenGreenFlagClicked()
{
LightSensor.SetMaxPoint(LightSensor.GetRawReading());
LightSensor.SetMinPoint(LightSensor.GetRawReading());
LeftMotor.runUnlimited();
Right Motor.runUnlimited();
for (int i = 0; i < n; ++i)
{
if (LightSensor.GetRawReading() > LightSensor.GetMaxPoint())
{
LightSensor.SetMaxPoint(LightSensor.GetRawReading());
}
if (LightSensor.GetRawReading() < LightSensor.GetMinPoint())
{
LightSensor.SetMinPoint(LightSensor.GetRawReading());
}
}
while(true)
{
LeftMotor.setPower(LightSensor.GetBrightness());
Right Motor.setPower();
}
}
There are still flaws and shortcomings to overcome, and it isn’t real code yet, but I’m pleased with the progress.
If you want to try it out (without getting the latest from version control), download this snapshot of the program, drag and drop Enchanting.image onto your Enchanting.exe or Enchanting.app, and have fun.
The files of note are:
- the Enchanting.image file, which has all the code changes
- locale/lejos.exp — a translation file used to translate from Scratch to the java-esque output (go ahead and play with it; it is reloaded every time you click export)
- the ReferenceImages folder, which has pictures of the different code blocks with their block specification name in English. (This is handy for translating, and, if you hold down Shift and click on the file menu, there is an option to generate these files.)
An Enchanting Idea
Update: Please see the vision on the Enchanting website.
In my previous post, where I discussed creating custom code-tiles in a variant of Scratch and using them to control devices external to the computer — namely an Arduino and an NXT robot — I finished off by saying that I would discuss where I want to go to from here.
An Enchanted Vision
I want to make a graphical code-creation tool to make it easier for kids to program robots—so that they can cast a magic spell, as it were, that makes a robot sing and dance and do as they’ve instructed.
My current plan is to build off of Scratch, which my kids enjoy working with. (The same can not be said for the NXT-G programming environment which comes with the LEGO Mindstorms NXT kits).
I envision two modes of operation:
1. Interactive Mode
In interactive mode, your robot is instructed step-by-step on what to do by the computer, and can interact with other sprites and such in Scratch.
You could, for example:
- have a robot with an ultrasonic sensor on it that spins in place, with a sonar-like display on-screen.
- adjust parameters (like speed, or degrees to turn) with sliders on screen as a program is running.
- use the NXT as an input device; for example, one could build a crank to spin a motor, and on the computer, read the motor’s encoder position to, say, reel in a fish on screen
- have a robot (or robots) be in a play with virtual actors who appear on screen.
- see what your sensors are seeing, so as to better understand and correct a program.
- or just use this method to prototype programs.
2. Autonomous Mode
When you are ready to try a program that the robot runs directly, you export the code (see this video tutorial for Scratch For Second Life to get an idea of what I mean), and from there, put it onto the robot.
The reasoning behind this is two-fold:
- Plenty of good programming environments already exist that can be used to program the NXT (and other potentially supported platforms), and there is no sense re-inventing the wheel
- I want to expose kids to the idea of text-based programs, so that they are able to transition to them when the time is right.
Having said that, though, I want the code to look nice so that someone could study and learn from it.
Inclusive Support
My primary focus at the moment is to help kids program their NXTs. I’d like the same environment, though, to be able to target other systems, such as the RCX, the Arduino, and perhaps things like VEX or OLLO.
Next, I’d like the software to be multi-platform and open-source. It should run on Windows, Mac OS X, and Linux.
Lastly, I’d like the software to be accessible to as wide an audience as possible. If I were designing it from square one, I’d want to ensure that you could do everything with the keyboard, everything with the mouse, and that you can make the text really large so that people with poor visual acuity can use it. I also want to ensure that text can be translated into other languages. The software should also allow users to do advanced things but in a comprehensible manner.
Where Am I At?
I’m close to the beginning of this journey. My previous post gives a proof-of-concept peek into what I want to do. I’m reading through Squeak By Example and lurking in the Scratch Advanced Topics Forum to learn how to hack on Scratch. (I should mention that I programmed video-games professionally for three years, but Squeak (the language Scratch is written in) is not widely used and different from C++ and Python).
How Can You Help?
I’m not expecting much by way of coding help (but if anyone is interested, do let me know!), and I’m nowhere near ready for testing help, but I could certainly use some thoughts on design, specifically, on what sort of code tiles would be useful to have and how to organize them.
My current though is that, if you are programming for an NXT, perhaps you should use a special NXT sprite, which supports all options available for use in programming the NXT, and omits those which are not applicable (such as changing costumes, setting the graphic effect, or determining the distance to another sprite). That being the case, though, perhaps it is better to have different categories of tiles altogether, although I’m not really sure what those categories should be. I’d love to hear your ideas on this.
I’m also quite happy to hear suggestions on what language(s?) should be used as an intermediate language for autonomous programs. I’m partial to leJOS NXJ, and certainly there is something to be said for NXC ; I can’t help but think that NXTalk would be ideal, but it is also in an embryonic state and working on Enchanting itself is more important to me. I’d like to balance ease of use, readability of code, and ease of installation, and yes, there are many, many languages to choose from.
What of the Arduino? It might be easier to get it up and working with Scratch—especially as Scratch will already talk to a PicoBoard over a serial port, and the language to output to is obvious and readable. It looks to me like the Modkit project might have things covered, and the NXT is my primary focus at this time.
Feel free to contact me or add a comment below.