

Open 4.2Invasion alarm.py in Files and click
.
'''
* Filename : Smart Safe Home: Invasion Alarm
* Thonny : Thonny 4.1.4
* Auther : http//www.keyestudio.com
'''
from machine import Pin, PWM
import time
PIR = Pin(3, Pin.IN)
led = Pin(11, Pin.OUT)
trumpet = PWM(Pin(2))
while True:
PIR_value = PIR.value()
if PIR_value == 1:
for i in range(6):
trumpet.freq(880)
trumpet.duty_u16(2000)
led.on()
time.sleep(0.5)
trumpet.freq(523)
trumpet.duty_u16(1000)
led.off()
time.sleep(0.5)
else:
trumpet.duty_u16(0)
led.off()

Conceive:
When the sensor detects a human motion (an invasion), the LED flashes and the amplifier alarms. If it detects nothing, LED will go off and the amplifier will not emit sound.

Code structure:
Initialization.
Set the pins of the PIR motion sensor, white LED module and the power amplifier.
Loop.
Read the power level of the board input from the PIR motion sensor.
Determine whether the power level is 1(indicates an invasion).
If yes, the LED flashes and the amplifier alarms.
If not, LED will go off and the amplifier will not emit sound.


After uploading code, when the sensor detects a human motion (an invasion), the LED flashes and the amplifier alarms.




