Jump to main content

X Marks the Spot: Build a Robot to Protect Your Treasures

122 reviews


You have secret treasure that you need to keep safe from thieves. How do you keep guard 24 hours a day without skipping school and the rest of your important activities? You build a guard robot and have it do the hard work! You can build a guard robot using a LEGO® MINDSTORMS® set and the instructions in this project to guide your efforts. And do not worry: your secret is safe with your robot.


Areas of Science
Time Required
Average (6-10 days)
Material Availability
This project requires a LEGO® MINDSTORMS® NXT 2.0 kit and a computer. See the Materials tab for details.
Very High (over $150)
No issues

Michelle Maranowski, PhD, Science Buddies

  • LEGO® MINDSTORMS® is a registered trademark of the Lego Group.


To build and program a robot to guard a territory.


Treasure means different things to different people. Some people treasure their rare Pokémon cards. Other people treasure stamps, books, or jewelry. Different people find value in different things, but one thing we all agree on is that we should guard whatever items we treasure. Which of your possessions do you treasure and how do you keep them safe from a sibling or other treasure seekers? Maybe you put your things in a lockbox and then bury it in the backyard. That could work, but your treasure would stay safe only as long as you physically guard the location to prevent others from digging it up. Is there a way to guard your treasure 24 hours a day, even when you are not there? Yes: you could build a guard robot to do the job. Your guard robot could sound an alarm and give any treasure seekers a little push to warn them away if they come within the area of the treasure.

But what is a robot, how do you build one, and is it easy to build? A robot is mechanical device that has been programmed to carry out a series of actions. Generally, a robot is created to help humans do tasks, both complicated and simple. For example, robots aid in medical procedures, collect data from the deepest oceans to the surfaces of other planets, and help us build things. In this robotics engineering project, you will build a small guard robot using LEGO® MINDSTORMS®. Several different companies make robotic platforms that you can use to build a robot. For this project, the Procedure is written using the LEGO® MINDSTORMS® platform because it is easy to get started with and learn to use and put together, and there are online forums where you can get help if you need it. However, if you have access to another robotics platform you can take the general ideas in the Procedure and adapt them to that platform. You will build a guard robot and write a computer program to make it to do exactly what you need it to do. The instructions in the following procedure will help guide you. You can even decorate your guard robot as an animal to help divert an intruder's attention.

Because everybody's treasure location is different, you will build a model guard robot and program it to defend a territory that has a 70 centimeter (cm) radius circle from an intruder. Your robot will detect the intruder and then "push" it out of its circular territory. Ready to get started? Great! Begin with the procedure and start forming your own robotic security team.

Terms and Concepts



These tutorials provide an overview of the NXT programming software that you will use to communicate with and direct your robot.

The following two sources are manuals that describe how to write programs with NXT software.

Materials and Equipment

LEGO Mindstorms Availability

Note: The LEGO Mindstorms product line was discontinued at the end of 2022. However, many Mindstorms kits are still in circulation and you can probably borrow one or purchase a used one. The instructions for this project were written for the LEGO Mindstorms NXT 2.0 kit. You can do this project with an EV3 kit, but the programming software is different. Follow LEGO's online resources to learn how to program your EV3 robot.

Experimental Procedure

Note: This engineering project is best described by the engineering design process, as opposed to the scientific method. You might want to ask your teacher whether it's acceptable to follow the engineering design process for your project before you begin. You can learn more about the engineering design process in the Science Buddies Engineering Design Process Guide.

