How to use a PIR motion sensor with Raspberry Pi 4

Raspberry PI 4 Wiring with PIR Sensor and Buzzer

Passive infrared sensors (PIR) allow to detect the movement of people or any body that emits infrared thermal radiation. They are called passive due to the fact that they do not emit any type of wave to detect movement. Instead they use certain crystals capable of generating a voltage in the presence of a heat source.

Due to their low cost, size, and low power consumption, PIR sensors are widely used in home security systems. In this tutorial we show you how to build a circuit that emits a buzz when the Raspberry detects movement.

Hardware required:

  • 1 Raspberry Pi 4
  • 1 Breadboard
  • 1 Buzzer or Active Buzzer
  • 1 PIR motion sensor
  • Cables

Circuit Wiring:

Identify the 3 pins of the PIR sensor: VCC, OUT, and GND. If you can’t tell them apart, carefully open the Fresnel lens, the white plastic cover.

PIR sensor

Then, connect each of the pins directly to the Raspberry: the VCC pin to the VCC, the GND to the GND, the OUT pin to pin 12. It is through the OUT pin that the sensor sends a signal when it detects movement.

Next, place the buzzer on the breadboard. Connect the positive pole to pin 26 and the other to any of the GND pins.

Python code to detect motion with a PIR sensor and emit buzz

import RPi.GPIO as GPIO
import time
GPIO.setup(pir, GPIO.IN)
GPIO.setup(zum, GPIO.OUT)
while True:
      if x ==0:
          print(“No motion detected”)
          GPIO.output(zum, 0)
      if x==1:
          print(“Motion detected”)

To run the code, go to the Raspberry Pi OS desktop, open the Raspberry menu, and select Programming > mu. Then copy and paste the code. Click Save and save the file with a .py extension. Finally, click on Run.


Leave a Comment

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

Scroll to Top