Lego Mindstorms Graphical Programming (NXT-G 2.0)

The NXT-G graphical integrated development environment (IDE) provides the five basic elements of programming: input/output, conditionals, loops, variables and functions.  It combines research ideas from MIT, with a graphical IDE from National Instruments.

The graphical IDE reduces the initial learning curve.  Novices can start programming by combining Action blocks to make the motors move and generate light or sound.  Once these concepts they can introduce flow elements such as conditionals, loops and wait.  These flow elements can used embedded sensor readings.  More advanced programmers can read the sensors directly and process the outputs using math and logic functions.

Coming from a more traditional text based programming environment, and trying stay a step ahead of my children, I read Terry Griffin’s excellent book [1] along with some other resources [2,3,4,5,6,7,8].  This is a write up for my future reference.

Work Flow

To create a program, you connect blocks.

  • Drag and drop blocks from the palette onto the work space.  Each block performs an unique function such as turning the motors, or measuring a distance.
  • Use the tabs on the bottom of the palette to switch between sets of blocks.
  • To save your work as a package use Tools > Create Pack and Go.
  • As always, keep it simple.  Divide up the problem in small steps, and test each step before combining them.

To compile, download to the NXT robot and run, you use the controls in the bottom-right of the work space.

  • middle-button, compiles the program, downloads and runs it to  the NXT
  • top-right button, compiles selected blocks, downloads and runs it to  the NXT
  • bottom-left button, compiles the program and downloads it to  the NXT
  • bottom-right button, stops the program on the NXT.
  • top-left button, manages the NXT memory space, click the top-left button; click on the bar graph on the left to select a memory.  This will also let you transfer files.
  • Bluetooth dongle, such as the AZiO BTD-V201 (don’t install the drivers from CD).  To configure the connection use the top-left button on the NXT controls shown above.
  • Gyro Sensor, such as the HiTechnic part to build a Segway.
  • To change the default project directory: change settings.ini located in  “shell:UsersFilesFolder\Documents\LEGO Creations\MINDSTORMS Projects\Profiles\Default”.  For example:
    • Data Directory=”//FILESERVER/Your Name/Lego Mindstorms/Default”
  • Organizer to store the set, such as DeWalt Deep Pro Organizer.
  • Rechargeable batteries, max voltage is 1.75 V for each of the six AA batteries.  NiMH appears to be a good fit.

I/O blocks (equiv. to library functions in C)

A program is composed by connecting blocks on a sequence beam.  Blocks are executed sequentially.


Outputs functions are called “Action blocks” in NXT-G jargon.  The Motor block controls a single motor that for example moves a robot arm, or rotate a wheel.  The Move block, on the other hand, drives two motors, such as to rotate the wheels on a robot to make it go forward.  The Move block synchronizes the motors to keep the robot travelling in a straight line.  In all cases, you specify how far the motor turns, not how far an arm moves or a wheel travels.  For a wheel, the distance traveled depends on the number of motor rotations, optional gears and the circumference of the wheel (14 cm for the snap-on tires).

Other output blocks generate sound, light or write to the display or Bluetooth device.

  • Motor action
    • Motor block
      • Controls a single motor
      • Automatically corrects for rotation errors.
      • Parameters:
        • action, constant or ramp up/down until target is met.
        • power, <10 will not move; >75 moves out of control.
        • control, automatically adjust the power to correct for resistance or slipping.
        • duration, unlimited = switches the motor on and continues with the next block on the Sequence beam.
        • wait, when unchecked, it will move on to the next block on the Sequence beam while the motor meanwhile completes its action.
    • Move block
      • Controls two motors, synchronizing the B and C motors
      • Automatically corrects for rotation errors.
      • Parameters (also see the Motor block):
        • steering, middle = straight; far left/right = spin in place (motors turn opposite directions).
        • next action, brake = provides accurate travel distance and then hold the motor in place; coast = stop the power to the motor so it rolls to a stop (Because the motor coasts to a stop, it will move a little further than the Duration setting. When a following Move or Motor block runs, it will automatically adjust for the rotation error.)
    • Reset
      • Resets the accumulated rotation error, causing the next Motor or Move block to disregard prior rotation errors.  The Reset block should precede the first Motor or Move block in a program.
  • Other action blocks
    • Sound
      • Plays a sound file or a single tone
      • Parameters:
        • wait, when unchecked, the next block on the Sequence beam will start while the sound is playing.
    • Display
      • Writes a text string or shapes to the NXT display
    • Send message
      • Transmits a wireless message to another NXT assuming it has been paired before.  Each NXT has 10 mailboxes each 5 messages deep.  Head-drop when mailbox is full.
    • Color lamp
      • Turns a color lamp on or off.


