Project 08: Anti-theft Alarm

1. Overview

When the smart anti-theft alarm detects that the anti-theft box has been moved, the speaker on the micro:bit board will alarm and the red LED will flash.

2. Components

Img

Img

Img

micro:bit board *1

micro:bit T-type expansion board *1

micro USB cable *1

Img

Img

Img

red LED *1

220Ω resistor *1

jump wire *2

Img

Img

Img

breadboard *1

battery holder *1
(self-provided AA batteries *2)

alarm card *1

3. Components Knowledge

Accelerometer

Img

The micro:bit board boasts a built-in LSM303AGR acceleration sensor (we called accelerometer) which includes standard, fast, plus and high-speed mode (100 kHz, 400 kHz, 1 MHz and 3.4 MHz) of I2C serial bus interface and SPI serial standard interface for external communication, with resolution of 8/10/12 bits and range of ±2g, ±4g, or ±8g.

When the micro:bit board is at rest or in uniform motion, the accelerometer only detects the acceleration of gravity. If it is slightly swung, the detected acceleration is much less than the that of gravity, so the difference can be ignored. Therefore, we mainly detect the change of gravitational acceleration on x, y, and z axes.

4. Wiring Diagram

Img

The board control pin of LED is P1 (the pin of T-type expansion board is digital 1).

5. Code Flow

Img

6. Test Code

The code file is provided in folder Project 08:Burglar Alarm, file Project-08-Burglar-Alarm.py.

Img

Complete code:

After importing the code, if the buzzer keeps sounding even though the breadboard is not moved; it may be caused by geographical factors. You can modify the threshold in the condition -60 and 50 according to actual conditions.

'''
Function: The accelerometer controls a buzzer and LED to simulate a anti-theft alarm
Compiling IDE: MU 1.2.0
Author: https://docs.keyestudio.com
'''
# import related libraries
from microbit import *
import music

display.show(Image.HAPPY) # LED matrix displays a smile face

while True:
    if accelerometer.get_x()<-60 or accelerometer.get_x()>50: # If the value of the accelerometer in the X direction is less than -60 or greater than 50
       music.play("C4:4")      # speaker plays C4 tone
       pin1.write_digital(1)   # P1 pin value is high, LED on
       sleep(200)
       pin1.write_digital(0)   # P1 pin value is low, LED off
       sleep(200)
       display.show(Image.NO)  # LED matrix shows X
    else:  # or
        display.show(Image.HAPPY) # LED matrix displays a smile face
        pin1.write_digital(0)
        music.reset()             # no tone

7. Test Result

Click “Flash” to load the code to the micro:bit board.

Img

After downloading the code to the board, power on via micro USB cable or external power supply(turn the DIP switch to ON), and press the reset button on the board.

Img

After downloading the code to the board, move the breadboard. If the acceleration value x<-60 or x>50, the speaker on the board alarms and the LED flashes, and the micro:bit LED matrix shows Img. Otherwise, the speaker makes no sound and LED is off, and the micro:bit LED matrix shows Img.

ATTENTION: If the wiring is correct but you cannot see the results, press the reset button on the back of the board.

Img