Designing the Guard Robot

  1. In this project, you will create a guard robot that monitors a space or territory defined by a circle with a radius of 70 centimeters (cm). The guard robot will need to scan the territory continuously and monitor when an intruder enters. When the robot detects an intruder, it must move to block or "push" away the intruder, then return to its original location and continue scanning.
  2. Start by organizing the specific details of the project. These details are called the design criteria. Having a good set of design criteria will help you focus your efforts. The following is a set of questions to help you think about your design criteria. Note that it is not a complete list. You can either use this list of questions to develop your design requirements, or you can develop your design criteria based on your own ideas.
    1. What should the robot look like? Should it be big and heavy in order to easily nudge an intruder or light so that it can quickly move to the intruder's location? Should it be tall or low to the ground? Tall objects tend to be unstable and topple over when they move quickly. Your robot should be no more than 6 - 7 inches tall.
    2. How will the robot turn? In order to scan the 70 cm territory, the guard robot needs to easily turn or pivot about a point. You will find the parts in your robot kit that will enable you to make a pivot with two wheels. The pivot should be strong enough to carry the weight of the robot.
    3. How will the robot move? In addition to the pivot, you will probably need to use at least two more wheels to move the robot. These additional wheels should be placed behind the pivot and should each use a motor. Your design will have at least two motors.
    4. Will the robot be working on a carpeted floor or on a wood floor? How will this affect the pivot?
    5. What kind of tires will you use in your robot? Tires with smooth tread move better on certain types of surfaces like hardwood flooring.
    6. How will the robot "sense" the presence of an intruder? It will need a sensor. Because your robot must detect objects or intruders, the best sensor to use is an ultrasonic sensor. Ultrasonic sensors can measure distances and "see" where objects are. Where on the robot should the sensor be placed in order to see an intruder accurately? If it is placed too high, it may not detect smaller intruders. If it the sensor is placed too low, it may get in the way of the robot or the cables.
    7. Do you want the guard robot to make a noise or play music when it senses an intruder?
    8. What will the robot use to push out the intruder? Will it have pincers or large shovels? Or will it scare away the intruder with a loud alarm?

Building the Guard Robot

  1. Once you have created a set of design criteria, it is time to build the guard robot. Your kit comes with an NXT brick. The NXT brick is the "brains" of your robot. It has a microprocessor that you program to give the robot its instructions. The NXT brick will be the center of your design and you will build off of it. Hint: Build the structure of the robot first and then find a way to attach it to the NXT brick. This will give you a sturdier robot.
  2. Open the kit and familiarize yourself with the parts. There are three motors, axles, beams, connectors, and the NXT brick that you will use to build the robot. Use the connectors to attach beams to each other and other parts to beams. Use the axles to connect wheels to each other and to the motors.
  3. You will use at least four wheel and tire assemblies to build your robot. Two wheel and tire assemblies will go into building the pivot and supporting the front of the robot. An example of a pivot is shown in Figure 1 below. The pivot enables the robot to move in a circle and also support the weight of the robot.
Two LEGO wheels on an axle connect to a spinning vertical shaft
Figure 1. This figure shows some parts from the LEGO® MINDSTORMS® set that you can use to assemble a pivot. A pivot will enable the robot to move smoothly in a circle.
  1. The second two-wheel and tire assemblies will attach the motors onto the structure of the robot and provide power for the robot to move. 'Figure 2 below shows a motor and a wheel and tire assembly put together.