ultrasonicMost input block read data from external sources and compare the data read to a preset value or range.  The results are made available at its data hub.  The data hub can be shown by clicking on the tab at the bottom of the block.  Data wires connect the outputs to other blocks.  Configuration parameters can also be set by incoming wires.

  • Sensor blocks
    • Ultrasonic Sensor block
      • Measures the distance to an object and compares it to a criterion.
      • Outputs:
        • yes/no, true if criterion is met, false otherwise
        • distance, scaled value from sensor [0..255 cm | 0..100 inches].
    • Touch Sensor block
      • Reads the touch sensor’s state and compares it to a criterion (pressed, released, bumped). Note that a program may not be fast enough to detect the “bumped” state.
      • Outputs:
        • yes/no, true if criterion is met, false otherwise
        • raw value, unscaled value from sensor [0..1024]
        • logic number, 0=released; 1=pressed.
    • NXT Buttons block
      • Reads a NXT button state and compares it to a criterion (pressed, released, bumped). Note that your program may not be fast enough to detect a condition as “bumped”.
      • Output:
        • yes/no, true if criterion is met, false otherwise.
    • Color Sensor block
      • Measures color or light intensity and compares it to a criterion.  When the parameter “light” is checked it generates its own light and measure the reflected light.
      • Outputs:
        • yes/no, true if criterion is met, false otherwise
        • detected color, light intensity [0..100] or detected color (1=black; 2=blue; 3=green; 4=yellow; 5=red; 6=white).
    • Rotation sensor block
      • Measures the number of degrees that the motor turned and compares it to a criterion.  The same block can also be used to reset the rotation counter.
      • Output:
        • yes/no, true if criterion is met, false otherwise
        • direction, true=forwards; false=backwards
        • degrees, scaled value from sensor [0..2147483647].
  • Other blocks
    • Timer block
      • Reads one of the tree timers and compares it against a criterion.  Time is specified in seconds on the configuration panel, but in milliseconds when using input data wires.  The block can also be used to reset a timer.
      • Output:
        • yes/no, true if criterion is met, false otherwise
    • Receive Message block
      • Receives a Bluetooth message and compare it against a criterion.
      • Outputs:
        • yes/no, true if criterion is met, false otherwise
        • message received, true when a message has been received, false otherwise
        • {text|number|logic} out, message data.
    • Random block
      • Generates a random number within a range.  Can be used to make the robot a little unpredictable and thus more interesting.
      • Output:
        • number, random value between specified lower and upper bounds.

Data manipulation

Sophisticated data manipulations are supported through combining several number and text blocks.

  • Numbers
    • Math Block
      • Performs basic math operations (addition, subtraction, multiplication, division, square root, absolute value).
      • Output:
        • result, result of the operation [-2147483648 .. 2147483647]
    • Logic Block
      • Performs Boolean operations (AND, OR, XOR or NOT)
      • Output
        • result, result of the operation [true|false]
    • Compare Block
      • Performs less than (<), greater than (>) or equal (=) operations
      • Output
        • result, result of the operation [true|false]
    • Range Block
      • Determines if a value is within a range of numbers
      • Output
        • yes/no, true of the value is within range, false otherwise
  • Text strings
    • Number to Text block
      • Converts a number to a text string
      • Output
        • text, text representation of number [string]
    • Text block
      • Concatenates a maximum of three text strings
      • Output
        • text, concatenation of text strings [string]

Program Flow

