Pantech.AI

How to Interface PIR Motion Sensor Interface with PIC18F4550

Overview of PIR Motion Sensor

The PIR (Passive Infrared) sensor detects infrared radiation, primarily emitted by living objects. This makes it ideal for detecting the presence of people, animals, or any warm-blooded beings.

The sensor is divided into two segments, which are connected to a differential amplifier. When a stationary object is in front of the sensor, both segments detect the same level of infrared radiation, resulting in a zero output signal. However, when a moving object passes by, one segment registers more radiation than the other. This difference in radiation levels causes the output to fluctuate, either high or low.

This change in output voltage indicates detected motion.

For further details on the PIR sensor and its applications, refer to the “PIR Sensor” section under sensors and modules.

PIR Motion Sensor

Connection Diagram of PIR Motion Sensor to PIC18F4550

PIR Motion Sensor Interfacing with PIC18F4550

Note:

Avoid placing the PIR (Passive Infrared) sensor near Wi-Fi antennas, ESP32, or NodeMCU, as this can affect its accuracy. PIR sensors detect motion by sensing changes in infrared radiation, but the electromagnetic radiation from Wi-Fi signals can interfere, leading to false detections.

To ensure reliable performance, keep the PIR sensor and Wi-Fi antenna as far apart as possible. Alternatively, you can shield the PIR sensor from Wi-Fi interference by using a metal shield or a Faraday cage around it.

Detect Motion Using PIR Sensor with PIC18F4550

Let’s create a simple application where an LED lights up when motion is detected.

To achieve this, we’ll connect the PIR motion sensor to the PIC18F4550 microcontroller. In the setup, the output pin of the PIR sensor is linked to the PORTA.0 pin on the microcontroller.

In operation, if PORTA.0 reads HIGH, motion has been detected, and the LED will turn ON. Conversely, if the pin reads LOW, it indicates either no motion is present or the trigger period has ended, so the LED will turn OFF.

We’ll configure the PIR module in repeatable trigger mode for continuous detection.

Note: After powering on, the PIR module requires 30-50 seconds to stabilize and function correctly.

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 */
}

Leave a Comment

Your email address will not be published. Required fields are marked *