Hacking the Lego NXT – Part 1

Before we start with this slightly tangential series of posts, let me give a word of advice to those who saw the word “hacking” in the title and immediately thought, “HE IS GOING TO HACK INTO SECRET FILES!”. Firstly, any democracy should not have secret files, but that’s for another post, and secondly, “hacking” is what I define as a skillset: the ability to alter the main function of something, be it hardware or software, to suit what you want to do with it. Henry Dyer (@Direthoughts) did a nice piece of hacking, for example, on those Apple Store iPads. It has been the media convincing people that hacking is bad, when in fact, only a small proportion hack with malicious intent. Taking open-source code and altering it is software hacking; building a buggy out of sheet aluminium and plastic is a form of hardware hacking, and what I am going to describe here is a mere alternate manifestation of that.

I am usually not a very adventurous person: I liked my electronic products simple and stupid, never opening them up to reveal the wonders inside. That was until I changed schools, where I was introduced into an environment of tweaking with things and where I encountered a whole new range of people. I urged myself to get involved, and having screwed up my first year by accidentally doing the exact opposite, sought to catch-up in my next year, the one I just finished. Just adding, I believe I have accomplished that!
My altered RJ12 socket

Aside from that, I also set myself the task of clearing out the dusty abyss beneath my bed, having accumulated 4 years worth of junk down there. I found a Lego Mindstorms NXT kit, the robotics kit Lego introduced a few years ago. It was, unfortunately, missing some main pieces, including the processing unit. A new kit cost £200 and the processor itself £99, so there was no way I was going to be able to use that thing again! I still had one out of the three stepper motors and a few sensors. With no clue what to do with these, I offered them to my technology teacher, who kindly denied, as he had no use for them, but recommended I try and hack them for use with an Arduino or PICAXE as a summer project. Maybe then the school could use them.

So, lets start on this project. I quickly realised that the annoying NXT cables will not fit into a standard RJ12 socket (telephone cable). Having purchased one and made it breadboard compatible, looked at this guide and decided that my 3 remaining cables, although replaceable, were far too precious to be mutilated like that. So I opted for a more interesting way: cut it!

Yes, you heard me. Instead of using two 30cm cables to make two modified ones, I used one cable and cut it in half, giving me two 15cm modified cables to use with the sensors and motor. I needed to create cables that were breadboard compatible so I could test them with my Arduino. A friend of mine and I worked out the best way to execute this, which I shall describe below.

The Cable

I assume here that you have basic soldering skills, a soldering iron and wire strippers, as every good technician or keen engineer should have.
Firstly, take the NXT cable, attainable at ebay or Lego.com, and cut it in to two equal halves. Then strip the insulating black plastic to reveal the six coloured wires. Strip these as well to expose the copper.

The cut NXT cable
Then, you will need to buy either some single-core wire or one of these jumper cables. This has to be stripped and soldiered to the exposed end of your modified NXT

The jumper cable
Half a jumper cable soldered to NXT cable

Repeat this process for all 6 wires. Realistically, there should only be the need for 3 jumper cables, as you are cutting them in half; however, if you want to, you could use a corresponding colour cable to make things easier when hooking these up to a breadboard.

All wires soldered
To add a professional feel to it, avoid interference and to make your cable sturdier, I would reconnect heat shrinking the connection, in such a way that the colours from the original NXT cable can still be seen.
Modified cable with heat shrink

A quick test with a multi-meter later, we were all set!


The Motors

I started testing this, thinking it would be somewhat simpler to work out than the sensors, being an output, not an input. This site provided me with the information as to what signal would have to be sent through each pin. I wrote up an initial Arduino sketch to send high and low signals to pins 1 and 2 respectively, hooking the pins directly into the Arduino without a protective diode or Full-H motor driver IC (something I would not recommend!), putting my beloved Arduino at great risk, as I did not have the necessary equipment, like a SN754410 or L293D. They still worked fine though, just not as well.
By pulsing (PWM) the pins, I was able to get the motor to spin at different speeds. The harder part was to get the encoder pins to send data back; more specifically, what type of data they were sending back and how to read it.

Initial testing

I coded up a simple Arduino sketch to do an analog read from those pins at every button press to see if there was a discernable pattern emerging depending how fast the motor turned. After at least 3 hours of fiddling about, I found this library someone had coded to control the NXT motors for a university project; however, not being a university computer science student, new to the Arduino platform and tired of those damn motors, I could not decipher it. I like to understand what it is doing: I could not! Plus, I noted that his project had a bit of external circuitry involved, something I tried to find in his paper, but being in Italian, nothing could be gleaned.

This is a schematic I made depicting how my tests were carried out, adding in the SN754410 for added safety. I am still not quite sure how to use PWM with one of these chips, whether to pulse the enable or input, but I am sure my electronic-savvy friends will pummel me with operational amplifiers when I get back to school for getting it wrong! I have also included the code.
Image developed using Fritzing. For more circuit examples, see the Fritzing project page

Lego NXT actuator data connection test
Reads the analog value at pins A4 and A5, being pins 6 and 5 on the NXT cable respectively.
The motor enable pins are connected to I/O pins 3 and 5
A push-to-make button voltage divider circuit (10kΩ) is connected to I/O pin 2.

This code was written (badly) by Nicolas Weninger, auther of TheCompBlog.

This code is in the public domain.

const int button = 2; //push button pin
const int pin5 = A4; //pin 5 of the NXT cable
const int pin6 = A5; //pin 6 of the NXT cable
const int mtrEnA = 5; //motor enable pin A
const int mtrEnB = 3; //motor enable pin B

void setup()
pinMode(mtrEnA, OUTPUT);
pinMode(mtrEnB, OUTPUT);


void loop()
digitalWrite(mtrEnA, HIGH);
digitalWrite(mtrEnB, LOW);
if(digitalRead(button) == 1) //only feedback data when the button is pressed
int pin5val = analogRead(pin5);
int pin6val = analogRead(pin6);
Serial.print("pin5 = ");
Serial.print("pin6 = ");
Serial.println( pin6val);

while(digitalRead(button) == 1) //hold until the button is released


I was to later find that the encoder pins needed their own, slightly complex, external circuitry, for which I did not have the components or expertise! On my search to find this out, I stumbled across this nifty Arduino shield. It is a shield with all the external circuitry for two NXT motors plus one sensor, with the correct NXT header female ports. I will be purchasing one in the near future, but it was still enjoyable to try without it!
So, I have the stepper motor working without the ‘stepper’ aspect of it until I get my hands on that Arduino shield. In the next post [I am too tired to continue writing!] I shall be moving on to the sensors, so stay tuned!

Update: Part 2 is now up.


One thought on “Hacking the Lego NXT – Part 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s