MISSION OBJECTIVE
Learn how to set off an alarm when an object breaches a set perimet
In this mission, you will help Piperbot and Pip detect when an object breaches a set perimeter using the Distance Sensor and sound an alarm. Ask students: “What do you think home security systems use as the sensor? What about smart doorbells?”
View student interface at make.playpiper.comMISSION CHARACTERS
MISSION MATERIALS
Computer with USB port and Chrome or Edge browser
Piper Make Base Station or Starter Kit
Distance Sensor
MISSION RESOURCES
Learning Goals
- Students will build a “security zone” around their Distance Sensor and sound an alarm when a Zomar breaches the zone.
- Students will learn to use if/then statements in code to create conditional statements.
- Students will understand how the Distance Sensor measures distance using sound waves reflected off objects.
Career Connections
Robotics Engineer: Salary $104,600/yr
Automotive Engineer: Salary $95,300/yr
Civil Engineer: Salary $88,050/yr
Electro-Mechanical and Mechtronics Technicians: Salary $60,570/yr
Hardware Diagram
Tutorial Steps
Security ZoneIf you've got your Ultrasonic Range Finder sensor, let's get started!
Let's pretend you're a dolphin for a second. Go ahead, make your best dolphin face, we'll wait. So the way dolphins orient themselves is through a process called echolocation, where they let out a series of clicks which reflect off of objects around them. Dolphins then sense the sound waves coming back and can figure out where objects are in space — which is exactly what our range finder does as well!
The colors of the wires on your 4-pin cable can vary, so it is important to match the pins on your sensor module to the correct connections on your Breadboard.
Remember, the DIGITAL VIEW is a handy way to see the GPIO pin locations. For this circuit, you will connect:
- GND to G (ground)
- SIG to GP22
- VCC to 5V
To do this, we're basically going to want to print out the distance measured by the sensor, right?
So let's grab a start block and drag it to your programming space. Then find the print block under Chip menu and drag it to the start block.
We want to read the sensor more than once, so start with a repeat forever block from the Loops menu and connect it to the start block.
Find the range finder block under Sensing menu and drag into the print block as shown. When you place the range finder block where the "_" block is, the "_" block will be displaced. That's fine, go ahead and put the "_" block in the trash can.
Now change the pin input to the GPIO pin that we connected the yellow wire to: GP22.
Check out the CONSOLE tab at the bottom of the workspace. Do you see the readings change as you move your hand in front of the sensor?
After you've experimented with the distance sensor, click STOP and then click NEXT.
Basically, if an object comes within the range of our Ultrasonic Range Finder, let's set off an alarm. So how do we do that? Well, we want to set up a conditional statement, with an alarm set off if an object is detected. We can do this using an if _ do block under the Logic menu. Go ahead and drag one of those into the repeat forever block. Use the one with the inequality already in it:
So let's set up the if statement to test if there is an object within a certain range. Duplicate the range finder block and drag the new one to the left side of the _ = _ block. Then set the inequality to ≤ (less than or equal to).
Drag a number value block from the Values menu to the left side of the _ = _ block. Change the value from 0 to 50.
Finally, change the wait time of the repeat forever block to 1 second:
Let's set off an alarm!
There are many different sounds you could play in your program if the condition is met. Do you want a loud alarm to wake you up? Or a funny sound to make you laugh?
Whichever it is, drag a play sounds block from the Sounds menu and place it in the do statement in the if block. Then, pick which sound you want the program to play! You can find lots of different sounds in the Sounds menu.
You may change the sounds at any time by changing the blocks in your program:
Position the sensor and move your hand towards and then away from the sensor. Check the CONSOLE. Do you see the measurements printing?
Check the PYTHON tab. Do you see any text that looks like what the blocks you dragged and dropped above?
If you don't hear any sounds - check your computer's volume. The sound blocks actually send a command back to your computer that tells it to play a sound!
Click NEXT.
Replace the print block in your program with a graph number block from the Actions menu:
What is the graph showing you?
Click STOP and then click NEXT when you've figured it out!
How close can someone come to your sensor without tripping the alarm? How would the alarm change if you used units of inches?
Try it, and when you're finished, click NEXT.
Can you think of any other places where you might use a range finder like this?
Blueprint
OPEN IN BLUEPRINT VIEWER DOWNLOAD
Have an different version? Look for more information on the Support Page.
Troubleshooting Tips
-
Is the Range Finder not measuring distance?
- Pay extra attention to the wiring diagram for this one.
- Verify all your electrical connections, including that your Pico is connected to your computer.
- It is common for the connections to be off by 1 or 2 holes. We recommend going through each build to ensure the connections are correct.
- Don’t worry! One of the wires (NC) does not need to be connected.
-
How does the Distance Sensor work?
- The Distance Sensor emits sound waves and measures the time it takes for them to bounce back. Measuring this determines the distance.
Our customer support specialists are on hand to ensure your implementation of Piper runs seamlessly.
View Support Docs or Contact Support
EXPANDED RESOURCES
Term Glossary
Engineer Someone who designs, builds, or fixes things to solve problems.
Sensor A device that produces an output signal to sense a physical phenomenon.
Conditional Statement A coding instruction used to compare values and express and make decisions, like an if-then statement.
List A sequence of several values grouped under a single name in code. A group of items or components. A tool used to store multiple pieces of information at once.
Standards Alignment
- CA 3-5.CS.1 Describe how computing devices connect to other components to form a system. (P7.2)
- 3-5.AP.18 Perform different roles when collaborating with peers during the design, implementation, and review stages of program development.

