X
Home > Blog > STEM for Arduino > HOW to Make a Motion Alarm with KidsIOT

HOW to Make a Motion Alarm with KidsIOT

By r December 9th, 2024 446 views
Motion alarm has become an important anti-theft device to protect the items in the smart safe house.
In this experiment, we adopts a three-axis magnetic sensor, a power amplifier and a 6812 RGB module to form a motion alarm. When a certain item moves to a specific angle, the alarm will be triggered: the amplifier sounds and the 6812 RGB emits lights.

Flow

4401

Assembly

line1

Required Parts

44_00

line1

Step 1

44_01

line1

Step 2

44_02

line1

Step 3

44_03

line1

Step 4

44_04

line1

Step 5

44_05

line1

Step 6

44_06

line1

Step 7

44_07

line1

Completed

44_08

line1

Wiring Diagram

4402

Test Code

Open KidsBlock and connect the board to your computer. Click File –> Load from your computer.

3111

Choose D:\Code\2.Code_kidsIOT to open 4.4Motion Alarm.sb3 file.

4403

Click Unconnectedto connect to port and then 2210.

Explanations

5top

Conceive:

Set a threshold angle range to determine whether objects have been moved.

Place the object and ensure its Course Angle is within the range, which means the object is not moved. Once the Course Angle value is greater than the threshold, it indicates that the item has been moved, so the amplifier alarms and the 6812 RGB lights up.

line2

Conceive:

  1. Initialization

    Initialization: initialize AK8975 three-axis magnetic sensor; set the pin and the pixel number of the 6812 RGB module and set the brightness to 5; initialize serial port; define a float variable angle to store the read Course Angle.

    4404

line3

  1. Main Code

    Assign the calculated Course Angle to the variable angle and print it on the serial monitor.

    Determine whether the angle is within 0° ~ 45°(this range can be modified according to needs).

    • 0° < angle < 45°: the amplifier stay quiet and LED keeps off.

    • angle is not within 0° ~ 45°: the object is moved, so the amplifier alarms and the LED lights up in yellow.

    4405


    Build blocks:

    ① Add code blocks to the editing area and build as follows:

    4406

    Read the Course Angle of the AK8975 three-axis magnetic sensor and assign it to variable angle.

    ② Add code blocks to the editing area and build as follows:

    4407

    9top

    greater determines whether the left value is greater than the right one. If yes, output True.

    less determines whether the left value is smaller than the right one. If yes, output True.

    and determines whether the two conditions are both satisfied. If yes, output True.

    9bottom

    if else block determines whether the object is moved.

    Conditions: 4408 sets the range to 0 ~ 45. When the object is moved, the angle exceeds this range.

    So this block determines whether the variable angle is within 0 ~ 45.

    • If yes, execute codes in if to turn off all pixels.

    • If not, execute codes in else to light up all pixels in yellow, and the amplifier alarms.

5bottom

Test Result

4top

After uploading code, place the device and maintain the Course Angle within 0° ~ 45°. In the initial state, the amplifier does not emit sound and the pixels are off. Move this device, and the angle will exceed the range of 0° ~ 45°, so the amplifier alarms and the pixels are on in yellow.

4409

4bottom

FAQ
10top
Q: After uploading the code, the amplifier alarms no matter how we move the device?
A: You may scale up the standard range according to needs.
10bottom
HOW to Make a Track Alarm with KidsIOT
Previous
HOW to Make a Track Alarm with KidsIOT
Read More
How to Program Relay Module on Kidsuno
Next
How to Program Relay Module on Kidsuno
Read More
Message Us