Integrating Lego Mindstorm NXT and Arduino

To show off the power of the NXTBee and XBee radios, we decided to integrate the Lego Mindstorm NXT with the Arduino.  Normally, to get the two to talk, it takes some wires, some serious understanding of I2C protocol, and lots of patience.

Using the NXTBee, an XBee, an Arduino, and of course a Lego Mindstorm NXT, we created a computer controlled car.  And again, it was really easy, and the car went far.

The NXTBee wired up to the R/C car, ready to receive commands from the Arduino.

The NXBee wired up to the R/C car, ready to receive commands from its overlord, Arduino.

In this video, the race car reads signals from the NXTBee, the NXTBee gets direction from the Arduino (see picture below) and the Arduino is linked to the computer via USB.  We send signals into the serial communications and the NXT converts that into directions.

Arduino and XBee Example: the Arduino is connected to the Arduino.

Arduino and XBee Example

You can see the whole thing is mounted onto a breadboard.  The XBee has a Sparkfun adapter to make it fit.  Besides connecting the 3.3V and ground up, Pin 3 on the Arduino is connected to Pin 2 (DOut) of the XBee and Pin 4 of the Arduino is connected to Pin 3 (DIn) of the XBee.

We used RobotC (you could just as easily use NXC).  The code is pasted below.  The Arduino code is pasted below as well.  They can both be downloaded here.

RobotC Code:

///—————————————————————————-
/// Crosstalk_receive.c – Crosstalk receive test.
///
/// Created by Dexter Industries, 2011.
/// www.dexterindustries.com
///—————————————————————————-

#pragma platform(NXT)
int power = 0; // Global variable for power.
int steer = 0; // Global variable for steering.
int direc = 0; // Global variable for direciton.

void setupHighSpeedLink()
{
// Initialize port S$ to “high speed” mode.
nxtEnableHSPort();
nxtSetHSBaudRate(9600);
nxtHS_Mode = hsMsgModeMaster;
return;
}

////////////////////////////////////////////////////////////////////////
// Reads a Response from the XBee
// Displays the response from the XBee
// This reads any response from the XBee
////////////////////////////////////////////////////////////////////////
void ReadResponse()
{
byte BytesRead[5]; // Array we’ll be reading into.
nxtReadRawHS(BytesRead[0], 5); // Read the array.

if(BytesRead[0] == ‘/’) { // If we catch the null, read the next three things.
power = BytesRead[1]; //
steer = BytesRead[2]; // -90 is left, 90 is right
direc = BytesRead[3]; // “0” is forward, “1” is reverse.
}
}

//The number of milliseconds to delay between checks of the
//receive buffer.
int MS_TO_DELAY = 270; // 100;

////////////////////////////////////////////////////////////////////////
// Main Task
////////////////////////////////////////////////////////////////////////
task main()
{

eraseDisplay();
bNxtLCDStatusDisplay = true; // Enable top status line display
wait1Msec(MS_TO_DELAY);

//Setup the link to be a high-speed link.
setupHighSpeedLink();

bFloatDuringInactiveMotorPWM = true; // The motors WILL coast when power is not appli
int motorpower = 0; // Variable holds the power.

//The test will continue until an error occurs or
//the enter button on the brick is pressed.
while(nNxtButtonPressed != kEnterButton)
{

wait1Msec(MS_TO_DELAY); // Put in place to not overload the system.
ReadResponse(); // Check the steering, power, and direction.

// Direction and power.
if(direc == 0){
motorpower = power;
}
else{
motorpower = power*-1;
}
motor[motorA] = motorpower;
motor[motorC] = motorpower;

// Change Course
int steer_posn = steer; // Steer Posn is going to be between -90 and 90
int rx_posn = nMotorEncoder[motorB]; // This is where our motor posn is right now.
int tx_posn = steer_posn; // Set the target for Motor Encoder of Motor B to 360.

if(tx_posn > rx_posn){
motor[motorB] = (tx_posn-rx_posn)/2;
nMotorEncoderTarget[motorB] = rx_posn – tx_posn;
}
if(tx_posn < rx_posn){
motor[motorB] = (tx_posn-rx_posn)/2;
nMotorEncoderTarget[motorB] = tx_posn – rx_posn;
}

string steer_posn_str;
StringFormat(steer_posn_str, “%3d”, steer_posn);
}
}

And the Arduino code . . .

#include <NewSoftSerial.h>

int inByte = 0; // Data coming in from computer to ARduino

byte power = 0; // Initializing power, steering, direction.
byte steer = 0;
byte direc = 0;

NewSoftSerial mySerial(3, 2);

void setup()
{
Serial.begin(9600);
// set the data rate for the NewSoftSerial port
mySerial.begin(9600);
}

void loop() // run over and over again
{
if (Serial.available() > 0) { // Take steering information in
// get incoming byte:
// w – forward
// a – left
// d – right
// z – reverse
inByte = Serial.read();
Serial.println((char)inByte);
if (inByte == ‘w’) {
power = 10;
direc = 1;
}
if (inByte == ‘a’) steer = -70; // Turn left
if (inByte == ‘d’) steer = 70; // Turn right
if (inByte == ‘z’) { // Reverse
power = 10;
direc = 0;
}
if (inByte == ‘s’) power = 0; // Stop
if (inByte == ‘q’) { // Go straigh
steer = 0;
}
}
//char data[] = {NULL, 100, 50, 30};
//mySerial.print(data[0]);

mySerial.print(‘/’);
// RobotC now knows the next two transmissions will be power and steering respectively.

mySerial.print(power); // Send Power
mySerial.print(steer); // Send steering info.
mySerial.print(direc);
delay(270);

}

9 Comments

  1. Jason July 27, 2011

    Where can I find the building instructions for the chassis used in this demonstration? Thanks!

  2. Administrator July 28, 2011

    Jason,

    The chassis is only slightly modified from the NXT Programs version of a Race Car . It’s a great design and we have only modified it slightly.

  3. Jason July 28, 2011

    Terrific, thanks guys!

  4. Ryan Tran May 5, 2012

    Can I using Series 1 XBee? and do I need to config. each XBee for transmitting and receiving?
    Thank you

    • John May 9, 2012

      Hey Ryan,
      These are designed for Series 1 XBees; if you buy a version with the XBee radio in it, it will come with a Series 1 radio in it.

      You will need to configure the radio you interface with the NXT; if it’s connected directly to the NXT you need to configure it. We have software for download on our website that does that for you though.

      – John

  5. EJ October 21, 2012

    Hello
    My man is EJ. I’m doing a project that is similar to the Arduino RC car. I’m having a problem with the ROBOTC program that you made. It is with the nxtReadRawHS command. just wondering if there is a library that i need. I am a novice with programming. Could you help me please.

Leave a reply

{"cart_token":"","hash":"","cart_data":""}