PIR Sensor With MicroPython Using Bharat Pi Board

by bharatpi in Circuits > Electronics

215 Views, 0 Favorites, 0 Comments

PIR Sensor With MicroPython Using Bharat Pi Board

WhatsApp Image 2024-04-21 at 4.24.11 PM.jpeg
WhatsApp Image 2024-04-21 at 4.13.19 PM.jpeg


To demonstrate how to handle interrupts, we’ll build a simple project with a PIR motion sensor. Whenever motion is detected we’ll light up an LED.this project uses the PIR motion sensor and Bharat Pi board to detect moving living objects.it allows us to detect the presence of people, and animals when they are within the range of the sensor.

In this project, we are using Bharat Pi which has both an ESP32 microcontroller and a SimCom A7672S 4G/LTE module.

Bharat Pi is an IoT prototyping board for students, innovators, startups, and developers. A simplified board with an all-in-one compute, network and storage on a single board to build rapid prototypes.


Supplies

WhatsApp Image 2024-04-21 at 2.08.34 PM.jpeg
WhatsApp Image 2024-04-14 at 3.44.58 PM.jpeg
WhatsApp Image 2024-04-21 at 4.12.39 PM.jpeg

Parts required:

Here’s a list of the parts you need,

1. ESP32

2. LED

3. PIR motion sensor

4. USB Cable

5. Jumper wires

Pre required:

To follow this instruction you need an IDE to write and upload the code to your board, you can use uPyCraft IDE or Thonny IDE.you also need Micropython firmware installed in your ESP32 boards.

If you don't have the setup means then you can check the README file of installing and setup of uPyCraft and Thonny IDE in our Bharat Pi github page you can find the link below,

> https://github.com/Bharat-Pi/MicroPython/blob/main/README.md

PIR Sensor

WhatsApp Image 2024-04-21 at 2.06.17 PM.jpeg


PIR is a short form for Passive Infrared sensor – an electronic sensor which can detect levels of infrared radiation from objects in its field of view.when a warm body like a human or animal passes by, it first intercepts one-half of the PIR sensor, which causes a positive differential change between the two halves.when the warm body leaves the sensing area, the reverse happens, whereby the sensor generates a negative differential change. These change pulses are what is detected.

PIR sensors are more complicated than many other sensors explained because multiple variables affect the sensor's input and output.

The PIR sensor has two slots in it, each slot is made of a special material that is sensitive to IR.When the sensor is idle, both slots detect the same amount of IR.

pinout of PIR Sensor

GND

Data

VCC

Schematic – ESP32 With PIR Sensor

WhatsApp Image 2024-04-21 at 2.06.43 PM.jpeg

In this section, we will see how to connect PIR sensor with ESP32. The PIR sensor will be connected with the ESP32 board with its 3 pins (GND, VCC, DATA).

Follow the schematic diagram for the ESP modules and connect them accordingly. Connect the ESP32 device with the PIR sensor as shown in the schematic diagram above:

Pinout of the PIR Sensor :

PIR Sensor - ESP32

VCC - Power Supply

Data - GPIO

GND - GND

Wire the PIR sensor to the ESP32 as shown in the following schematic diagram. We’re connecting the vcc pin to 3v/5v and the data pin to GPIO 18, but you can use any other suitable pins, and GND pin to GND of esp32. then connect led to 23 and GND pin of esp32.

ESP32 Wiring With PIR Sensor :

PIR Sensor - ESP32

VCC - 5V / 3V

Data - 18 (ANY GPIO)

GND - GND

LED - 23 - GND

CODE Snippet (PIR Sensor)



from machine import Pin
from time import sleep


motion = False


def handle_interrupt(pin):
  global motion
  motion = True
  global interrupt_pin
  interrupt_pin = pin 


led = Pin(23, Pin.OUT)
pir = Pin(18, Pin.IN)


pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)


while True:
  if motion:
    print('Motion detected! Interrupt caused by:', interrupt_pin)
    led.value(1)
    sleep(10)
    led.value(0)
    print('Motion stopped!')
    motion = False



Applications

PIR sensor can be used for a wide range of real-life applications,

·      Security use case: Alert or raise an alarm based on a human moment.

·      Automatic door opening upon human arrival.

·      Automatic lighting controls to help reduce power consumption.

·      Vending machines for automated dispensing based on a gesture.

·      Lift lobbies, count people entering malls, events, public place etc.

·      Wireless remotely monitoring for secured assets.


Demonstration

Screenshot (16).png

Upload the code to your ESP32/ESP8266 board, the LED should turn on for 10 seconds when motion is detected, and a message should be printed in the Shell.after 10 seconds the LED turns off.