Flow elements can take input from an embedded sensor or from a data wire.

  • Conditionals
    • Switch block (equiv. to switch())
      • Checks for a condition, and runs blocks on the sequence beam corresponding to that condition.
      • More then two conditions are allowed.
      • The ‘*’ button selects the default condition.
      • Floating point inputs are rounded to the nearest integer before comparing them.
      • Comments should be left outside a switch block.
  • Wait
    • Wait block
      • Busy-loop to poll an input until the specified condition is met.
  • Loops
    • Loop block (equiv. to do while())
      • Repeats a set of blocks until the end condition is met.
      • The loop count output is updated at the start of the last run through the loop, it will be one fewer than the total number of times the loop repeats.

My blocks (equiv. to user functions)

My blocks should be used to abstract program sections that perform specific functions.  Dividing a program into smaller pieces, makes the program easier to read and maintain.  Each My block can be tested independently and reused in the same or other programs.

  • Usage
    • Create, select the blocks; “Edit > Make a new My Block” from the menu
    • Open, double-click the My Block
    • Use, select from the Custom Palette (using the tab at the bottom of the Palettes)
    • Close, “File > Close” from the menu
    • Organize, “Edit > Manage Custom Palette” from the menu; then use the file system to organize.
  • Creating My blocks that have a data hub
    • Use compare, text or math blocks to supply input values to the blocks that will become a new My block.  These additional blocks are only needed to supply the data wire, and will not become part of the My block.
    • Select the blocks for the new My Block and call “Edit > Make a new My Block” from the menu.  All the data wires will remain connected. The value of the wire will also become configurable using the Configuration Panel.
    • Note that once a My block is made, you can change the name of data plugs, but you can not add more plugs.  You will have to recreate the My block instead.
  • Appears broken if
    • two data plugs have the same name, or
    • there is problem with the data wire connections, or
    • the source file for the block has been moved or renamed in the computer’s file system.
  • Note
    • If you absolutely have to copy a My block, then use the file system.  Don’t use “save as” from the menu.
    • When moving a program to another computer, also move the My Blocks or better yet create a package that contains all the related files (“Tools > Create Pack and Go”)


dataMost blocks have input plugs that correspond to the parameters in its configuration panel.  Using the data inputs, a program can change these parameters.  For example, the output of an ultrasound sensor could be used to specify the power of the motors, thereby slowing down the robot when it approaches an object.

Data wires (equiv. to mailboxes)

In the NXT-G world, data passes over so called “Data wires”.  A Data wire connects one output to one or more inputs.

  • Scope
    • Local to the blocks that it connects to the wire.
  • Usage
    • draw a wire between blocks
      • expand the data hubs (click on the tab at the bottom of a block).
      • the plugs on the left are inputs; the plugs on the right are outputs.
      • click on an output terminal
      • click on the input terminal that you want to connect.
    • draw a wire into the boundary of a Switch block
      • enable tabbed view (disable flat view)
      • connect a wire to the input
      • switch back to flat view
    • draw wires from within the boundary of a Switch block
      • enable tabbed view
      • select the “true” condition and connect a wire
      • select the “false” condition and connect a wire from the output to the edge of the block where the existing wire crosses it.
      • switch back to flat view
    • delete a wire
      • click the data plug at the right of the wire.
  • Note
    • Use variables instead of passing data wires in/out of a Loop or Switch block.
    • Occasionally, a wire may be routed behind another block, making it appear as if its terminals are connected.
    • A dashed grey wire indicates a problem, such as
      • two plugs have different data types (Boolean, integer, float, string)
      • the output of a pass-through plug is not used without the input side connected
      • multiple inputs are connected to one output plug.
      • an input is connected to an output that appears later on the sequence beam.

Variable block (equiv. to variables)

Variables are expected to change over time, such as a distance to a wall.  A variable can be used to connect one or more outputs to one or more inputs.

  • Scope
    • Local to the program (and its My blocks) that defines it.
  • Usage
    • create a variable, “Edit > Define Variables” from the menu.
    • use a variable, insert a Variable block.
    • delete a variable, first remove any Variable blocks that use it.
  • Notes
    • A program shares its variables with its My blocks if they have the same name and data type.
    • Perhaps not the best coding practice, but variables can be used when a My Block needs to remember a value.

