Overview

本文主要介绍如何用树莓派实现简易防人体入侵安防系统,树莓派周期性检测人体红外检测模块输出的高低电平,当有人入侵时候蜂鸣器报警.

DSC_5470

Experimental Parts

PI
Pi3 x1
Digital-Motion-Sensor
人体红外感性模块 x1
18
有源蜂鸣器 x1
19
杜邦线 x3

Circuit Graph
硬件连线很简单,如图。
Untitled Sketch_buzzer

在连接电路的时候特别注意不要把电源正负极接反,否则会烧毁Pi和传感器模块,上图中蜂鸣器模块引脚顺序可能跟实物引脚顺序不一样,在接线的时候需要对照实物连线。

其中蜂鸣器是低电平有效的。

下面给出人体红外检测模块、继电器模块的原理图。

Motion sensor Schematic

schematic

Motion sensor Interface Layout

The two potentiometer can adjust delay time and sensitivity, as the follow picture:

adjust

Software

1)在/home/pi路径下用nano新建一个motionsensor-test.py文件

sudo nano motionsensor-test.py

2)向motionsensor-test.py中写入代码

import RPi.GPIO as GPIO
import time

M_pin = 18 #select the pin for motionsensor
B_pin = 26 #select the pin for buzzer

def init():
         GPIO.setwarnings(False)
         GPIO.setmode(GPIO.BCM)
         GPIO.setup(M_pin,GPIO.IN)
         GPIO.setup(B_pin,GPIO.OUT)
         pass

def buzzer():
         while GPIO.input(M_pin):
                  GPIO.output(B_pin,GPIO.LOW)
                  time.sleep(0.5)
                  GPIO.output(B_pin,GPIO.HIGH)
                  time.sleep(0.5)

def detct():
         for i in range(101):
                  if GPIO.input(M_pin):
                           print "Someone is closing!"
                           buzzer()
                  else:
                           GPIO.output(B_pin,GPIO.HIGH)
                           print "Nobody!"
                  time.sleep(2)

time.sleep(5)
init()
detct()

GPIO.cleanup()

写完代码后,键盘输入Ctrl  X根据提示输入Y保存退出

示例代码可以通过下面命令下载得到

sudo sudo wget http://osoyoo.com/driver/motionsensor-test.py

3)运行脚本

sudo python ./motionsensor-test.py

屏幕上打印出了提示信息,检测到人体移动的时候,蜂鸣器响起。

2017-03-23-064257_1824x984_scrot