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
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Wiring up a PIR to a PiFace Digital2
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 16 replies
  • Subscribers 665 subscribers
  • Views 3100 views
  • Users 0 members are here
  • piface_digital pir
  • rasberry_pi_3_b_plus
Related

Wiring up a PIR to a PiFace Digital2

balanga
balanga over 6 years ago

Apologies for this newbie type question, but I can't figure out how to use inputs on a PiFace Digital2.... I would like to connect up a PIR to a to this device and have come across instructions here Review of PiFace Digital , which even has sample code, but it's not clear to me which wires go where.

 

Any guidance would be appreciated.

  • Sign in to reply
  • Cancel
  • balanga
    balanga over 6 years ago in reply to jomoenginer

    After some experimentation I came up with:-

     

    import time

    import pifacedigitalio as pfio

    start_time = time.time()

    print('start',time.time())

     

     

    pfio.init()

     

     

    while(True):

        for pin_number in range(0,1):

            if pfio.digital_read(pin_number) == 0:

                print('hello')

                print('elapsed time',time.time() - start_time)

     

    This produces something like:

    start 1542805023.9155102

    hello

    elapsed time 2.0424649715423584

    hello

    elapsed time 2.0430703163146973

    .

    .

    .

    .

    hello

    elapsed time 4.6204235553741455

    hello

    elapsed time 4.62097692489624

    hello

    elapsed time 4.621711015701294

    hello

    elapsed time 11.211778163909912

    hello

    elapsed time 11.2123863697052

    hello

    elapsed time 11.21294355392456

    hello

    elapsed time 11.213491439819336

    hello

    elapsed time 11.214038372039795

    .

    .

    hello

    elapsed time 13.741790294647217

    hello

    elapsed time 13.742336750030518

    I'm trying to figure out what is going on... I have the Delay Time setting to max, which I think is around 7 secs, so I guess each output of 'hello' is a result of polling which must be taking place every .001 secs over 7 secs.

     

    How would I go about printing a single 'hello' after motion has been detected?

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • dougw
    dougw over 6 years ago in reply to balanga

    If sensor == 0 set a flag = 0.

    print if the flag == 1 and the sensor == 0. 

    When the signal == 1 set the flag = 1.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • jomoenginer
    jomoenginer over 6 years ago in reply to balanga

    Yeah, what Doug said.

     

    From my experience, a PIR sensor stays active for a period of time after it senses motion, so I would suspect this is what your are seeing.

     

    You could put another while loop after the if statement to have it wait for the input pin change state again such as:

    NOTE: This will cause the code to pause while waiting for the pin_state to change to 1 again.

    pin_number = 0
    pin_state = 1
    
    
    while(True):
    
    
        pin_state = pfio.digital_read(pin_number)
        if pin_state == 0:
    
    
            print('hello')
    
    
            print('elapsed time',time.time() - start_time)
    
    
            while pin_state is 0:
                pin_state = pfio.digital_read(pin_number)
                time.sleep(1)

     

     

    NOTE: the 'for range' in your code is only looping on 0 so it is a bit unnecessary.

    for pin_number in range(0,1):

     

    There are a ton of ways to accomplish this in python so you may have to experiment to find what works best for you.

     

    Did you tie the input pin low with a 10K resistor to ground?  This will ensure the pin is 0 when the signal is not present.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Cancel
  • jomoenginer
    jomoenginer over 6 years ago in reply to balanga

    So, I tested this with a RasPi B+ and Digital 2 and after looking at some previous code I had I seemed to have missed something in my example. I found for the PIR sensor, it was best to disable the pullup on the Digital 2 for the pin you are reading an input from.

    This is accomplish by adding the following:

     

    pfio.digital_write_pullup( pin_number, 0 )

     

    This seems to work in my config and should give you idea of what is needed to read the input:

     

    #!/usr/bin/env python3 
    
    import sys 
    PY3 = sys.version_info[0] >= 3 
    if not PY3: 
        print("This app only works with `python3`.") 
        sys.exit(1) 
    
    import os 
    import time 
    import pifacedigitalio as pfio 
    
    
    start_time = time.time() 
    print('start', time.time()) 
    
    
    pfio.init() 
    
    time.sleep(1) 
    
    pin_number = 3 
    pin_state = 1 
    pfio.digital_write_pullup( pin_number, 0 ) 
    
    while(True): 
    
        pin_state = pfio.digital_read(pin_number) 
        print ("pin_state = %d" % pin_state) 
        if pin_state is 0: 
            print('hello') 
            print('elapsed time ', time.time() - start_time) 
    
            while pin_state is 0: 
                print("We're looping!")  
                pin_state = pfio.digital_read(pin_number) 
                time.sleep(1) 
    
        time.sleep(1)

     

    NOTE: I'm using input 3 and python3, so you would have adjust this for your config.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • balanga
    balanga over 6 years ago in reply to jomoenginer

    Many thanks for the help. My python programming skills are non existent so would not have managed to come up with this, although I'm wondering if you have a typo in line 18... Also my electronics knowledge is non existent so using resistors is a no no at this time. However I am now able to use my PIR connected to my PiFace Digital2 to trigger various actions. The next step is to get the PIR to do something with the relays.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jomoenginer
    jomoenginer over 6 years ago in reply to balanga

    Have a look at the post i made yesterday.  The code there does work in my config.

     

    For the relays, they should be tied to output pins 0 and 1 and would be controlled as such:

    pifacedigital.output_pins[0].turn_on()

     

    The Relay section of the following doc for the PiFace Digial as the commands to control the relays.

    https://media.readthedocs.org/pdf/pifacedigitalio/latest/pifacedigitalio.pdf

     

    Note: Udemy has a number of courses discounted to $9.99 for Black Friday in the US.  That might be a good place to find a Python course that will assist your learning. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • 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