Constant block (equiv. to const or #defines)

As the name implies, constant values do not change.  An example is the diameter of the wheels on a robot.  A constant block supplies a constant value to more or one inputs.

  • Scope
    • Global to all programs.
  • Usage
    • create, “Edit > Define Constants” from the menu.  Specify the action “choose from list”.
    • use, insert a Constant block.
    • change, save any open programs that use that constant.
    • delete, “Edit > Define constants” from the menu.  Note that any Constant block that uses that constant will change to Custom.
  • Note
    • When working with a program that uses custom constants, note that:
      • changing the value in one Constant block won’t affect the other programs.
      • creating a constant using the Edit Constant dialog will change all “Custom” constants with the same name.

File Access block

Files can store settings or input/output data for a program.

  • Scope
    • Global to all programs running on a NXT.  Remain available after the program ends.
  • Usage
    • create, insert a File Access block; select create as the action.  A list of files already on the NXT will appear (assuming it is connected using either USB or BT).
    • write, insert a File Access block; select write as the action to appends to a file.
    • read, insert a File Access block; select read as the action.
    • delete, insert a File Access block; select delete as the action.
    • close, insert a File Access block; select close.  Remember to close a file after writing to it.  Otherwise you can not delete or read from it.
  • Note
    • A program can write to or read from up to four files at once.
    • The Initial File Size data plug that allows you to specify how much memory to allocate for the file. If the file grows past the initial size, the NXT will move the file to larger memory space, thereby introducing a slight delay.

Sequence Beams (=threads).

Multitasking means doing more than one thing at a time. The sequence beam can fork, allowing your robot to do more than one thing at a time.  But first, a short word about execution order:

Execution order

  • In general, a block will finish what it’s doing before the next block on the Sequence Beam runs.  Exceptions are when “wait for completion” is unchecked, or a Move/Motor block is set to run indefinitely.
  • A block starts once all input values on the data wires are available.
  • A Loop/Switch block starts only once all the input values are available on data wires that enter the block (even though they might not be immediately used)
  • A data wire that starts inside a Loop/Switch block and connects to a block outside will only have a value after the Loop/Switch block finishes.
  • Only one copy of a particular My Block can run at the same time (non-re-entrant)
  • A program ends once it reaches the end of all the Sequence beams.

To add a task

  • Expand vertical space:
    • Add a Display block (that has a large data hub) at the end of the Loop block.
    • Open the data hub to create space within the loop.
    • Add a small comment to the bottom-left side of the Loop block to keep it open after the Display block is removed.
    • Delete the display block.
  • Add a new Sequence beam:
    • Add a new block under the existing Sequence beam.
    • Shift-click the existing Sequence Beam, and connect it to the new block.
  • To expand horizontal space:
    • Select the outer Loop block.
    • Click-and-hold the mouse on the Sequence Beam between the top-right block and the edge of the Loop block.  Slowly drag the mouse to the right to widen the loop block.


  • Especially when working with multiple Sequence beams, allow the NXT-G some time to catch up with your editing.
  • Do not access the same sensor or motor from different Sequence beams.
  • Blocks on different sequence beams can communicate using variables, not pure data wires.


Often debugging a program proves to be more challenging than writing it.  The NXT-G environment offers the following debug approaches:


[1] The Art of Lego Mindstorms NXT-G Programming (book)
Terry Griffin, 2010
[2] The Lego Mindstorms NXT 2.0 Discovery Book (book)
Laurens Valk, 2010
[3] Creating and using your own blocks with My Blocks
Dave Parker, 2007-2012
[4] Robots in a box (article)
New York Times, 2008
[5] Basic Programming: Tips, Skills, & Resources (presentation)
Tony Ayad and LeRoy Nelson, 2012
[6] NXT Tutorial (video)
Dale Yocum at Catlin Gabel School, 2012
[7] FFL Programming 101
Innovations in Science and Technology Education, 2007
[8] Example projects by complexity
Dave Parker, 2007-2012
[9] Programming solution for NXT – debugging [link dead]
Robot Magazine, 2012
Johan Vonk

Johan Vonk

Johan is a student in Los Altos, CA.I see education as the foundation upon which entrepreneurs are able to build innovative organizations and execute their vision for the future.
Johan Vonk

Latest posts by Johan Vonk (see all)

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>




Protected with IP Blacklist CloudIP Blacklist Cloud