User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Problem getting PIR to work...!

Mon Apr 01, 2013 2:29 pm

Hi all, I need help!!!

I purchased this PIR from Cool Components: http://www.coolcomponents.co.uk/catalog ... p-312.html.

I have followed a couple of tutorials but can't seem to get it working properly. The code I am using is:

Code: Select all

#!/usr/bin/python 
import RPi.GPIO as GPIO 
import time   
GPIO.setmode(GPIO.BCM)   
GPIO_PIR = 7  
print "PIR Module Test (CTRL-C to exit)"  
GPIO.setup(GPIO_PIR,GPIO.IN)      
Current_State  = 0
Previous_State = 0  
try:     
  print "Waiting for PIR to settle ..."    
     while GPIO.input(GPIO_PIR)==1:
       Current_State  = 0
     print "  Ready"
     while True :       
       Current_State = GPIO.input(GPIO_PIR)
       if Current_State==1 and Previous_State==0:
         print "  Motion detected!"      
         Previous_State=1
      elif Current_State==0 and Previous_State==1:
         print "  Ready"      
         Previous_State=0      
      time.sleep(0.01)   
except KeyboardInterrupt:   
  print "  Quit"  
  GPIO.cleanup() 
I have read in some places that you need a pull-up resistor. I have tried 4.7K, 10k and 20k with no luck.

It just keeps saying that motion is detected over and over. Even when i am in a different room.

Can anyone advise me on what to do??? :)
Pi Pi Pi...

User avatar
joan
Posts: 14944
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 2:57 pm

Could you say which pins on P1 are connected to which pins on your sensor?

http://elinux.org/Rpi_Low-level_peripherals

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 3:03 pm

Hi,

Red wire connected to pin 2 (+5V)
Brown wire to GND
Black wire to pin 26 (BCM 7)

Any help would be appreciated thanks :D
Pi Pi Pi...

User avatar
joan
Posts: 14944
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 3:26 pm

Have you enabled gpio7's internal pull up, or have you tried pulling up the black wire via a resistor to 3.3V?

The signal should go low (0) when motion is detected.

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 3:42 pm

joan wrote:Have you enabled gpio7's internal pull up, or have you tried pulling up the black wire via a resistor to 3.3V?

The signal should go low (0) when motion is detected.
I have physically attached a resistor between the Red & Black wires... Is this not correct???
Also how would I enable the internal pull up for GPIO 7???
I'm a bit of a nube to all this and really appreciate your help. :D
Pi Pi Pi...

User avatar
joan
Posts: 14944
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 4:18 pm

drewibbo wrote:
joan wrote:Have you enabled gpio7's internal pull up, or have you tried pulling up the black wire via a resistor to 3.3V?

The signal should go low (0) when motion is detected.
I have physically attached a resistor between the Red & Black wires... Is this not correct???
Also how would I enable the internal pull up for GPIO 7???
I'm a bit of a nube to all this and really appreciate your help. :D
Between the red and black would be pulling the pin up to 5V which may eventually strain the gpio as they are only designed for 3.3V. Without the resistor the gpio might have blown straight away. It's probably safer to tie black to 3V3 via a resitor.

I'm fairly sure the Python library you are using has the ability to set the internal pull ups and downs on individual gpio pins. Just a question of searching the documentation. I don't know Python so can't help with that.

However it may be a simple logic problem. Have a look at the logic in your code. Does the alarm being low when active (rather than high) screw up the code?
Last edited by joan on Mon Apr 01, 2013 5:41 pm, edited 1 time in total.

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 4:27 pm

