BrickEngineer: LEGO Design

LEGO Engineering for LEGO NXT and Robot Enthusiasts

Interface a Potentiometer to the NXT


In this exercise, I will walk you through interfacing a potentiometer (variable resistor) to the NXT brick.
You will need:
– A stripped NXT cable
– A potentiometer with a maximum resistance no more than $10 k\Omega$
– A small piece of wire
– An NXT Brick

This exercise is derived and expanded from a chapter in Extreme NXT by Gasperi, Hurbain and Hurbain.


The NXT monitors the potential difference between the black and white wires with an Analog-to-Digital (A/D) converter. The A/D converter converts this potential difference to a RAW value between 0 and 1023 (10 bits accuracy). This RAW value is given by the ratio

(1) $RAW = \frac{RAW_{max}}{V_{max}} V_{R} = \frac{1023}{5} V_{R}$

where $RAW_{max}$ is the maximum RAW value of 1023, $V_{max} = 5V$ is the voltage used by the NXT A/D Converter, and $V_{R}$ is the voltage drop between the black and white wires.

The circuit diagram looks like this:

NXT A/D Converter Schematic

I have a little $1k\Omega$ potentiometer that can turn over a range of about $0^{\circ}$ to $270^{\circ}$. Below is a diagram. The resistance between the leftmost and rightmost pins is the maximum resistance of $1k\Omega$. We will focus on the resistance between the leftmost and center pins, which varies based on the angle through which the potentiometer has been rotated. To keep things safe, we wire the center pin and rightmost pin together. This doesn’t affect the potential difference between the leftmost and center pins.

Potentiometer Wiring

I will assume that it is a linear potentiometer (a pretty good assumption), which means that the resistance at any given angle $A$ is given by

(2) $R = \frac{A}{A_{max}} R_{max} = \frac{A}{270} \times 1 k\Omega}$

where $A_{max}$ is the maximum angle of the potentiometer and $R_{max}$ is the $1k\Omega$ maximum resistance.

Equation (2) says that if the angle $A = 0^{\circ}$ then the resistance of the potentiometer $R_{max} = 0 \Omega$, and if the angle $A = 270^{\circ}$ then the resistance of the potentiometer is maximum $R_{max} = 1 k\Omega$.

Looking at the circuit diagram for the A/D converter, the potential drop across our potentiometer (represented by resistor $R$) is given by the typical voltage divider relation

(3) $V_R = \frac{R}{R+R_{int}} V_{max} = \frac{R}{R+10k\Omega} \times 5V$

We can now substitute (2) into (3) so that the voltage between the black and white wires is determined by the angle of the potentiometer rather than its resistance. Then we can substitute the result into (1) to get an equation for the RAW value

(4) $RAW = RAW_{max} \frac{A R_{max}}{A R_{max} + A_{max} R_{int}}$

with my particular values, this is

$RAW  = 1023 \frac{A \times 1 k\Omega}{(A \times 1 k\Omega) + (270 \times 10 k\Omega)}$

This formula will let us predict the NXT RAW value based on the angle of the potentiometer.

For my potentiometer, I find that a maximum angle of $270^{\circ}$ gives me a maximum value of 93. This is less than 7 bits of information, and each RAW value corresponds to $2.9^{\circ}$. If you want a nice angle detector, you will probably need a $10 k\Omega$ potentiometer!


1. Before beginning, you need to cut and strip one of the NXT cables so that you can interface with the wires directly. I have placed a layer of solder on mine, so they can be inserted into a breadboard for easy connecting.

2. Next connect the center and right pins of the potentiometer together with a wire

3. Plug the other end of the NXT cable into the NXT brick.

I wrote a simple NXT-G program to read the sensor and display the RAW value. Notice that the Touch Sensor actually reads the resistance between the wires. So we are just replacing the Touch Sensor with a potentiometer. We will use the raw number output of the Touch Sensor Block, which is represented by the 1010 0101 symbol. We then need to convert it to text so it can be displayed on the NXT LCD panel.

potentio-01.rbt Screenshot

You may download it here,
or write your own.

When I try my potentiometer, I find that the RAW value goes from 0 to 95, pretty close to my predicted range of 0 to 93. So it works! Not bad considering I guessed that the potentiometer sweeps through and angle of $270^{\circ}$.

Determining the Angle of the Potentiometer

Now, let’s convert this RAW value to an angle.
In Extreme NXT, the authors worry about the fact that the resulting relationship is nonlinear with respect to the RAW value. As far as I can see, this isn’t a problem. We simply solve (4) above for the angle $A$ in terms of RAW. We can output the angle if we wish, but here I’ll take it a step further and demonstrate the resulting equation by controlling a motor so that it maintains an angle equal to the angle through which I have rotated the potentiometer.

I will leave out the algebra. Try it yourself. Solve (4) for angle A:

(5) $A = \frac{RAW A_{max} R_{int}}{R_{max} (RAW_{max} – RAW)}$

for my potentiometer, this is simply

$A = \frac{2700 RAW}{(1023 – RAW)}$

which is easy to code in NXT-G.
You can download my code here:

The motor control is a bit crude, but it works well enough for the demonstration.
Check out the YouTube video to see it in action!


Danny – NXT Matlab Bluetooth Router

Daniele Benedettelli introduces a MATLAB-based NXC Bluetooth Router. This router relies on connecting a master NXT Brick to a computer via USB. This master NXT Brick then can communicate messages to up to three additional slave NXT Bricks up to a distance of 10 meters from the master. This software would allow one to create small swarms of up to three LEGO robots, which is a nice starting point for investigating distributed robotic systems.

MATLAB NXT Bluetooth Router

MATLAB NXT Bluetooth Router

The system relies on the RWTH – MINDSTORMS NXT Toolbox, the NXT Fantom Library, and John Hansen’s enhanced firmware.  The brick software is written in Not eXactly C (NXC), which requires Brick CC 3.3.

Daniele Benedettelli also has a book published titled Creating Cool MINDSTORMS NXT Robots (Technology in Action)

Infrared-Ultrasonic Beacons for Localization

An article at highlights a three-wheeled robot that moves in one dimension and detects signals from an external beacon that emits ultrasonic bursts.  The robot relies on a microcontroller that runs a Kalman filter to perform and maintain spatial localization.  The NXT software is implemented using the LabVIEW NXT toolkit

NXT Reciever with Kalman Filter

NXT Reciever with Kalman Filter

Details on the project can be found at

MATLAB Packages for the NXT

There are now several MATLAB packages for robotics, and specifically for the NXT.  One paradigm is to run the code on a PC and have it communicate direct commands to the NXT Brick via Bluetooth or USB.  I have found this paradigm to be a bit dangerous since in the event of a MATLAB crash or a miscommunication, the NXT Brick will continue with its last command until ordered to stop.  This has the potential to destroy your robot.  The paradigm that I prefer to use is to write several programs that run on the brick.  These programs take commands from files on the brick that can be uploaded rapidly from the PC.  The MATLAB code then is in charge of sending the command files and starting and stopping programs.  In the event of a MATLAB crash or communication failure, the software running on the NXT Brick can be designed to terminate gracefully.

Here are the MATLAB packages that I know of.  The first two are specifically geared toward the NXT; whereas the last is a general robotics package.

Little Rover with Instructions and Code


I have finally compiled building instructions for my Little Rover, which can be seen above in a 3D Rendering courtesy of POVRay.  An earlier version of this rover can be seen in this YouTube video:

Little Rover Prototype Video

Rover Design

The complete detailed building instructions can be found here in this 94-page pdf file.
Warning: it is about 9MB in size.  The design is not entirely compatible with the standard NXT Mindstorms Kit.  This design relies on two touch sensors, several 1×9 bent liftarms, and as far as I can tell from Peeron, the NXT Kit has only two.  This may require a little redesign.  Other compatibility issues and their solutions can be found in the Parts List in the instructions.

Remember to download the software DriveSmart here as well.
Installation instructions can be found in the zip file.

DriveSmart Code

The main file is called DriveSmart.rbt.  Drive Smart runs four threads:

Drive Thread
The Drive Thread (lowest one of the four) drives until a warning flag is set by one of the other
threads. It then waits until it gets an all clear message via the Wait Until Free block, and then
it starts driving again.

Bumper Threads
There are two threads that monitor the bumpers.
The reaction is only activated if nothing else is currently commanding the robot.  In this case the
bumper has been pressed and the robot will veer away from the hazard.

Ultrasound Thread
This thread monitors the ultrasound rangefinder.
The reaction is only activated if nothing else is currently commanding the robot.  When the robot
comes too close to a hazard, the robot is commanded to stop.  It then looks both ways and then turns
in the direction with more room.  If the robot is within 10 cm of a hazard on both sides, it then
backs up.

The robot can roam about a wide variety of rooms and not get stuck.
He does not detect stairs though!  So be careful.

Download: instructions and code.

Kevin Knuth

10+1 LEGO Design and Engineering Tips

1. Use only parts that are necessary…no less and no more.
In the course of trying to fortify a LEGO construction, it is extremely easy to get carried away and add too many parts. If you are like me, you have a limited supply of parts.  Using too many parts rapidly depletes your supply and can add significant weight to your creation.  If you are building a robot, this extra weight can really slow it down.

2. Build strong connections
We all know that when stacking bricks to make a wall, you need to stagger the bricks so that the next layer of bricks holds the bricks in the lower layer together by covering up the cracks.  This is one way to build strong connections.  You can do even better by bracing the wall with a beam by pinning it to technic bricks embedded in the wall.  By adding a few carefully chosen pieces, it is possible to significantly strengthen a structure.  Just be careful not to go crazy and violate Tip #1.

3. Be aware of design constraints
Every time you add a LEGO part to your creation, you limit the possibilities of what you can make.  When you have nothing you can make anything.  When you grab a brick, you can now only make things that have that brick in it.  Each part constrains the creation. Be aware of parts or constructs or mechanisms that place too strong of a constraint on your design.  You dont have to get rid of them, but just be aware of the role they play in constraining what you do next.

4. Dont become TOO attached to your creation.
Sometimes we find that we really really like a certain aspect of the construction or a set of parts, but nothing else works. The tendency is to get rid of everything else and then build up around those parts we really like. However, these parts are placing strong constraints on the design… often too strong, which is why nothing else works. The solution is to get rid of the problem. In this case, you have to get rid of the set of parts you like because it is over-constraining the rest of the design. You dont have to destroy it (see Tip #9)… just set it aside, but be sure to remove it from your creation.

5. Be open to new ideas.
There is always more than one way to solve a problem. Be open to new, and seemingly crazy ideas. Sometimes these lead to ingenious solutions. If you suspect that you are having problems similar to those described in Tip #4, take some time out to brainstorm and see if you can come up with a new idea!

6. Build in stages.
Designing a complex structure or mechanism in one step is almost impossible. Break the construction up into stages, and consider each stage separately. Sometimes a given stage will still be too complex. If so, break that construction up into stages as well. Just be aware of the dangers in Tip #4 above. Your solution for one stage might be awesome, but if it doesn’t work well with your solution as a whole… it has to go. Again, if you have enough parts, keep your creations. Otherwise, at the very least preserve their memory by building them in a LEGO Computer Aided Design (CAD) system.

7. Watch for opportunities.
Sometimes we get lucky and we find that a part or a set of parts can serve two or more functions. This is an excellent situation as it saves you both parts, size and weight. Watching out for these opportunities, and taking advantage of them when you can, can really help make an elegant and efficient design. Just be careful not to become too attached to the idea, as you could end up in trouble as described in Tip #4.

8. Study your design.
When you are all finished, take some time to study and test your design. Consider both form and function. When considering form, you are concerned mainly with aesthetics (beauty). What do you like about the design? What dont you like? Is it too big? Is it bulky? Can it be made smaller, sleeker, more elegant? When considering function, you are concerned mainly with its operation and effficiency. Does it do what it is supposed to? Do the parts go together well? Does it vibrate? Do the gears mesh properly? Does it get stuck? Is there too much friction in the system? Could it be smoother? Is it safeguarded against parts breaking in high torque situations? Then consider the big picture. What did you do right? What did you do wrong? If you could do it over again, what would you change?

9. Keep your designs
I have mentioned this tip several times above. If you have enough parts, and enough room, keep your creations. Otherwise, at the very least preserve their memory by building them in a LEGO Computer Aided Design (CAD) system and generate building instructions. That way you can keep a good idea. Who knows if it will come in handy later?

10. Do it over again!
When building mechanisms that require careful consideration of either form or function, you should plan to make several prototypes (a prototype is a first design). Don’t destroy what you just constructed. You may need to refer to it by copying a part of the design that worked well. You may also need to see if you have improved the form or function by comparing it to your first attempt. I personally plan to make at least three prototypes until settling down with a final design.

11. Color coordinate your creation
I dont apply this rule to my first prototypes, but as I settle in on a final design, I work to choose the brick colors carefully. Of course, we do not always have enough parts to do this, but it is worth the extra effort. Colors scattered all over a design leads the eye to seeing it as haphazard rather than elegant. A careful choice of colors can really enhance the form of your creation. You can also use colors to enhance the function by color-coding functional segments of your design. This is maybe better for illustrative purposes (as in a LEGO CAD design), but usually I choose the latter and aim for an elegant coloration.

Kevin Knuth
Albany NY

Content Protected Using Blog Protector By: PcDrome.