The Anti-Theft Alarm

Let’s teach our programs to see and react to the world! This mission introduces you to the power of sensors and conditional logic. You’ll learn how to use a simple but powerful ‘IF this happens, THEN do that’ command to build a smart anti-theft alarm that triggers when it detects motion. Get ready to create your first intelligent system!

Conditionals; If-then; Main loop; Sensors; Motion sensor; Camera

The Anti-Theft Alarm illustration

What will we do?

Are you a teacher?

Courses

  • Grades 3-12

Materials

  • Cell phone, tablet, or computer
  • Internet connection

Educational Objectives

  • Understand the concept of “IF-THEN” conditionals.
  • Create a technological object (prototype) using a device.
  • Identify relationships between technology and the surrounding world.
  • Evaluate personal work and the work of others.
  • Engage in discussions and reflect on improvement ideas.

Start (10 minutes) - The Logic of an Alarm

  1. Welcome students and introduce the day’s activity: “Today, we will learn how to prototype a motion-detecting alarm.”
  2. Ask the class: “Have you ever seen an automatic door or a security light? How do you think they know you’re there?”
  3. After discussing their ideas about sensors, explain that we’ll use their phone’s camera as a motion sensor. But how does a program decide when to sound the alarm? This is the perfect moment to introduce conditional logic—the ‘IF/THEN’ statement.
  4. Finally, explain that the program needs to check for motion constantly, not just once, which requires a Main Loop to run the check over and over.

Have you ever heard of motion sensors?

Motion sensors are smart devices that can detect when someone or something moves near them.

Some, like those in automatic doors, work with infrared (a type of light we can’t see) or ultrasound (a sound we can’t hear). They sense changes when their signals bounce off a moving object.

Using a Camera as a Motion Sensor

There are also motion sensors based on cameras, which is what we will use in this activity. A program can watch the video feed from a camera and detect changes between frames. If a large part of the image suddenly changes, the program knows that something is moving.

IF There’s Motion, THEN Sound the Alarm

To make our program smart, we need to teach it how to make decisions. The simplest way to do this is with a conditional statement: IF a condition is true, THEN an action happens. This “IF… THEN…” structure is a core building block of all programming, from simple games to complex artificial intelligence.

A Practical Example

Let’s see how to use a conditional to activate our alarm. We’ll tell the program: IF the amount of motion detected by the camera is greater than a certain number (like 40), THEN play the alarm sound. But how do we make the program check for motion continuously? We can’t just check once! This is where a main loop is essential. By placing our ‘IF motion’ check inside a ‘repeat forever’ loop, we create a system that is always watching.

Development (20-30 minutes) - Building the Security System

  1. Now that they understand the logic of “IF motion is detected, THEN sound the alarm,” it’s time to build the security system.
  2. Lead the students through the instructions for adding the components and constructing the conditional code, as detailed in the hands-on section below. Make sure they understand why the IF block must be placed inside the main loop to be effective.

Closure (5-10 minutes) - Security Debrief

  1. Once the alarms are working, it’s time for a security expert’s debrief. A real security expert always thinks about the weaknesses of their system.
  2. Use the final section to guide a discussion about the limitations of their camera-based alarm. This encourages them to think critically about how technology works in the real world and how it could be improved.

Reflect

You’ve built a security system. Now, think like a security expert.

  • What would happen if a thief entered at night with all the lights off? Would a camera-based sensor still work?
  • How reliable is this alarm? What might accidentally trigger a false alarm (like a pet running by)?

Have you ever heard of motion sensors?

Have you ever heard of motion sensors?

Motion sensors are smart devices that can detect when someone or something moves near them.

Some, like those in automatic doors, work with infrared (a type of light we can’t see) or ultrasound (a sound we can’t hear). They sense changes when their signals bounce off a moving object.

Using a Camera as a Motion Sensor

Using a Camera as a Motion Sensor

There are also motion sensors based on cameras, which is what we will use in this activity. A program can watch the video feed from a camera and detect changes between frames. If a large part of the image suddenly changes, the program knows that something is moving.

IF There’s Motion, THEN Sound the Alarm

IF There’s Motion, THEN Sound the Alarm

To make our program smart, we need to teach it how to make decisions. The simplest way to do this is with a conditional statement: IF a condition is true, THEN an action happens. This “IF… THEN…” structure is a core building block of all programming, from simple games to complex artificial intelligence.

A Practical Example

A Practical Example

Let’s see how to use a conditional to activate our alarm. We’ll tell the program: IF the amount of motion detected by the camera is greater than a certain number (like 40), THEN play the alarm sound. But how do we make the program check for motion continuously? We can’t just check once! This is where a main loop is essential. By placing our ‘IF motion’ check inside a ‘repeat forever’ loop, we create a system that is always watching.

Let’s create our anti-theft alarm!

Let’s build the prototype for our alarm.

First, we need to add two components: one to (1) detect motion and another to (2) play the alarm sound.

1

Add the component CameraMotion which detects motion through the phone’s camera.

2

Add the component AudioPlayer to play the sound of an alarm.

3

Remember that if you don’t have a smartphone to scan the ▣ QR codes, you can click on Open in this window.

We are ready to start prototyping!

Suggestion: In Protobject, you can add more components on the same smartphone by pressing the SCAN button as many times as you need.

Media illustration

Code Composition

Click on the question mark icon to open the comments that explain the code. Conditional blocks are found in the Logic category.

Reflect

You've built a security system. Now, think like a security expert.

  • What would happen if a thief entered at night with all the lights off? Would a camera-based sensor still work?
  • How reliable is this alarm? What might accidentally trigger a false alarm (like a pet running by)?

Hint: Thinking about a system’s weaknesses is the first step to making it stronger. Our sensor relies on light and images to work!