In this lesson, we'll introduce how to use Raspberry Pi to drive PIR motion Sensor(Human Body Pyroelectric Infrared Sensor) and make an alarm system.
1 * Raspberry Pi
1 * Breadboard
1 * buzzer
1 * PIR motion sensor
Some jumper wires
Software Preparation:Note: In this lesson, we remotely control raspberry pi via PuTTy on PC. To learn how to config raspberry pi, please visit lesson 1: getting started with raspberry pi.
Work PrinciplePassive infrared sensor (PIR motion sensor) is an electronic sensor that detects changes in the amount of infrared (IR) radiating from objects within certain distance. The sensor converts the incoming infrared radiation into voltage signal.
The PIR Sensor detection range is 8-12um and human body temperature radiates about 10um IR. So PIR Sensor is often used to detect human motion in alarm system.
The PIR sensor in this projects has 3 pins (VCC, OUT, GND), 2 potentiometers (one for sensitivity adjustment and another for time delay adjustment) as showed in following picture.
Wiring up the PIR sensor and a buzzer to the Pi as following connection graph.
Sample codeFor C language user, please take steps as followed:1) Download the C sample code by typing following terminal command:
cd ~sudo wget http://osoyoo.com/driver/pi3_start_learning_kit_lesson_16/pirsensor.cNote: If you want to customize the sample code file , you can use nano editor to edit source code by typing following command in terminal:
sudo nano pirsensor.c2)Compile codegcc -Wall -o pirsensor pirsensor.c -lwiringPi4) Run the programsudo ./pirsensor5) Running result
Once the program starts running, the terminal will show print message as per program. If you move in front of the Raspberry PI PIR sensor, then it should make the piezo buzzer beep. When you go away, then the buzzer stop beeping.
C language sample Code Analysis
for python user, please take steps as followed:
1) Download the Python sample code by typing following command:
cd ~sudo wget http://osoyoo.com/driver/pi3_start_learning_kit_lesson_16/pirsensor.py2)Run the programsudo python ./pirsensor.py3) Running result
Once program starts running, the terminal will show print message. If you move in front of the Raspberry PI PIR sensor, it should make the piezo buzzer beep. When you go away, the buzzer will stop beeping.
Python Sample Code Analysis:
import RPi.GPIO as GPIO
# set BCM_GPIO 17(wPi#0) as PIR pin
PIRPin = 17
# set BCM_GPIO 18(wPi#1) as buzzer pin
BuzzerPin = 18
#print message at the begining ---custom function
print ('| Alarm |')
print ('| ----------------------- |')
print ('| PIR connect to GPIO0 |')
print ('| |')
print ('| Buzzer connect to GPIO1 |')
print ('| ------------------------ |')
print ('| |')
print ('| OSOYOO|')
print ('Program is running...')
print ('Please press Ctrl+C to end the program...')
#setup function for some setup---custom function
#set the gpio modes to BCM numbering
#set BuzzerPin's mode to output,and initial level to HIGH(3.3V)
#read Sw520dPin's level
print ('* alarm! *')
print ('= Not alarm... =')
#define a destroy function for clean up everything after the script finished
#turn off buzzer
# if run this script directly ,do:
if __name__ == '__main__':
#when 'Ctrl+C' is pressed,child program destroy() will be executed.
The HC-06 is a class 2 slave Bluetooth module designed for transparent wireless serial communication. Once it is paired to a master Bluetooth device such as PC, smart phones and tablet, its operation becomes transparent to the user.