0 Replies Latest reply on Feb 24, 2016 5:34 PM by joe-iot

    Reading Parallax PIR sensor with Galileo Gen 2 and Python

    joe-iot

      I'm trying to wrap my brain around the Galileo Gen 2 and reading sensors. This is an example of the Galileo Gen 2 and the Parallax PIR sensor being monitored with python.

       

      Wiring example:

      GalileoGen2-PIR.png

       

       

      The process is simple.

      1. Initialize the digital pin.

      2. Wait a few seconds for it to settle.

      3. Then read the pin, it it goes high motion has been detected.

       

      Code:

      #!/usr/bin/env python
      # File    : pir.py  An example script of reading a Parallax PIR sensor
      # Author  : Joe McManus josephmc@alumni.cmu.edu
      # Version : 0.1  02/24/2016
      # Copyright (C) 2016 Joe McManus
      #
      # This program is free software: you can redistribute it and/or modify
      # it under the terms of the GNU General Public License as published by
      # the Free Software Foundation, either version 3 of the License, or
      # (at your option) any later version.
      #
      # This program is distributed in the hope that it will be useful,
      # but WITHOUT ANY WARRANTY; without even the implied warranty of
      # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      # GNU General Public License for more details.
      #
      # You should have received a copy of the GNU General Public License
      # along with this program.  If not, see <http://www.gnu.org/licenses/>.
      
      import argparse
      import time             
      import mraa    
      import datetime
      
      parser = argparse.ArgumentParser(description='Script to monitor PIR sensor on Intel Galileo')
      parser.add_argument('pinNumber', help="The pin number the PIR is attached to. i.e. 2-13", type=int)
      parser.add_argument('--version', action='version',version='%(prog)s 0.1')
      args=parser.parse_args()
      
      pin = mraa.Gpio(args.pinNumber)
      pin.dir(mraa.DIR_IN)
      
      
      print("Waiting for PIR to settle")
      i=5
      while i > 0:
              print i
              time.sleep(1)
              i=i-1
      
      print("Detecting movement")
      try:
              while True: 
                      if pin.read(): 
                              print("Motion Detected " + str(datetime.datetime.now()))
                      time.sleep(2)
      except:
              quit()
      
      
      

       

      Output:

      
      root@galileo:~# ./pir.py 4
      Waiting for PIR to settle
      5
      4
      3
      2
      1
      Detecting movement
      Motion Detected 2016-02-25 00:08:46.188826
      Motion Detected 2016-02-25 00:08:50.194598
      
      

       

      Code can be downloaded here: https://github.com/joemcmanus/pir

       

      Thought this might be helpful for others.

       

      Cheers,

      -Joe