A LEGO wheel connects to a motor on an axle
Figure 2. This image shows how to attach a wheel and tire assembly to a motor, which you can do by simply pushing an axle through both pieces to connect the two.
  1. When building the robot, keep in mind that you must balance the weight of the robot over its entire body. If one end is heavier than the other, the robot could flip over. Figure 3 below shows a simple guard robot. ('This is just an example; you should build a robot that you enjoy putting together and that you think will guard your treasure.)
  2. When you attach the ultrasonic sensor, remember the height of the objects you will be attempting to detect. If you place the sensor too high, it won't be able to detect intruders shorter than the sensor. If your sensor is too low, it may impede the motion of the robot. Remember to connect the sensor to the sensor port on the NXT brick.
Photo of a fully assembled guard robot
Figure 3. This is an example of a guard robot. Notice the placement of the sensor. Where will you place the sensor, the wheels, the brick, and other important parts on your robot?
  1. While Figure 3 above shows an example of one type of robot, you should take this opportunity to use your imagination to build a cool robot. For example, make your robot look like an animal or insect. Add pincers, or other extensions, to the body. Just make sure that your robot is stable, is not too tall, and can move smoothly in a circle.

Installing the MINDSTORMS® NXT-G Software

  1. In order to tell the guard robot what to do, you will have to write a program that your robot can understand. You will write the program using the NXT-G software, a graphical-based, drag and drop programming environment. To do this you need to install the NXT-G software on your computer. The software is on a compact disc included with the kit.
    1. Be sure that you have permission from whoever owns the computer to install the software on the computer.
    2. Insert the compact disc in the computer's CD-ROM. Instructions for installation will then pop up on the screen.
    3. If you install the software on an Apple or Mac computer, you may find that you don't see the MINDSTORMS® NXT icon. If this occurs, then call the MINDSTORMS® help line and they will guide you through changing the files for proper installation.
    4. When you finish installing the software, drag a MINDSTORMS® NXT shortcut icon to your desktop.
    5. The NXT language is easy to learn and use. But if this is your first time, then read through the manuals listed in the Bibliography to learn about the different blocks and about the programming environment.
  2. Click on the MINDSTORMS® NXT icon. You will see a screen like the one shown in Figure 4 below. To learn more about the NXT, click on the Getting Started Guide tutorial and the Software Overview tutorial. After going through the tutorials, pick one of the robots on the Robo Center (located on the right of the screen) and duplicate the design using your Mindstorms® parts.
Screenshot of the opening screen to the program Mindstorms NXT-G

A screenshot of the opening screen for the program Mindstorms NXT-G shows a large window at the center of the screen where tutorials and guides can be found. Underneath the tutorial and guides there are options to create a new program or to open a recent program.

Figure 4. This is the screen that you see when you open NXT 2.0.
  1. Have your manual with you while you are playing with the MINDSTORMS® NXT-G software. Read through the first few chapters to learn about the different types of programming blocks (common, complete, and custom). Notice that each block has a configuration panel where you control how the block works. Some blocks have data hubs through which you can pass and share information.

Develop the Program Flow Chart

  1. Keeping your design criteria in mind, make a flow chart to plan out what the computer program needs to do. A flow chart is a diagram of boxes, where each box represents a step in the computer program to make the guard robot perform a specific task. The sequence listed below is one of many ways to think about approaching the problem. Writing down on a piece of paper of what you want your robot and program to do will keep you focused and thinking of any potential problems that may crop up.
    1. The guard robot needs to perform a set of actions when monitoring its territory. First press the orange button on the brick when you are ready to start the guard robot. Then, pause for 10 seconds so that you can get out of the way and not be mistaken for the intruder. This is the WAIT portion of the program.
    2. After waiting, the guard robot needs to scan the 70 cm radius circular area for a predetermined amount of time. You can scan the area forever or for just a few minutes. This is the SCANNING portion of the program.
    3. If, during a scan, the guard robot encounters an intruder, it needs to stop and calculate the location of the intruder. This is the CALCULATION portion of the program.
    4. Then, in the PUSH OUT portion of the program, the guard robot needs to quickly go to the intruder's location, nudge the intruder out of its territory, and then come back to its original location.
    5. The guard robot should repeat steps b through d over and over again until you no longer need its help and can switch it off.
  2. Figure 5 below is an example program flow chart. This flow chart gives more detail to the steps detailed above.
Flow chart for programming a guard robot

The example flow chart shows the actions of a guard robot and is broken into 4 parts: wait, scanning, calculation and push out. The wait section simply checks if the orange button is pushed and waits 10 seconds if it was. Scanning checks if scanning values are set before scanning for intruders until one is detected. Calculations run after an intruder is detected and sends the intruders position information to the motor. Push out involves moving the robot towards the intruder until the intruder is no longer in the protected space, then returning the robot back to its original position.

Figure 5. Program flow chart. This is one of many ways to program a guard robot.

Creating the Guard Robot's Program

  1. Keep your manual with you while you are creating the MINDSTORMS® NXT program. Read through the first few chapters to learn about the different types of programming blocks (common, complete, and custom). Notice that each block has a configuration panel where you control how the block works. Some blocks have data hubs through which you can pass and share information.
  2. Start a new NXT-G program on your computer. Name your program in the Create New Program section and then click on the "Go" button.
  3. Go back to the diagram or flow chart that you wrote in step 3 of the previous section. Now fill out the diagram with NXT programming blocks.
    1. The first step is to make the robot wait until you push the orange button. This is the WAIT portion of the flow chart plan. The robot continually checks to see if you have pushed the orange button. When you instruct the robot (or any computer) to do something over and over again, you use a loop or, in this case, a LOOP block. In the LOOP panel, set the control to "sensor" and the sensor to "NXT Buttons." This tells the robot to look at the sensors and specifically at the NXT buttons. Then choose the button you want to push. Finally, set the action to "Pressed." Figure 6 below shows what a LOOP block looks like.

      Screenshot of a loop block being used in the program Mindstorms NXT-G

      A screenshot of a loop block being used with a sensor control in the program Mindstorms NXT-G. The loop block will continously run and can be set to continously look out for when a button on the robot is pressed.

      Figure 6. LOOP block with configuration panel.

    2. Choose a WAIT block and set the time to wait to ten seconds.
    3. Now your robot has to continually scan a 70 cm radius area for a set time. This is the SCANNING portion of the flow chart plan. Remember that when you ask your robot to do something over and over again, you will use a LOOP block. Design your loop in its configuration panel to operate for the set time you choose. In the control section of the configuration panel, you can choose the scan to last "Forever" or set it to "Time" and then set the desired scan time in the "Until" section. Add the actions that you want your robot to perform in the LOOP block.
    4. The robot should move in a circle in order to scan the territory completely. Pick a MOVE block and place it inside the LOOP block. Because you want it to move in a circular fashion, set the steering to either one motor or the other. Now, experiment with the power setting. You want the robot to scan and move as fast as possible but not so fast that the robot becomes unstable and tips over. Figure 7 below shows a MOVE block.

      Screenshot of a move block being used in the program Mindstorms NXT-G

      A screenshot of a move block being used with 8 data plugs in the program Mindstorms NXT-G. The move block controls the movements of the robot and can be set to move the robot in a circle in order to scan its surroundings.

      Figure 7. This figure shows a MOVE block with its data hub and configuration panel. Notice that the data hub has eight data plugs and that the steering control is in the configuration panel.

    5. As the robot scans, it has to keep checking for intruders. This means that you will need a LOOP block within the larger LOOP block. This second LOOP block will continually check for intruders; it is embedded within the first LOOP block. Once the robot detects an intruder, the program will exit the embedded loop and proceed to the next steps. Set the control in the embedded LOOP block's configuration panel to "sensors" and the sensor to "ultrasonic." The ultrasonic sensor detects obstacles. Set the sensor to detect obstacles within 70 cm. Once the ultrasonic sensor LOOP block senses an intruder within 70 cm, the program will exit the loop. Use a MOVE block and stop the robot.
    6. Once an intruder has been detected, what does the robot do? This is the CALCULATION portion of the flow chart plan. Once your robot has detected an intruder or treasure seeker, it has to push it out of its territory and sound an alarm. But in order to push it out, the robot must calculate the intruder's location. A simple way to calculate the intruder's location is to average the location of the starting edge of the intruder and the ending edge of the intruder. That will give the center location of the intruder.
    7. To find the location of the intruder, use the rotation sensor in the motors.
      1. Drag and drop a ROTATION sensor block. This block uses the sensor located in the motors. Reset the rotation sensor of one of your motors. This sets the rotation sensor to zero at the starting edge of the intruder. Now you need to find the ending edge of the intruder and then you will know how wide the intruder is.
      2. Use a MOVE block and scan just past the intruder (set the duration in the MOVE block's configuration panel to "rotation" and experiment to get the right number).
      3. Once the robot has completely passed the intruder, use another MOVE block and restart the scan, but in the opposite direction. To do this, switch the steering from one motor to the other in the MOVE block's configuration panel; simply slide the steering cursor from one motor to the other.
      4. Now start scanning for the intruder that the robot found earlier. Use an ultrasonic sensor LOOP block, just as you did in step e.
      5. Once the LOOP senses the intruder, stop the robot with a MOVE block right after the ultrasonic LOOP block.
      6. Now, take a rotation sensor reading from the same motor that you reset in step i. Using a data wire, send the reading to a MATH block. A data wire passes information between blocks and allows the blocks to process the information. You use a data wire to connect data plugs in the data hubs between two blocks. A MATH block is used to perform mathematical operations on data that it receives through data wires and data plugs.
      7. Because you reset the rotation sensor at the starting edge of the intruder and you have the position of the ending edge of the intruder, you know how wide it is.
      8. Send the data wire from the rotation sensor to the A data plug in the data hub of the MATH block and then divide A by two. Remember that "A" is the width of the intruder and dividing A by two gives the center location of the intruder.
      9. Send the center location data, from the MATH block's results plug, to the MOVE block's duration data plug using a data wire. The duration data plug is denoted by a circular arrow in the MOVE block's data hub. The robot will reposition itself directly in front of the obstacle.
    8. Now that the robot knows the intruder's location, use a MOVE block to "push" the intruder. This is the PUSH OUT portion of the flow chart plan. Set the steering arrow in the MOVE block's configuration panel to right between the two motors so that the robot will move straight ahead. You will have to experiment in order to set the correct number of rotations that will "push" the intruder out of the scanned area.
    9. Put in another MOVE block that brings the robot back to its original location. Simply change the direction in the motor block and keep the number of rotations identical to the MOVE block just prior to this action. Remember to keep the steering in between the two motors so that the robot moves straight.
    10. Once your program is complete, save it and then download it to the robot using the special cable that comes with the kit. Now test the computer program and the robot.

Testing the Guard Robot and Program

  1. Build a model intruder. For this project, glue the 6-inch pipe and the cap together with the cement. Let it dry completely.
  2. Once the model intruder is dry, find a good test location. There shouldn't be a lot of foot traffic at the test location or obstacles that are within 70 cm of your test location.
  3. Turn on the robot and start the program. First, try the program without an intruder in the area. Once you are confident that your robot scans the area properly, test the robot with the intruder in the area.
    1. If your robot doesn't scan properly or to your liking, you will have to troubleshoot the robot's body and/or the program.
      1. During testing you may find that the robot's body doesn't work as well as you thought it would. For example, you may need to strengthen the pivot or other parts of the body. Change the position of the NXT brick to provide strength and stability to the robot. Testing gives you the opportunity to try different ideas.
      2. To troubleshoot the program you will have to closely look at each step the robot takes, pretending to be the robot yourself. One block that can help you step through the program is the STOP block. Insert a STOP between steps to make sure that each step is working properly. Once you are sure that a step is working properly, you can remove the STOP block and use it on the next step that you want to investigate.
      3. Troubleshoot the MOVE blocks and make sure that the power setting is at the right level and that the steering is set properly.
    2. Place an intruder outside the 70 cm radius and watch what happens. Bring the intruder closer and closer until the robot detects it. Does the robot respond to the 70 cm criteria?
  4. Test the robot several times to make sure that it will "push out" an intruder each time it encounters one.
  5. Once your robot and program are working, give a demonstration to your friends and family to show how you can keep intruders out of your room and away from your treasures.
icon scientific method

Ask an Expert

Do you have specific questions about your science project? Our team of volunteer scientists can help. Our Experts won't do the work for you, but they will make suggestions, offer guidance, and help you troubleshoot.


  • Could your guard robot monitor a semicircular rather than a circular territory? Modify the scanning portion of your program to monitor a semicircle.
  • Build an additional intruder and test if your robot can distinguish intruders that are close together. See how closely you can place the intruders and still have the robot "push" them out individually.


If you like this project, you might enjoy exploring these related careers:

Career Profile
Have you watched "The Transformers" cartoon series or seen the "Transformers" movies? Both shows are about how good and evil robots fight each other and the humans who get in the middle. Many TV shows and movies show robots and humans interacting with each other. While this is, at present, fantasy, in real life robots play a helpful role. Robots do jobs that can be dangerous for humans. For example, some robots defuse landmines in war-stricken countries; others work in harsh environments like… Read more
Career Profile
Robots are no longer futuristic machines. Robots are here and now and are used in manufacturing, health care, service industries, and military applications. They perform tasks that are repetitive and hazardous—things that humans don't want to do or are unsafe to do. But robots are still machines, which means they require humans to build, maintain, program, and keep them functioning efficiently. Robotics technicians work with robotics engineers to build and test robots. They are… Read more
Career Profile
Mechanical engineers are part of your everyday life, designing the spoon you used to eat your breakfast, your breakfast's packaging, the flip-top cap on your toothpaste tube, the zipper on your jacket, the car, bike, or bus you took to school, the chair you sat in, the door handle you grasped and the hinges it opened on, and the ballpoint pen you used to take your test. Virtually every object that you see around you has passed through the hands of a mechanical engineer. Consequently, their… Read more
Career Profile
Computers are essential tools in the modern world, handling everything from traffic control, car welding, movie animation, shipping, aircraft design, and social networking to book publishing, business management, music mixing, health care, agriculture, and online shopping. Computer programmers are the people who write the instructions that tell computers what to do. Read more

News Feed on This Topic

, ,

Cite This Page

General citation information is provided here. Be sure to check the formatting, including capitalization, for the method you are using and update your citation, as needed.

MLA Style

Science Buddies Staff. "X Marks the Spot: Build a Robot to Protect Your Treasures." Science Buddies, 8 Feb. 2023, https://www.sciencebuddies.org/science-fair-projects/project-ideas/Robotics_p004/robotics/lego-guard-robot. Accessed 6 June 2023.

APA Style

Science Buddies Staff. (2023, February 8). X Marks the Spot: Build a Robot to Protect Your Treasures. Retrieved from https://www.sciencebuddies.org/science-fair-projects/project-ideas/Robotics_p004/robotics/lego-guard-robot

Last edit date: 2023-02-08
Free science fair projects.