PIR Motion Sensor Interface with AVR ATmega16/ATmega32

PIR motion sensor senses the Infrared signal radiated from moving human or animal body. It is generally used to detect the motions of humans or animals.

20.2k views2 min readUpdated May 8, 2023
PIR Motion Sensor Interface with AVR ATmega16/ATmega32

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.

PIR Sensor

 

Connection Diagram of PIR Sensor with ATmega16/32

ATmega Interfacing with PIR Sensor
Interfacing PIR Sensor With AVR ATmega 32

 

 

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 ATmega16/32 Microcontroller

  • Here, we are going to detect motion using a PIR sensor which is interfaced with AVR ATmega16/32.
  • When Motion is Detected (i.e. out pin goes high), the LED will turn ON.

PIR Motion sensor Code for ATmega16/32

/*
 PIR Motion Sensor Interface with AVR ATmega32
 http://www.electronicwings.com
 */

#define F_CPU 8000000UL
#include <avr/io.h>
#define LED_OUTPUT		PORTB
#define PIR_Input		PINC

int main(void)
{
	DDRC = 0x00;	/* Set the PIR port as input port */
	DDRB = 0xff;	/* Set the LED port as output port */

    while(1)
    {
        LED_OUTPUT = PIR_Input;
	}
}

 

Video of Motion Detection using ATmega16/32 Microcontroller

Components Used

Powered byMouser Electronics

Downloads

Comments5

Join the discussion — share a question or tip.

  • MA
    maxprog

    Hi, what software do You use to create Interfacing Diagram? or what are Your suggestions?

  • SR
    sridhar

    One small correction in above code. LED output port direction should be DDRB. After the changes, the above code is working for me. needs to be adjust sensitivity and time delay to get accurate result.

    • LO
      lokeshc

      yes. correct. Sensitivity needs to be adjusted.

  • SR
    sridhar

    i have connected circuit according to the pgrm. But LED is blowing continuously. I think it's not sensing the object. Please help me guys. what could be a problem

    • LO
      lokeshc

      Try to adjust the sensitivity. Also cross-check vcc and gnd connection of pir sensor.