element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
RIoTboard
  • Products
  • Dev Tools
  • Single-Board Computers
  • RIoTboard
  • More
  • Cancel
RIoTboard
Blog Riotboard: IoT based intrusion detection & alert system: Part1 : Connecting Pir Sensor
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join RIoTboard to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: tusharp
  • Date Created: 28 Sep 2015 12:20 PM Date Created
  • Views 888 views
  • Likes 5 likes
  • Comments 1 comment
  • tusharp
  • alert
  • Ubuntu
  • gpio
  • pir
  • freescale
  • imx6
  • riotboard
  • embedded
  • riot
  • intrusion
  • iot
  • arm9
  • sensor
  • linux
Related
Recommended

Riotboard: IoT based intrusion detection & alert system: Part1 : Connecting Pir Sensor

tusharp
tusharp
28 Sep 2015

In this mini blog series we will build an IoT based intruder detection and alert system.

 

Blogs:

Part1 : Connecting Pir Sensor  <--- this blog

Part2 : Capturing intruder image

Part3 : Email Alerts

 

 

In this post we shall connect a simple cheap Pir motion sensor to Riotboard.

We will be using Pir sensor HC-Sr501.

 

pir_sensor_image

 

pir_sensor_image_open

 

Pir Sensor HC-Sr501 has pins : VCC, GND, OUT.


VCC will be connected to pin2 (5VIN) in J13 expansion port.

GND will be connected to pin4 (GND) in J13 expansion port.

OUT will be connected to pin7 (GPIO4_17) in J13 expansion port.

 

246292s.jpgpir_sensor_pins

 

(plz check usermanual  for more info on J13 expansion port)

 

 

Pir setup..

2yx3tdg.jpg

 

this post uses the Linux BSP available here.

 

Next we will compile the below code and test.

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main()
{
int ret =0;
int fd=0;
char buf[1];

system("devmem 0x20E00A0 w 0x5  ");
system("echo 113 > /sys/class/gpio/unexport");
system("echo 113 > /sys/class/gpio/export");
usleep(100000);
system("echo in > /sys/class/gpio/gpio113/direction");
usleep(100000);

fd = open("/sys/class/gpio/gpio113/value",O_RDONLY);
while(1)
{
ret = read(fd,buf,sizeof(1));
if(lseek(fd,0,SEEK_SET) < 0) return 1;

if(ret>0 && (atoi(buf)==1))
    {
        printf("\n\033[031;43m HUMAN DETECTED \033[0m ");
        fflush(stdout);
        usleep(500000);
    }
else
    printf("\nNo Detection");

fflush(stdin);
fflush(stdout);
usleep(500000);

}
close(fd);
}

         

 

code explanation:

ln12 : using devmem to convert pin7 to gpio.

ln14:  exporting pin7 as gpio in /sys/class  for user.

ln16 : configure pin7 as gpio input , in pir we are reading(input) the OUT pin value .

ln19 : opening gpio as READ_ONLY

ln23: while reading a file the pointer moves by certain position,

           in this case the file  frequently updated.

           so using lseek  system call to read file from beginning,

ln25 : if HIGH value (1)  present it means movement detected.

 

Q. why so many fflush & usleep used ??

A. to ensure the GPIO read and the terminal write happens in sync with program flow.

 

Now perform some hand movement infront of Pir sensor, it will show HUMAN DETECTED

 

No movement

30j4l4w.jpg

 

Hand movement

msk6t5.jpg

 

 

Video demo:

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

 

in the next blog we will capture intruder image & send snapshot based on pir interrupt.

 

 

  • Sign in to reply

Top Comments

  • gadget.iom
    gadget.iom over 9 years ago +1
    Nice tutorial!
  • gadget.iom
    gadget.iom over 9 years ago

    Nice tutorial! image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube