Motion Sensing Using PIR

Hey folks,

In this blog today we are coming up with a new sensor which is PIR sensor. Here  we are going to learn to interface a PIR sensor and a buzzer with our Odinub board by implementing a small project. But first let me brief you about the PIR sensor.

PIR sensor:

A passive infrared sensor (PIR sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. The PIR sensor itself 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, the ambient amount radiated from the room or walls or outdoors. 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.

In this we will make the buzzer on when the PIR sensor detects the motion.


Hardware Required:

Odinub Board

GPIO Converter (1.27mm to 2.54 mm)

PIR sensor


Jumper Wires




The PIR sensor has three pins that is Vcc, Out & Ground. We will connect these pins to our GPIO converter. The Vcc and Ground of the sensor will be connected to the 5V Vcc and Ground pin of the converter respectively. The output pin will be connected to the GPIO pin 21 on converter. Further we will connect buzzer to the GPIO pin 23 on converter and the ground to the ground pin with the help of breadboard.




Now we have done our connections, we will start with our code to run the project.

Step 1: Open a terminal window in our Odinub and make a directory there

mkdir <your_directory_name>

This command will help to make a directory.

Step 2:  Now to move in the directory we will use the following command

cd <your_directory_name>

Step 3: Here we will make a file for our program

sudo nano <your_file_name>.py

This will make a file and open a window where we will write our code.

Step 4: First we will need to import our two built-in libraries as follows

import nubio
import time

This will help us to access GPIO pins and time.

Now we will set the GPIO pins as input or output using our functions as


Now we need to get the data from the output pin of the sensor and have to store it in a variable. For this we need to read the GPIO pin 21 on converter as the data from the sensor will be passed to respective pin. This will be done by the following command.


Further, we have a condition that the buzzer should only go high when and only there is a motion sensed by the sensor. If no motion is sensed then the buzzer should remain low. When the sensor senses a motion it will make the GPIO pin 21 high and the result will be stored in our variable. This data will be used in our conditions to make our buzzer high or low.

For this condition making, we will make use of if..else statements as follows


Step 5: Having done with our code, now we will save our code by Ctrl+X. It will ask to press Y or N to save our code. Pressing Y it will save our code.

To run our code we will need to run the following command.

python <your_file_name>.py
Code :
Code Title :
Code Language :
0 Commenting Overall Comment