🐭 Tutorial 9 - PIR Motion Sensor

The PIR motion sensor module is a type of electronic sensor that is used to detect motion in a specific area by sensing the infrared radiation emitted by living beings and objects. “PIR” stands for “Passive Infrared”, which means that the sensor does not emit any energy but only detects the energy emitted by other objects.

The PIR motion sensor module consists of a pyroelectric sensor, a lens, and supporting circuitry. The sensor detects changes in the amount of infrared radiation in its field of view and converts them into an electrical signal, which is then processed by the supporting circuitry.

The lens is designed to focus the infrared radiation onto the sensor and limit its detection range to a specific area, making it ideal for detecting motion in a particular zone. The supporting circuitry is designed to amplify and filter the electrical signal, providing a reliable output that can be used to trigger other devices or systems.

PIR motion sensor modules are commonly used in a variety of applications, such as security systems, lighting control systems, and home automation. They are also used in industrial and commercial settings for occupancy sensing and energy management.

Overall, PIR motion sensor modules are an important and versatile component in electronic circuits and devices, providing reliable motion detection for a wide range of applications.

Components Needed

ComponentQuantity
Raspberry Pi Pico W1
Micro USB Cable1
Breadboard1
WiresSeveral
PIR Module1

Fritzing Diagram

Code

import machine
import utime

pir_sensor = machine.Pin(14, machine.Pin.IN)

def motion_detected(pin):
    print("Somebody here!")

pir_sensor.irq(trigger=machine.Pin.IRQ_RISING, handler=motion_detected)yt

Code Explanation

  1. Importing necessary modules:
import machine
import utime

The code imports the machine module, which is typically used for interacting with hardware on microcontrollers, and the utime module, which is used for time-related operations.

  1. Setting up the PIR sensor pin:
pir_sensor = machine.Pin(14, machine.Pin.IN)

The code defines a pir_sensor variable and assigns it to the Pin object with the pin number 14 and the mode set to machine.Pin.IN, which means the pin is configured as an input pin to read the state of the PIR sensor.

  1. Defining an interrupt handler function:
def motion_detected(pin):
    print("Somebody here!")

The code defines a function motion_detected that will be executed as an interrupt handler when the PIR sensor detects motion. The function simply prints the message “Somebody here!” to the console.

  1. Setting up an interrupt for the PIR sensor:
pir_sensor.irq(trigger=machine.Pin.IRQ_RISING, handler=motion_detected)

The code sets up an interrupt on the pir_sensor pin using the irq() method. It specifies the trigger condition as machine.Pin.IRQ_RISING, which means the interrupt will be triggered when the pin goes from low to high (i.e., rising edge), indicating motion detected by the PIR sensor. The interrupt handler function motion_detected is specified as the handler argument, so it will be called when the interrupt is triggered.

With this code, whenever the PIR sensor detects motion and the pin goes from low to high, the motion_detected function will be executed, printing the “Somebody here!” message to the console.