Overview of PIR Motion Sensor
PIR sensor detects infrared heat radiations. It can be used to detect the presence of living objects that emit infrared heat radiation.
The PIR sensor is split into two slots. The two slots are connected to a differential amplifier.
Whenever a stationary object is in front of the sensor, the two slots receive the same amount of radiation and the output is zero.
Whenever a moving object is in front of the sensor, one of the slots receives more radiation than the other slot. This makes the output swing high or low.
This change in output voltage is the result of the detection of motion.
For more information on the PIR sensor and how to use it, refer to the topic PIR sensor in the sensors and modules section.
Connection Diagram of PIR Motion Sensor to PIC18F4550
Note:
- PIR sensor: Never keep PIR Sensor close to the Wi-Fi antenna, ESP32, or NodeMCU.
- PIR (Passive Infrared) sensor close to a WiFi antenna impacts the sensor's performance.
- PIR sensors detect changes in infrared radiation for motion detection.
- WiFi signals emit electromagnetic radiation that can interfere with the PIR sensor. Which causes false detection.
- So always keep the PIR sensor and WiFi antenna as far apart as possible.
- Also, you can try to shield the PIR sensor from the WiFi signal. This can be done by using metal shields or Faraday cages around the PIR sensor.
Detect Motion Using PIR Sensor with PIC18F4550
Let’s design a small application in which LED will turn ON when motion is detected.
To do this, interface the PIR motion sensor with PIC18F4550.
As shown in the Circuit Diagram, the output pin of the PIR motion Sensor connected to the PORTA.0 pin.
If this pin goes HIGH, then the motion is detected and LED will turn ON.
According to the mode of Operation, If LOW on this pin detected, which means either motion is absent or Period of Trigger is over and will Turn OFF the LED.
Here we configure the module in repeatable trigger mode.
Note: After powering the module it needs about 30-50 secs to warm-up in order to function properly.
PIR Motion Sensor Code for PIC18F4550
/*
* PIR Motion sensor interface with PIC18F4550
* http://www.electronicwings.com
*/
#include <pic18f4550.h>
#include "Configuration_Header_File.h"
#define Motion_detection PORTAbits.RA0 /* Read PIR sensor's data on this pin */
#define PORT_Dir TRISAbits.RA0 /* define for setting direction */
#define LED LATD0 /* connect LED to the PORT pin */
#define LED_Dir TRISDbits.RD0 /* define for setting direction */
void MSdelay(unsigned int val);
void main(void)
{
ADCON1=0x0F; /* this makes all pins as a digital I/O pins */
PORT_Dir = 1; /* set as input port */
LED_Dir = 0; /* set as output port */
LED = 0; /* initially turned OFF LED */
OSCCON = 0x72;
while(1)
{
while(Motion_detection)
LED = 1; /* LED turn ON if any Human motion is detected */
LED = 0; /* LED turn OFF */
}
}
void MSdelay(unsigned int val)
{
unsigned int i,j;
for(i=0;i<val;i++)
for(j=0;j<165;j++); /*This count Provide delay of 1 ms for 8MHz Frequency */
}
Video of PIR Motion Detect using PIC18F4550
Components Used |
||
---|---|---|
PIR Sensor PIR motion sensors sense the Infrared signal radiated from moving objects including human or animal body. It is generally used to detect the presence of human or animal motions. |
X 1 | |
LED 5mm LED 5mm |
X 1 | |
PIC18f4550 PIC18f4550 |
X 1 | |
PICKit 4 MPLAB PICKit 4 MPLAB |
X 1 | |
Breadboard Breadboard |
X 1 |