Build this Raspberry Pi guardian robot and stave off intrusions!

A new DIY build, courtesy of the good folks at Dexter Industries.
Written by Greg Nichols, Contributing Writer

Once inside the GoPiGo folder, double click the Projects folder, and finally double click the AgentKK folderAllow me to appeal to the kid in you.

Younger siblings -- gross! They sneak into your room and grime up your stuff. Sometimes you might also feel that way about your spouse. Babe, get your greasy mitts off my sunglasses.

What you need is a sentinel, a guardian, a robot defender. This latest project comes to ZDNet via Dexter Industries, which makes cool components for DIY robotics builds. I have no affiliation to Dexter, but I dig what they do and I'm happy they've brought us a new build.

Build and program this little GoPiGo to protect your wardrobe, your closet, or your whole room. Hide GoPiGo behind a closed door and when the door is opened it will attack, scaring intruders and snapping a picture as proof that your inner sanctum has been violated.

What you'll need

You can choose to use any of the last three items, or all of them, or none of them. The more you use, the better the protection, but if one is not available, it's not a deal-breaker. The Guardianwill guard without it.

If this is your first project with GoPiGo, take a look at the Getting Started pages before implementing this project.

1. How to assemble the guardian of your man or woman cave

Agent KK, your mission, should you choose to accept it, is to assemble the Guardian following these steps. We will deny all knowledge of your mission (but if you have questions, feel free to contact us).

  1. Make sure your GoPiGo is up and running and connected to your home network. Check your batteries.
  2. Plug the ultrasonic sensor into port A1. It doesn't need the servo, so don't worry if you don't have it. You do need to find a way of attaching the ultrasonic in a fairly stable position, pointing forward.
  3. Plug in the camera module. Installing the camera is a bit more time consuming. Please refer to these instructions, especially the video to see how to attach the camera to the Raspberry Pi. You do not need to attach it to a servo for this project, you can attach it directly to the chassis.
  4. Plug the buzzer into Port D11. D11 is underneath the red board, so you have to flip GoPiGo on its back to see it. Attach the buzzer to the GoPiGo using tie wraps, or any other method that you have available to you.
  5. Plug the speaker into the audio plug on the Raspberry Pi itself. If it's USB powered, you can use one of the USB ports on the GoPiGo. Otherwise, make sure its batteries are good.

2. Getting the code

The code for this project is available for both Python and Scratch. Use "DI Update Software" to get the latest versions of our projects (you can find this script on your Raspbian for Robots desktop). Depending on which version you want to use, choose one of the following two methods to access the code. Just don't execute the code right away as our Guardian is not in position yet.


Launch Scratch through the "Scratch for Robots" interface. If you need help with this step, please follow the instructions described in this tutorial

Click on File/Open (you can also use Ctrl-O for a direct access) to get the File dialog.

Click on the Desktop button, then double click the GoPiGofolder.


Once inside the GoPiGo folder, double click the Projects folder, and finally double click the AgentKK folder.


Choose agentKK and click the OK button to load it up inside Scratch.


Open up a Terminal window.

Navigate to ~/home/pi/Desktop/GoPiGo/Projects/AgentKK

cd ~/Desktop/GoPiGo/Projects/AgentKK

if you want to see what the Python code looks like, you can:

nano agentKK.py

3. Testing

Let's verify that everything is working, before sending our Guardian off to do its job.

Start by putting GoPiGo on its back so it won't run off when it detects an intrusion. Be careful not to hurt the ultrasonic sensor or the camera when you do this.Place an obstacle in front of the ultrasonic sensor, less than 10 cm or 4 inches away.Launch the script.

  • Scratch: click the green flag in Scratch
  • Python:
Sudo python agentKK.py

Remove the obstacle in front of the ultrasonic sensor. There should be plenty of space in front of it now, enough to trigger the attack mode.

Are you getting the appropriate reactions?

  1. Yelling can be heard through the speaker
  2. A loud noise comes out from the buzzer
  3. A photo is taken, it is saved to your Pi Desktop
  4. The two LEDs on the GoPiGo blink
  5. The wheels on the GoPiGo turn (and if the GoPiGo wasn't on its back, it would be going forward)

4. Sending the GoPiGo on its mission

The Guardian is meant to guard a closed door against unauthorized opening.

  1. Put it behind a door, either a closet, or a bedroom door.
  2. Start the code remotely via your laptop
  3. Scratch: click the green flag
  4. Python:

cd /home/pi/Desktop/GoPiGo/Projects/AgentKK

sudo python agentKK.py


For an ultimate test, open the door, and watch it go!

Editorial standards