LEGO Mindstorms Class:  Lesson 1

Some Important LEGO Mindstorm Parts


Ultrasonic Sensor

Light Sensor

Touch Sensor

Color Sensor




Straight Beam

Angled Beam


The NXT-G Programming Environment

Write a simple program to make the brick play a sound

  1. Connect the USB cable between the brick and the computer.
  2. Select the Sound brick from the Action Blocks, and select a Sound file to play.


 The 'Play Sound' Program                                                The Sound Block Settings

  1. Select "Save" from the file menu, and save your program with the name "Play Sound".
  2. Press the "Download and Run" button.  Did it play?
  3. Unplug your brick from the computer.  See if you can find the program named "Play Sound" on the brick and play it directly from the brick.
  4. Congratulations!  You just wrote and ran your first NXT-G program!


Change the "Play Sound" program to repeat itself 3 times using a loop

  1. Select the "Loop" brick from the Flow Blocks and put it after the "Sound" block
  2. Put the "Sound" block from before inside the loop, and set the loop to repeat 3 times.

          The Sound block inside a Loop                                                   The Loop Block settings 

  1. Download and play the"Play Sound"  program.  Did it do what you expected?

Building a Door Alarm:

The parts you'll need:

Step 1:  Attach connectors to the arms:

Step 2:  Attach the arms together, and connect them to the ultrasonic sensor and the brick:

Step 3:  Connect a wire from the ultrasonic sensor to port 4 on the brick.

The Door Alarm Program:


  1. Repeat forever
  1. Wait for Ultrasonic Sensor to detect an object closer than 16 inches
  1. When an object is detected repeat the following 3 times
  1. Make a sound
  1. Return to 1)

Write the Program:

  1. Start with a Loop (from the Common Blocks List).  Set the Loop to run "Forever"

The Loop Block                                         The Loop Block Settings

  1. Put a Wait Block (from the Common Blocks List) inside the loop.  Set the control to "Sensor" and select "Ultrasonic Sensor" from the Sensor list.  Make sure the Port is set to "4" (where the Ultrasonic Sensor is plugged in), and the Distance is set to "< 16 Inches".

A Wait Block inside the Loop Block 

                                            The Wait block Settings

3.   Add a loop after the Wait block, and set the control to "Count" and the "Count" to "3".

               A loop inside the main loop                                    The inner Loop settings

4.   Put a sound block inside the inner loop and choose a sound to play when the alarm triggers

The finished Door Alarm program

5.  Save the program with the name "Door Alarm".  Download and test your program.  Did it work?   Check with your instructors if you have any problems.

If you finish the door alarm try this!

(1)  Pretty Tricky:  Can you change the program so that the alarm makes a different sound if the object it detects is closer than 6 inches?  Hint:  You will have to use a "Switch" block to perform different actions if the object is closer than 6 inches, or further than 6 inches.  Think carefully about where to put the Switch block, and if some of the existing program needs to go inside the Switch block.

(2)  Even Trickier:  Can you change the program so that the screen on the brick displays the distance detected?  This will require a block that will convert the distance (a number) to text (letters) to be displayed on the screen and a block to display the text.  It also requires the use of data wires to carry the output data from the ultrasonic sensor to the converter block, and from the converter block to the display block.  Don't worry if you can't do this one yet, we'll be covering data wires later in the class.

(see your instructors for help with the answers)