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

HOW to Make a Motion Alarm with Kidspico

By r December 13th, 2024 495 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 4.4Motion Alarm.py in Files and click 1407.

'''
 * Filename    : Smart Safe Home: Motion Alarm
 * Thonny      : Thonny 4.1.4
 * Auther      : http//www.keyestudio.com
'''
from machine import Pin, PWM
from AK8975C import ak8975c
import neopixel
import time

trumpet = PWM(Pin(2))  # power amplifier

scl = Pin(5) 
sda = Pin(4)
bus = 0
Triaxial = ak8975c(bus, scl, sda)

led = Pin(14, Pin.OUT)  # 6812RGB module
pixels = neopixel.NeoPixel(led, 4) 
brightness=5

while True:
    Triaxial.measure()  # measure data once
    print('x:',Triaxial.X,'y:',Triaxial.Y,'z:',Triaxial.Z)  # print the geomagnetic force on axis X Y Z
    if Triaxial.AK8975_GET_AZIMUTH(Triaxial.X, Triaxial.Y) == True:  # Print the course angle value(azimuth value) only when the angle can be calculated.
        degree = Triaxial.angle_val
        print('degree:', degree,'°')

        if degree > 0.0 and degree < 45.0:
            trumpet.duty_u16(0)
            for i in range(4):
                pixels[i] = (0,0,0)
                pixels.write()            
        else:
            for i in range(4):
                pixels[i] = (255,255,0)
                pixels.write()
            trumpet.freq(880)
            trumpet.duty_u16(2000)
            time.sleep(0.3)

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

Code structure:

  1. Initialization. Set the pins of the power amplifier, 6812 RGB module and IIC interface.

  2. Loop.

    Print the calculated Course Angle.

    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.

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 Kidspico
Previous
HOW to Make a Track Alarm with Kidspico
Read More
How to Program Relay Module on Kidsuno
Next
How to Program Relay Module on Kidsuno
Read More
Message Us