Keyboard Controlled NXT Robot


The Project:

In this project I made a Lego Mindstorms NXT robot which is connected by USB to my Mac laptop. The robot moves and is controlled by my laptop’s keyboard. The program for this robot is written in the programming language Python and the Python module nxt-python.


July 22,2014:

I completed the project today.The hardest part of this project so far was finding out how to install all the software I needed such as nxt-python. I found out that to install nxt-python I had to install a python library called Py-USB. I installed the library but then I saw that it wasn’t working. After a couple hours of research I discovered that for Py-USB to work I had to install a C library called LibUsb which allows applications easy access to USB devices. After installing all the software writing the code actually turned out to be the easy part and there weren’t many bugs or problems in it. 

July 23, 2014:

After finishing the main project I have decided to go to the next step which is connecting the robot to the Raspberry Pi to be able to control it with a wireless keyboard. I have also decided to add a camera to the robot called the Raspicam which is connected to the Raspberry Pi and will move up and down when I press the keyboard keys. I will also stream the camera’s live video to be able to view it on the iPad. I have a separate project just for streaming from the Raspberry Pi camera which can be found here: .

How To Do It Yourself:

Below are the steps for creating the robot but they only work if you have a Mac you can go to for more information on how to install nxt-python on other operating systems.

Step 1: Build the the Lego Mindstorms Robot:

You need the Lego Mindstorms NXT 2.0 kit to build this robot. You can buy it on Amazon but it is pretty expensive (around $529 on Amazon).The robot is basically like a car with two motors which are connected to the NXT Brick. The instructions are included in the user manual of the NXT 2.0 kit. make sure the left motor is plugged in to PORT C and the right motor is plugged in to PORT A on the NXT brick.

Step 2: Install LibUSB:
LibUSB is a C library that gives applications easy access to USB devices on many different operating systems. To install it first you need to install homebrew. Do this by going to your Mac’s Terminal and copying and pasting the following command.
ruby -e "$(curl -fsSL"
Then press enter.
Next enter the command brew install libusb into the Terminal and press enter.

Step 3: Install PyUSB:
PyUSB is a python module for communicating with USB devices.
To install it go to and download the file. Once the file is downloaded unzip it and copy and paste the folder onto your Desktop.
Then go to your Terminal and enter cd Desktop/pyusb-1.0.0b1 and press enter. This will take you inside the folder in the Terminal. Then enter python into the Terminal and press enter.

Step 4: Install nxt-python:
nxt-python is a Python module used to communicate with the NXT brick. To install it go to and download Unzip it and place the resulting folder onto your Desktop. Then go to your terminal and enter cd Desktop/nxt-python-2.2.2 and press enter. Then enter python into the terminal and press enter.

Step 5: Write your Python program:
You can copy my python program or look up the nxt-python documentation and make your own. Just create a text file in text edit and type the following code or create your own:

import nxt.locator

import sys

import tty, termios

from nxt.motor import *

b = nxt.locator.find_one_brick()

m_left = Motor(b, PORT_C)

m_right = Motor(b, PORT_A)

both = nxt.SynchronizedMotors(m_left, m_right, 0)
rightboth = nxt.SynchronizedMotors(m_left, m_right, 100)
leftboth = nxt.SynchronizedMotors(m_right, m_left, 100)

def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
ch =
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch

ch = ‘ ‘
print “Ready”
while ch != ‘q’:
ch = getch()

if ch == ‘w’:
both.turn(100, 360, False)
elif ch == ‘s’:
both.turn(-100, 360, False)
elif ch == ‘a’:
leftboth.turn(100, 90, False)
elif ch == ‘d’:
rightboth.turn(100, 90, False)
print “Aborted!”

Step 6: Run the Code! :

When you are done with the code save it onto your Desktop with the exact name “” then make sure your NXT brick is connected via USB into your Mac and that the brick is turned on. Now go to the terminal and type cd Desktop and press enter. Then enter python into the terminal and press enter. Use the ‘w’,’a’,’s’,and ‘d’ keys to move your robot and press ‘q’ to stop the program.