16 Replies Latest reply on Nov 22, 2018 3:23 PM by jomoenginer

    Wiring up a PIR to a PiFace Digital2

    balanga

      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.

        • Re: Wiring up a PIR to a PiFace Digital2
          lui_gough

          It would help if you could specify the type of PIR you are trying to use? Is it an alarm system type PIR that needs a 12V supply and has a set of NO/NC relay contacts? Or is it a 5V module of sorts that has a logic signal output?

           

          In general, you need to power the PIR (+ve and GND) and get a signal from the PIR. If the PIR outputs a logic level signal, you could use the digital inputs and connect the PIR output to one of them. If the PIR is a relay output, then you will need to configure a pull-up using resistors to provide an appropriate logic signal.

           

          - Gough

          3 of 3 people found this helpful
            • Re: Wiring up a PIR to a PiFace Digital2
              balanga
              2 of 2 people found this helpful
                • Re: Wiring up a PIR to a PiFace Digital2
                  dougw

                  I think you are correct about the pinout:

                  https://www.mpja.com/download/31227sc.pdf

                  2 of 2 people found this helpful
                    • Re: Wiring up a PIR to a PiFace Digital2
                      balanga

                      From my searches, I should connect:-

                       

                      VCC to GPIO pin 2

                      OUT to GPIO pin 7

                      GND to GPIO pin 6

                       

                      according to https://www.researchgate.net/profile/Zakaria_Rada2/publication/312218161/figure/fig3/AS:449572641218564@1484197878779/GP…

                       

                      but am unsure how to map these pins to the PiFace Digital2...

                       

                      Having said that this diagram https://projects-static.raspberrypi.org/projects/physical-computing/0cb2cbd34292a05a668aeea3f291ceb3c7d9cd83/en/images/p…  appears to have the pins inverted....

                       

                      This diagram http://www.piface.org.uk/static/img/piface_digital_2/pifacedigital2_diagram.png  show two possible locations for VCC and three possible locations for GND. Does it make any difference which I choose? And am I correct in thinking OUT must be one of the eight green terminals?

                      2 of 2 people found this helpful
                        • Re: Wiring up a PIR to a PiFace Digital2
                          lui_gough

                          Operating with and without the PiFace Digital is slightly different. The diagrams you have before are designed for direct GPIO interfacing rather than going through an I/O expander such as the PiFace Digital. The principles are, however, much the same.

                           

                          The VCC line should be connected to any of the 5V outputs from the PiFace Digital, the GND line should be connected to any of the GND lines on the PiFace Digital and the OUT line can be connected to any one of the input lines on the PiFace Digital. Your code should read the corresponding input line to find out the status of the PIR.

                           

                          - Gough

                          4 of 4 people found this helpful
                            • Re: Wiring up a PIR to a PiFace Digital2
                              balanga

                              I finally managed to get this working connecting the OUT pin to the terminal 0 of the input row.  I'm using the following code:-

                               

                              # Alarm program by Francesco Balsamo

                              #

                              # Using a PIR sensor with inverted logic

                              #

                               

                               

                              from time import sleep

                               

                               

                              import pifacedigitalio as pfio

                              # import piface.pfio as pfio

                               

                               

                              pfio.init()

                               

                               

                              while(True):

                                  for pin_number in range(0,4):

                                      if pfio.digital_read(pin_number) == 0:

                                          pfio.digital_write(pin_number+4,1) # Using only the leds

                                      else:

                                          pfio.digital_write(pin_number+4,0) # Disabling only the leds

                              This switches on LEDs 7,6,5,4  of which 4 is toggled by the PIR, although I can't figure how the code is supposed to work at all...

                               

                              I guess this calls for some experimentation. At least I have managed to get the PIR to control one of the LEDs.

                              1 of 1 people found this helpful
                                • Re: Wiring up a PIR to a PiFace Digital2
                                  lui_gough

                                  The code is pretty simple:

                                  - while (True) runs the code block that follows infinitely.

                                  - for pin_number in range(0,4) basically repeats the code for every pin_number in this set (0, 1, 2, 3)

                                  - if pifo.digital_read(pin_number) == 0 basically says "is this input at pin_number low (as the PIR output goes low when it senses something)?"

                                  - pfio.digital_write(pin_number+4,1) basically says "lets make LED at pin_number+4 (e.g. for pin 0, then LED4 will) turn on"

                                  the other case is for if that isn't the case, it turns off the LED.

                                   

                                  This is a rather simple case of polling the input continuously ...

                                   

                                  - Gough

                                  2 of 2 people found this helpful
                                    • Re: Wiring up a PIR to a PiFace Digital2
                                      jomoenginer

                                      The example that was referenced looks like a PiFace Digital rather than a Digital 2 but should be similar.  From my understanding, the first 4 (0-3) inputs/outputs are tied to the switches (S0-S3) so the example skips these to set LEDs 4-7 by offsetting the value from the 'for/range' loop with "pin_number+4" .  However, the "if" statement is only checking for inputs 0-3 which would be one of the switches or an input from 0-3 but then sets an LED, 4 plus pin_number (4-7), which seems odd.  The example also is using 4 PIR sensors, so if you have only 1 PIR sensor then you could remove the for range loop from the code example.

                                       

                                      If you want an output on say output 4, then you should put the Out from the PIR sensor to Input 4 on the Digital 2.   However, I found the inputs on the Digital 2 are tied to 5v which causes an issue with the Parallax PIR sensor so I place a 10K Ohn resistor between the input port I am using on the Digital 2 to ground (GND) to ensure the PIR reading is clean.  You can disable the pull-up on the input of the Digital 2 but that causes them to float so you would still have to to either tie it high or low depending on the device connected to the Digital 2.

                                       

                                      Reference to PiFace Digital inputs:

                                      https://pifacedigitalio.readthedocs.io/pifacedigital.html#inputs

                                      3 of 3 people found this helpful
                                        • Re: Wiring up a PIR to a PiFace Digital2
                                          balanga

                                          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?

                                          2 of 2 people found this helpful
                                            • Re: Wiring up a PIR to a PiFace Digital2
                                              dougw

                                              If sensor == 0 set a flag = 0.

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

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

                                              • Re: Wiring up a PIR to a PiFace Digital2
                                                jomoenginer

                                                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.

                                                2 of 2 people found this helpful
                                                • Re: Wiring up a PIR to a PiFace Digital2
                                                  jomoenginer

                                                  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.

                                                  2 of 2 people found this helpful
                                • Re: Wiring up a PIR to a PiFace Digital2
                                  peteroakes

                                  A lot of these PIR sensors will work on 3V3 and if the one you have is similar to the one Doug shows above then you should be able to feed the OUT signal directly to the PI

                                   

                                  I cant remember if the PIFace Dig 2 has level shifter, if it does them a more reliable 5V supply will also be OK

                                   

                                  But in a basic setup you would not need the PIFace Digital at all.

                                   

                                  I do have an older video showing how to wire and use the PIFace Digital 2 here https://www.hackster.io/peteroakes/piface-digital-2-on-a-rasberry-pi-2-and-windows-10-iot-3aa858  but it uses Windows 10 for IOT. the wiring will be the same

                                  2 of 2 people found this helpful