0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Interface PIR Motion Sensor in Embedded C

To interface a PIR motion sensor in Embedded C, connect its output pin to a microcontroller input pin and configure that pin as input. Then, read the pin state in your code to detect motion: a HIGH signal means motion detected, LOW means no motion.
📐

Syntax

To read a PIR sensor in Embedded C, you typically configure a microcontroller pin as input and then read its digital state.

Example syntax parts:

  • pinMode(PIN, INPUT); - sets the pin as input.
  • digitalRead(PIN); - reads the pin state (HIGH or LOW).

In pure Embedded C (without Arduino), you configure the microcontroller's GPIO registers to input mode and read the input data register.

c
/* Example syntax for reading PIR sensor input pin */
// Configure pin as input (pseudo-code)
GPIO_DIR &= ~(1 << PIR_PIN); // Clear bit to set input

// Read pin state
int motion = (GPIO_IN & (1 << PIR_PIN)) ? 1 : 0; // 1 if motion detected, 0 otherwise
💻

Example

This example shows how to interface a PIR sensor connected to pin P1.0 of a microcontroller. It configures the pin as input and continuously checks for motion, turning on an LED on pin P2.0 when motion is detected.

c
#include <stdint.h>
#include <stdbool.h>

#define PIR_PIN 0  // P1.0
#define LED_PIN  0 // P2.0

// Mock registers for GPIO direction and input/output
volatile uint8_t P1DIR = 0xFF; // 1=output, 0=input
volatile uint8_t P1IN = 0x00;  // Input register
volatile uint8_t P2DIR = 0x00;
volatile uint8_t P2OUT = 0x00;

void delay(void) {
    for (volatile int i = 0; i < 100000; i++); // simple delay
}

int main(void) {
    // Set PIR_PIN as input
    P1DIR &= ~(1 << PIR_PIN);
    // Set LED_PIN as output
    P2DIR |= (1 << LED_PIN);

    while (1) {
        // Read PIR sensor
        bool motion_detected = (P1IN & (1 << PIR_PIN)) != 0;

        if (motion_detected) {
            // Turn on LED
            P2OUT |= (1 << LED_PIN);
        } else {
            // Turn off LED
            P2OUT &= ~(1 << LED_PIN);
        }

        delay();
    }
    return 0;
}
Output
No console output; LED on P2.0 turns ON when motion detected, OFF otherwise.
⚠️

Common Pitfalls

Common mistakes when interfacing PIR sensors include:

  • Not configuring the microcontroller pin as input, causing wrong readings.
  • Ignoring the sensor's warm-up time (usually 20-60 seconds) before it gives stable output.
  • Not using pull-down or pull-up resistors if required, leading to floating input signals.
  • Reading the sensor output too fast without delay, causing unstable detection.
c
/* Wrong: Pin configured as output, reading will be incorrect */
P1DIR |= (1 << PIR_PIN); // Incorrect: sets pin as output
int motion = (P1IN & (1 << PIR_PIN)) ? 1 : 0;

/* Correct: Pin configured as input */
P1DIR &= ~(1 << PIR_PIN); // Correct: sets pin as input
int motion = (P1IN & (1 << PIR_PIN)) ? 1 : 0;
📊

Quick Reference

StepDescription
Connect PIR output to microcontroller input pinPhysical wiring of sensor output to MCU pin
Configure MCU pin as inputSet GPIO direction register bit to input mode
Wait sensor warm-up timeAllow 20-60 seconds for sensor stabilization
Read pin stateCheck if pin is HIGH (motion) or LOW (no motion)
Use pull-up/down resistor if neededPrevent floating input signals
Add delay between readsAvoid unstable rapid readings

Key Takeaways

Configure the microcontroller pin connected to PIR sensor output as input before reading.
Wait for the PIR sensor warm-up time to get stable motion detection.
Use pull-up or pull-down resistors if the sensor output line is floating.
Read the sensor output pin state to detect motion: HIGH means motion detected.
Add delays between readings to avoid unstable or noisy signals.