Detect the Human Movement using PIR sensor



  1. Introduction:

The PIR sensor is split into two slots that 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 , which makes the output fluctuate high or low. This change in output voltage is a result of detection of motion.

After the sensing module is wired, there is a one-minute initialization. During the initialization, module will output for 0~3 times at intervals. Then the module will be in the standby mode. Please keep the interference of light source and other sources away from the surface of the module so as to avoid the misoperation caused by the interfering signal. Even you’d better use the module without too much wind, because the wind can also interfere with the sensor.



  1. Scope:

This experiment demonstrates a basic motion detection system that can be used in various applications such as security systems, automatic lighting, and occupancy detection. The principles and code outlined here can be extended to more complex systems, including integrating alarms, cameras, or other output devices.

  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • LED

  • PIR motion Sensor

  • Buzzer

  • Resistor (220 KΩ)

  • Breadboard

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.

  1. Connection Details:

PIR Sensor Connections:

  • VCC: Connect to the 3.3V pin on the Raspberry Pi Pico.

  • GND: Connect to the ground (GND) pin on the Pico.

  • OUT: Connect to a GPIO pin on the Pico.

Output Pin Connection:

  • LED: Connect an LED to another GPIO pin (e.g., GPIO 14) to indicate when motion is detected.

  • Buzzer : Connect an buzzer to another GPIO pin (e.g., GPIO 15) to indicate when motion is detected.

Power Supply: Ensure the Raspberry Pi Pico is powered via a USB connection to your computer or an external power source.





  1. Working Principle:

The PIR sensor detects changes in infrared radiation, typically emitted by warm-blooded animals or humans or . When a person or animal moves within the sensor’s range, the PIR sensor's output pin sends a high signal. The Raspberry Pi Pico reads this signal through a GPIO pin. When the signal is high, the microcontroller will set another GPIO pin high to indicate motion detection, which could be connected to an LED and buzzer for visual/auditory feedback.











  1. Application code:



  • MicroPython:

from machine import Pin

import time

import utime


# Set up the GPIO pin for the PIR sensor, LED, and Buzzer

pir_pin = Pin(0, Pin.IN) # Connect OUT pin of PIR to GPIO 0

led_pin = Pin(14, Pin.OUT) # Use GPIO 14 as output for LED

buzzer = Pin(15, Pin.OUT) # Use GPIO 15 as output for Buzzer


while True:

if pir_pin.value() == 1:

for i in range(50):

led_pin.toggle() # Toggle LED on and off

buzzer.toggle() # Toggle Buzzer on and off

utime.sleep(0.1) # 0.1-second delay between toggles

else:

led_pin.value(0) # Turn off LED when no motion is detected

buzzer.value(0) # Turn off Buzzer when no motion is detected

time.sleep(0.1) # Short delay to debounce the sensor reading


  • Embedded C:

#include "pico/stdlib.h"

#include "hardware/gpio.h"


// Define GPIO pins

#define PIR_PIN 0 // GPIO 0 for PIR sensor

#define LED_PIN 14 // GPIO 14 for LED

#define BUZZER_PIN 15 // GPIO 15 for Buzzer


int main() {

// Initialize GPIO

stdio_init_all();

gpio_init(PIR_PIN);

gpio_set_dir(PIR_PIN, GPIO_IN); // PIR pin as input

gpio_init(LED_PIN);

gpio_set_dir(LED_PIN, GPIO_OUT); // LED pin as output

gpio_init(BUZZER_PIN);

gpio_set_dir(BUZZER_PIN, GPIO_OUT); // Buzzer pin as output


while (true) {

if (gpio_get(PIR_PIN)) {

// Motion detected, toggle LED and Buzzer

for (int i = 0; i < 50; ++i) {

gpio_xor_mask(1 << LED_PIN); // Toggle LED

gpio_xor_mask(1 << BUZZER_PIN); // Toggle Buzzer

sleep_ms(100); // 0.1-second delay

}

} else {

// No motion detected, turn off LED and Buzzer

gpio_put(LED_PIN, 0);

gpio_put(BUZZER_PIN, 0);

sleep_ms(100); // Short delay to debounce the sensor

}

}


return 0;

}



















  1. Hardware Image:

  1. Output:

When the experiment is running, and the PIR sensor detects motion, the connected output pin (e.g., an LED and buzzer) will be activated, indicating that motion has been detected. The GPIO pin assigned to the output will go high (logical 1) when motion is detected, and it will stay low (logical 0) when no motion is present.

  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Set the project name and version

project(pir_motion_detection C CXX)


# Set the Pico SDK path

set(PICO_SDK_PATH /path/to/pico-sdk)


# Initialize the Pico SDK

include(${PICO_SDK_PATH}/external/pico_sdk_import.cmake)


# Initialize the Pico SDK

pico_sdk_init()


# Add executable

add_executable(pir_motion_detection

main.c

)


# Link the Pico SDK libraries

target_link_libraries(pir_motion_detection

pico_stdlib

)


# Create map/bin/hex/uf2 file

pico_add_extra_outputs(pir_motion_detection)


# Set the serial port for debugging

pico_set_program_name(pir_motion_detection "pir_motion_detection")

pico_set_program_version(pir_motion_detection "1.0")

  1. References:





These references provide comprehensive inform

Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)