Detecting motions using PIR motion sensor with Raspberry PI

Maria Elijah
Geek Culture
Published in
4 min readJul 5, 2021

Wow! welcome back to my fourth series on Raspberry Pi. If you haven’t read the previous ones, feel free to check. I kinda linked each article at the end of every write-up.

This article will build a simple motion detection using PIR (Passive InfraRed) sensor with Raspberry Pi. PIR sensor is used to sense the movement of people, animals, or other objects. Basically, any object that emits infrared heat. Objects with a temperature above absolute zero emit heat energy in the form of radiation. Usually, this radiation isn’t visible to the human eye because it radiates at infrared wavelengths, but it can be detected by electronic devices such as PIR sensors designed for such a purpose.

image from image.made-in-china.com

Practical Explanation:

When a motion is detected, one of the LEDs turns on, and the other if no motion is detected. Also, a mail is sent if a motion is detected.

Hardware needed:

  1. LEDs
  2. 220 Ohms Resistors
  3. Jumper wires
  4. PIR motion sensor
  5. Breadboard
  6. Raspberry Pi 3 Model B+
  7. a 5V 2A micro USB power adapter.

Here is a brief explanation of the PIR motion sensor.

Image from ifuturetech.org

PIR motion sensor: PIR stands for Passive Infrared, which consists of an infrared detector, fresnel lens, and supporting detection circuitry. the lens can detect anything that emits infrared heat such as humans, animals. The heat is picked up by the motion sensor.

A PIR motion sensor has two potentiometers that are used for setting the delay time and sensitivity.

  • The delay time adjustment is used to set how long the output should remain high after detecting motion; it can range from 5 seconds to 5 minutes
  • The sensitivity adjustment is used to set the detection range, which can be anywhere from 3 to 7 meters.

A PIR motion sensor has three pins for connection which are for power(VCC), signal(Output), and ground(GND).

PIR motion sensors are small, inexpensive, low-power, easy to use, and don’t wear out. For that reason, they are commonly found in appliances and gadgets used in homes or businesses.

Software needed:

  1. PuTTy
  2. Raspbian OS

let’s head on to the practical part.

Step 1: Setting up the circuit

So I assume that you have successfully done some basic projects. if not, feel free to check my previous article here

Connecting LEDs and Resistors

  • Connect the LEDs (red and yellow respectively) to the breadboard.
  • Choose the negative one and using the resistor, connect it up to GND (pin 39)
  • Connect the other end to GPIO (pin 20 and 19) respectively

Connecting PIR motion sensor

  • Connect VCC pin to the 5v of Raspberry’s GPIO
  • Connect GND pin to the GND of Raspberry’s GPIO.
  • Connect Output pin to GPIO pin 4.

we should have a connection like this. if not, please feel free to use the picture.

Step 2: python code

  • SSH to the Raspberry PI using its IP address and port 22
  • Enter the Username and password
  • Upgrade and Update the Pi. It’s a good practice to always update and upgrade the pi each time we use it. Enter sudo apt-get update and sudo apt-get upgrade
  • Create a new python file sudo nano detect_motion.py
  • Enter the following codes
  • Run the python file in the terminal using ‘python3 detect_motion.py

Result:

One more step to go!

let’s add the following python codes to send a mail when a motion is detected

Now, our codes should look like this

  • Finally, run the python file in the terminal using python3 detect_motion.py

let’s check out the result-

Note: Make sure the PIR motion sensor is connected properly.

Check the full codes here.

There is another interesting module of the Raspberry Pi that I will be sharing in my next article — Pi Camera🙂!

Thank you for staying till the end.

--

--

Maria Elijah
Geek Culture

Flutter developer, IT support specialist and technical writer. I make DIY projects. I love learning as “learning is Life”