Overview

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.

Hardware Preparation:

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 Principle

Passive 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.

Hardware Setup

Wiring up the PIR sensor and a buzzer to the Pi as following connection graph.

Sample code

For 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.c

Note: 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.c

2)Compile code
gcc  -Wall  -o pirsensor  pirsensor.c  -lwiringPi

4) Run the program
sudo  ./pirsensor

5) 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

 
 
  1. #include < wiringPi.h>
  2. #include < stdio.h>
  3. #include < stdlib.h>
  4. #define BuzzerPin 1 //output buzzer signal to wiringPi pin#1(BCM#18)
  5. #define PIRPin 0 //input PIR sensor signalfrom wiringPi pin#0(BCM#17)
  6. int main(void)
  7. {
  8. // When initialize wiring failed, print messageto screen
  9. if(wiringPiSetup() == -1){
  10. printf("setup wiringPi failed !");
  11. exit(1);
  12. }
  13. pinMode(BuzzerPin, OUTPUT);
  14. pinMode(PIRPin,INPUT);
  15. printf("\n");
  16. printf("========================================\n");
  17. printf("| Alarm |\n");
  18. printf("| ------------------------------ |\n");
  19. printf("| PIR connect to wPi#0 |\n");
  20. printf("| |\n");
  21. printf("| Buzzer connect to wPi#1 |\n");
  22. printf("| |\n");
  23. printf("| OSOYOO|\n");
  24. printf("========================================\n");
  25. printf("\n");
  26. while(1){
  27. if(!(digitalRead(PIRPin))){
  28. digitalWrite(BuzzerPin, HIGH);
  29. printf("\n");
  30. printf("-------------------|\n");
  31. printf("| no alarm... |\n");
  32. printf("-------------------|\n");
  33. delay(1000);
  34. }
  35. else{
  36. digitalWrite(BuzzerPin, LOW);
  37. delay(500);
  38. printf("\n");
  39. printf("===================|\n");
  40. printf("| alarm... |\n");
  41. printf("===================|\n");
  42. }
  43. }
  44. return 0;
  45. }

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.py

2)Run the program
sudo  python  ./pirsensor.py

3) 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:

 
 
  1. import RPi.GPIO as GPIO
  2. import time
  3. # set BCM_GPIO 17(wPi#0) as PIR pin
  4. PIRPin = 17
  5. # set BCM_GPIO 18(wPi#1) as buzzer pin
  6. BuzzerPin = 18
  7. #print message at the begining ---custom function
  8. def print_message():
  9. print ('==================================')
  10. print ('| Alarm |')
  11. print ('| ----------------------- |')
  12. print ('| PIR connect to GPIO0 |')
  13. print ('| |')
  14. print ('| Buzzer connect to GPIO1 |')
  15. print ('| ------------------------ |')
  16. print ('| |')
  17. print ('| OSOYOO|')
  18. print ('==================================\n')
  19. print ('Program is running...')
  20. print ('Please press Ctrl+C to end the program...')
  21. #setup function for some setup---custom function
  22. def setup():
  23. GPIO.setwarnings(False)
  24. #set the gpio modes to BCM numbering
  25. GPIO.setmode(GPIO.BCM)
  26. #set BuzzerPin's mode to output,and initial level to HIGH(3.3V)
  27. GPIO.setup(BuzzerPin,GPIO.OUT,initial=GPIO.HIGH)
  28. GPIO.setup(PIRPin,GPIO.IN)
  29. #main function
  30. def main():
  31. #print info
  32. print_message()
  33. while True:
  34. #read Sw520dPin's level
  35. if(GPIO.input(PIRPin)!=0):
  36. GPIO.output(BuzzerPin,GPIO.LOW)
  37. #time.sleep(0.5)
  38. print ('********************')
  39. print ('* alarm! *')
  40. print ('********************')
  41. print ('\n')
  42. time.sleep(1)
  43. else:
  44. GPIO.output(BuzzerPin,GPIO.HIGH)
  45. print ('====================')
  46. print ('= Not alarm... =')
  47. print ('====================')
  48. print ('\n')
  49. time.sleep(1)
  50. #define a destroy function for clean up everything after the script finished
  51. def destroy():
  52. #turn off buzzer
  53. GPIO.output(BuzzerPin,GPIO.HIGH)
  54. #release resource
  55. GPIO.cleanup()
  56. #
  57. # if run this script directly ,do:
  58. if __name__ == '__main__':
  59. setup()
  60. try:
  61. main()
  62. #when 'Ctrl+C' is pressed,child program destroy() will be executed.
  63. except KeyboardInterrupt:
  64. destroy()
  65. pass

Leave a Reply

WordPress spam blocked by CleanTalk.
Bombax Theme designed by itx
"