Site Tools


Sidebar

Content

pub:pir_sensor

This is an old revision of the document!


PIR Sensor

what's PIR Sensor

The PIR (passive Infrared) sensor can detect the Infrared rays emitted by human or animals and then output On/Off signals. Traditional pyroelectric PIR sensor needs pyroelectric Infrared probe, special chip and complex circuits to make the effect. In this case, the sensor is a large one with complicated circuits and comparatively less credible. But this Infrared pyroelectric motion sensor adopts the digital integration of the probe, so it boasts high credibility, low power consumption and simple outside circuit with a small size. It can be applied to any cases in detecting moving human or animals.


Pin definition:

S Digital output
3.3V 3.3V
- GND



Microbit

1 * microbit
1 * microbit expansion board
1 * USB cable
1 * PIR Sensor
- Several Jumper wires

connection diagram:


Code:
makecode:


MU python:

#-----------------------------------------------------------
# File name   : PIR_sensor.py
# Description : The use of the PIR_sensor.
# Author      : jason
# E-mail      : jason@adeept.com
# Website     : www.adeept.com
# Date        : 2018/01/10
#-----------------------------------------------------------
from microbit import *
 
 
sleep(5000)
 
while True: 
    reading = pin0.read_digital()
    if reading == 1:
        pin1.write_digital(0)
        sleep(3000)
        pin1.write_digital(1)
    sleep(20)



Arduino

Components
- 1 * Adeept Arduino UNO R3 Board
- 1 * PIR Sensor Module
- 1 * USB Cable
- 1 * 3-Pin Wires

Build the circuit


Adeept UNO R3 Board PIR Sensor Module
D8 S
3.3V 3.3V
GND -

Code:

/***********************************************************
File name: _12_PIRSensorModule.ino
Description: The motion has been detected by PIR sensor module,
             and displayed in the serial monitor 
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2019/01/11 
***********************************************************/
int PIRPin=8;           //Set the digital 8 to PIR
void setup() {
  pinMode( PIRPin,INPUT);//initialize the  PIR pin as input
  Serial.begin(9600);   //opens serial port, sets data rate to 9600 bps
}
 
void loop() {
  if(digitalRead(PIRPin)==LOW)
  {
     Serial.println("No invasion"); //send data to the serial monitor
  }else
  {
     Serial.println("Invasion");    //send data to the serial monitor
  }
  delay(1000);                       //delay 1s
}

Compile and download the sketch to the UNO R3 board.



Open the Serial Monitor in Arduino IDE. When the PIR module detects human movement, “Invasion!” will be displayed on the window; otherwise, “No invasion”



Raspberry pi

Components
- 1 * Raspberry Pi
- 1 * GPIO Extension Board
- 1 * 40-Pin GPIO Cable
- 1 * Breadboard
- 1 * PIR Sensor Module
- 1 * 3-Pin Wires

Build the circuit


Code:
C code:

/*
* File name   : Pir.c
* Description : Intrusion detection
* Website     : www.adeept.com
* E-mail      : support@adeept.com
* Author      : Jason
* Date        : 2019/01/10
*/
#include <wiringPi.h>
#include <stdio.h>
 
#define Pir_S  0
 
void myISR(void)
{
	printf("Someone invasion !!!\n");	
}
 
int main(void)
{
	if(wiringPiSetup() == -1){
		printf("setup wiringPi failed !\n");
		return -1; 
	}
 
	pinMode(Pir_S, INPUT);
 
	pullUpDnControl(Pir_S, PUD_DOWN);
 
	if(wiringPiISR(Pir_S, INT_EDGE_RISING, myISR) < 0){
		printf("ISR setup error!\n");
		return -1;
	}
 
	while(1){
 
	}
 
	return 0;
}

Python code:

#!/usr/bin/env python
 
import RPi.GPIO as GPIO
import time
 
PIR_OUT_PIN = 11    # pin11
 
def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(PIR_OUT_PIN, GPIO.IN)    # Set BtnPin's mode is input
 
def loop():
	while True:
		if GPIO.input(PIR_OUT_PIN) == GPIO.LOW:
			print '...Movement not detected!'
		else:
			print 'Movement detected!...'
 
def destroy():
	GPIO.cleanup()                     # Release resource
 
if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()

For C language users:

Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_C_Code/16_PIR/pir.c)

Step 3: Compile

$ sudo gcc pir.c -o pir -lwiringPi

Step 4: Run

$ sudo ./pir

For Python users:

Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_Python_Code/16_PIR.py)

Step 3: Run

$ sudo python 16_PIR.py

When the PIR module detects human movement, “ Someone invasion !!!” will be displayed on the terminal.





Reference material:http://www.adeept.com/learn/
Video:http://www.adeept.com/video/

pub/pir_sensor.1547201215.txt.gz · Last modified: 2019/01/11 18:06 by Adeept