I will take a look as soon as I get home. Had to work today... :(

Thanks for your help. I will reply when I've checked it out :)
Pi Pi Pi...

drgeoff
Posts: 10765
Joined: Wed Jan 25, 2012 6:39 pm

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 5:29 pm

joan wrote:Between the red and black would be pulling the pin up to 5V which may eventually strain the gpio as they are only designed for 5V. Without the resistor the gpio might have blown straight away. It's probably safer to tie black to 3V3 via a resitor.
Probably a typo by joan - the GPIOs are not 5 volt tolerant. Tie back to 3v3 as she suggests.

drgeoff
Posts: 10765
Joined: Wed Jan 25, 2012 6:39 pm

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 5:41 pm

Code: Select all

       if Current_State==1 and Previous_State==0:
         print "  Motion detected!"      
         Previous_State=1
      elif Current_State==0 and Previous_State==1:
         print "  Ready"      
         Previous_State=0      
      time.sleep(0.01)
Not sure if this is relevant; the 'elif' and 'time.sleep' lines are one space to the left cf the 'if' line.
Last edited by drgeoff on Mon Apr 01, 2013 5:42 pm, edited 1 time in total.

User avatar
joan
Posts: 14944
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 5:42 pm

drgeoff wrote:
joan wrote:Between the red and black would be pulling the pin up to 5V which may eventually strain the gpio as they are only designed for 5V. Without the resistor the gpio might have blown straight away. It's probably safer to tie black to 3V3 via a resitor.
Probably a typo by joan - the GPIOs are not 5 volt tolerant. Tie back to 3v3 as she suggests.
Thanks, I've corrected my mistake.

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 6:41 pm

Thanks for the suggestions.
When you say tie back to 3.3V, do you literally mean draw power from pin 1 (3.3V) and connect a resistor between the 3.3V and the data wire connected to pin 26??? :D

Also, just a quick note. The datasheet states that it only operates from 5V - 12V. I am not getting anything when connecting to pin 1 (3.3V) :(
Pi Pi Pi...

User avatar
joan
Posts: 14944
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 6:50 pm

Not quite. Still power the IR unit from 5V. Tie the output to 3V3 via a resistor. Also connect the output pin to the gpio. This ensures that the gpio normally sees the safe voltage of 3V3. When the alarm fires the output will sink enough current to defeat the 3V3 voltage through the resistor, and the gpio will then see 0V.

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: Problem getting PIR to work...!

Mon Apr 01, 2013 7:34 pm

Thank you both so much for your help. It looks like you have solved my issue. :D :D :D

Tying the data wire to 3.3V was correct.
Also had to amend the code to read when the PIR state goes LOW, not HIGH.
Pi Pi Pi...

Gadjetnut
Posts: 18
Joined: Fri Apr 05, 2013 9:13 pm

Re: Problem getting PIR to work...!

Fri Apr 05, 2013 10:16 pm

If you are building an alarm system, please check out this website:

http://www.projects.privateeyepi.com/home

They have full instructions on how to create an alarm system with email alerts, web based dashboard to view and control you alarm. No fees.

Image

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: Problem getting PIR to work...!

Wed Apr 10, 2013 10:31 am

In addition to this I have a one wire temperature sensor connected to 3.3V / GPIO 7 / GND.

I have managed to get the PIR to work with the above information but it then kicks out my temp sensor and they don't work at the same time.

Any reason for this that anyone can think of??? :D
Pi Pi Pi...

User avatar
raspberrydt
Posts: 57
Joined: Mon Jan 21, 2013 3:09 pm

Re: Problem getting PIR to work...!

Wed Apr 10, 2013 11:14 am

Are you serious? You connected the PIR black wire to GPIO7 AND the 1w temperature sensor to GPIO7 at the same time?

User avatar
joan
Posts: 14944
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem getting PIR to work...!

Wed Apr 10, 2013 11:47 am

drewibbo wrote:In addition to this I have a one wire temperature sensor connected to 3.3V / GPIO 7 / GND.

I have managed to get the PIR to work with the above information but it then kicks out my temp sensor and they don't work at the same time.

Any reason for this that anyone can think of??? :D
The two sensors use different protocols and will conflict with each other. Choose another gpio for the one wire sensor.

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: Problem getting PIR to work...!

Wed Apr 10, 2013 12:01 pm

Sorry, i foolishly neglected to mention that the PIR is on a separate pin. There should be no conflict...

Andy
Pi Pi Pi...

User avatar
joan
Posts: 14944
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem getting PIR to work...!

Wed Apr 10, 2013 1:24 pm

Probably best to post the code you are using for the two devices and say how the 1-wire device is connected.

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: Problem getting PIR to work...!

Wed Apr 10, 2013 5:22 pm

Please see code below. Also bear in mind that these scripts are never run at the same time. If i run the PIR code, it seems to make the /sys/bus/w1/devices/28-00000495b443/ folder disappear. I then have to re-run a couple of modprobes to get it working again...!

Here is the code for my PIR (pir.sh):

Code: Select all

pin=8
i=0
gpio mode $pin in
echo "INITIALISING..."
sleep 5
echo "Waiting for PIR to settle..."

while [ `gpio read $pin` == "1" ]; 
do
  echo "Ready...!"
    while true;
    do
      if [ `gpio read $pin` == "0" ]; then
        echo "MOTION DETECTED $i"
        i=$(($i+1))
        sleep 5
      elif [ `gpio read $pin` == "1" ]; then
        echo "Ready to detect..."
      fi
    done
done
And for my One Wire Temperature Sensor (temp.py):

Code: Select all

#!/usr/bin/python -O

import time

timestamp = time.strftime("%d-%m-%Y,%H:%M:%S")
logname = time.strftime("%d-%m-%Y")
filename = "".join(["temp_log_", logname, ".txt"])
datafile = open(filename, "a")

tfile = open("/sys/bus/w1/devices/28-00000495b443/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature = temperature / 1000
datafile.write(timestamp + "," + str(temperature) + ",\n")
datafile.close()
Pi Pi Pi...

User avatar
raspberrydt
Posts: 57
Joined: Mon Jan 21, 2013 3:09 pm

Re: Problem getting PIR to work...!

Thu Apr 11, 2013 11:09 am

I don't speak Python. Your PIR script looks fine, except that you don't ned the "elif". "else" without any condition will be fine, because if the value read from PIR is not 0 it can only be 1, no other values are possible. So no need to check.
But in fact it will not make any difference.

I will try this at home to see what happens ;-)

icemanstan
Posts: 6
Joined: Mon Jul 15, 2013 4:50 pm

Re: Problem getting PIR to work...!

Mon Aug 19, 2013 4:57 am

Question to drewibbo - I am having a problem getting the PIR to work. What resistor did you use to get it to work?

Also, is this the correct way to connect the resistor?
pir-diagram.gif
pir-diagram.gif (6.6 KiB) Viewed 6333 times

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13110
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Problem getting PIR to work...!

Mon Aug 19, 2013 10:34 am

a 2K2 resistor should work, but the value is not critical, anything between 1K8 and 47K should be fine.

the wiring configuration is ok.

Return to “Troubleshooting”