Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2

In this step‑by‑step guide, you’ll learn how to connect a Passive Infrared (PIR) motion sensor to a Raspberry Pi B+/Model 2 and program the GPIO pins to detect movement and trigger actions. Whether you’re building a home‑automation system, a security alert, or a simple robot, mastering GPIO is essential.

What You’ll Need

  1. Raspberry Pi B+, B, or Model 2 with a microSD card, power supply, keyboard, mouse, and display.
  2. LED (any colour) and a 220 Ω resistor.
  3. PIR motion sensor (5 V output).
  4. Breadboard and jumper wires (male‑male and female‑male).

How GPIO Works on the Pi

The Pi’s GPIO pins are accessible via Python libraries such as RPi.GPIO. Each pin is numbered either by physical pin number (BOARD) or by Broadcom chip pin number (BCM). In this tutorial we’ll use BOARD numbering for clarity.

About the PIR Sensor

A PIR sensor detects infrared radiation emitted by warm bodies. It comprises a Fresnel lens, an IR detector, and control circuitry. When it senses a change in infrared levels, it outputs a 5 V logic HIGH for up to one minute. Typical detection range is 6–7 m.

Step 1: Blink an LED with GPIO Output

First, practice controlling an output pin by blinking an LED. Connect the LED’s anode to pin 3 through a 220 Ω resistor, and the cathode to a ground pin.

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)  # Pin 3 as output

while True:
    GPIO.output(3, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(3, GPIO.LOW)
    time.sleep(1)

Save this as ledblink.py and run it with sudo python3 ledblink.py.

Step 2: Read the PIR Sensor and Respond

Wire the PIR sensor: VCC to 5 V, GND to ground, and OUT to pin 7 (GPIO 4). Now create a script that prints a message when motion is detected.

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)  # PIR output as input

print('Ready – waiting for motion…')

try:
    while True:
        if GPIO.input(7):
            print('Intruder detected!')
            # Add your custom action here (e.g., trigger LED, send alert)
        time.sleep(0.5)
except KeyboardInterrupt:
    print('Exiting…')
finally:
    GPIO.cleanup()

Run with sudo python3 pir_detect.py and watch the console as the sensor reports motion.

Next Steps

From here you can expand the project: blink an LED when motion is detected, log timestamps to a file, or send a notification via MQTT. For beginners, we recommend downloading our free eBook on Raspberry Pi and Arduino to deepen your hardware skills.

References

Manufacturing process

  1. How to Connect a DS1820 One‑Wire Temperature Sensor to a Raspberry Pi Using GPIO
  2. Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
  3. Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
  4. Secure Home Monitoring with Home Assistant on Raspberry Pi: Motion, Alarm, and Video Capture
  5. Control an LED with a PIR Motion Sensor on Raspberry Pi
  6. Raspberry Pi Light Sensor Tutorial – Wire an LDR and Read Light Levels with Python
  7. Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
  8. Raspberry Pi Home Security System: PIR Motion Detection + Camera Email Alerts
  9. How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi
  10. Build a Raspberry Pi Obstacle‑Avoiding Robot – A Beginner’s Guide