luarit
Posts: 8
Joined: Fri Jan 24, 2020 4:01 pm

PIR motion detection code

Fri Jan 24, 2020 4:15 pm

Hello,

I ma trying to do something simple, I want to test the PIR motion sensor (PIR HC-SR501) I have.

I connected to GND, Vc5 and GPIO4. I tried to run below code, but it alwasy detect motion, I dont understand why! I disconnected the sensor and I ran it again and It also detect motion event if it is not plugged any more... any idea?

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)
#Read output from PIR motion sensor
#GPIO.setup(3, GPIO.OUT)
#LED output pin
print "Ready"

while True:
        if GPIO.input(7):
                print "Motion Detection"
        time.sleep(1)

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

Re: PIR motion detection code

Fri Jan 24, 2020 5:56 pm

GPIO4 (pin 7) has the internal pull-up enabled at power-up. It will read high unless a stronger external force sets it low. Perhaps set the internal pull-down in software?

luarit
Posts: 8
Joined: Fri Jan 24, 2020 4:01 pm

Re: PIR motion detection code

Fri Jan 24, 2020 6:54 pm

Thanks Joan. Raspberry is new. What does it mean to set the internal pull down in software? And how I can do it?

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

Re: PIR motion detection code

Fri Jan 24, 2020 8:55 pm

The RPi.GPIO documentation is at https://sourceforge.net/p/raspberry-gpi ... /Examples/

Basically change

Code: Select all

GPIO.setup(7, GPIO.IN)
to

Code: Select all

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

boyoh
Posts: 1468
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: PIR motion detection code

Fri Jan 24, 2020 9:30 pm

You are given the option of setting the GPIO In/ Put impedance ( resistance ) level that the processor can respond too

In its high impedance state Floating, it will not sink or source a working level signal this being a logic level

0 low or 1 high, So you MUST set it to a working impedance ( resistance ) level by setting the internal resistance 50k

or using external resistance of 10k. If you want it to respond to a logic 1 high you connect it to the 0v rail

If you want it respond to a logic 0 low you connect it to the 3.3v rail.

One point is if you have the pull up resister to low a value this will this will set the IN/PUT impedance to low

Ok for noise suppression, but will degrade a low input signal

Regards BoyOh
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: PIR motion detection code

Fri Jan 24, 2020 9:30 pm

You should not need a pull down on an input if it's connected to a PIR sensor as it provides both a low output and a high output.

Have you got the 1-wire interface enabled as gpio4 ( physical pin 7 ) as that is the default pin for the 1-wire interface.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

luarit
Posts: 8
Joined: Fri Jan 24, 2020 4:01 pm

Re: PIR motion detection code

Fri Jan 24, 2020 10:09 pm

I also tried other GPIO as 17, 27, etc,...

luarit
Posts: 8
Joined: Fri Jan 24, 2020 4:01 pm

Re: PIR motion detection code

Fri Jan 24, 2020 10:52 pm

This is how I set up everything.
WhatsApp Image 2020-01-24 at 23.50.18.jpeg
Image
WhatsApp Image 2020-01-24 at 23.50.18.jpeg (73.86 KiB) Viewed 536 times

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: PIR motion detection code

Fri Jan 24, 2020 11:33 pm

You're very likely to burnout all of your GPIOs (one each time you experiment until your RPi dies) with a 5V PIR connected to a GPIO without a 1.8K/3.3K voltage divider.

Code: Select all

   PIR
   |
   |
  +-+
  | |
  | | 1.8KOhm
  +-+
   |
   +---- GPIO
   |
  +-+
  | |
  | | 3.3KOhm
  +-+
   |
   GND
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
rpiMike
Posts: 1340
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: PIR motion detection code

Fri Jan 24, 2020 11:45 pm


luarit
Posts: 8
Joined: Fri Jan 24, 2020 4:01 pm

Re: PIR motion detection code

Sat Jan 25, 2020 12:14 am

It doesnt look to have a problem here
https://www.freva.com/2019/05/21/hc-sr5 ... pberry-pi/

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: PIR motion detection code

Sat Jan 25, 2020 11:26 am

So did you set the PIR up before you connected it to your pi ?

set the time delay pot fully anticlockwise, set the sensitivity pot to the mid position , and put the jumper on the H position for the repeated trigger mode ( see details in the link you posted ).

also check the pin layout matches your PIR as some I have seen have the +power and ground pins reversed, the +power pin should be next to the diode, highlighted yellow in the image below

Image

Then connect your PIR like this.

Image

and run the code in the linked site

Code: Select all

import RPi.GPIO as GPIO
import time
 
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
PIR_PIN = 23
GPIO.setup(PIR_PIN, GPIO.IN)

print('Starting up the PIR Module ')
time.sleep(1)
print ('Ready')

while True:
  if GPIO.input(PIR_PIN):
    print('Motion Detected')
  time.sleep(1)
  
once the PIR is triggered you have to leave it for a few seconds before it will stop triggering and be ready to trigger again.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

luarit
Posts: 8
Joined: Fri Jan 24, 2020 4:01 pm

Re: PIR motion detection code

Sat Jan 25, 2020 12:51 pm

thanks pcmanbob. It is working now. I am not sure why. I only change the Output pin GPIO23 as you said. Not sure why I cannot use the other PIN

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: PIR motion detection code

Sat Jan 25, 2020 1:03 pm

luarit wrote:
Sat Jan 25, 2020 12:51 pm
thanks pcmanbob. It is working now. I am not sure why. I only change the Output pin GPIO23 as you said. Not sure why I cannot use the other PIN
As I said gpio4 is the default pin for the 1-wire interface , if this is enabled it will interfere with the operation of the pin if you try to use it as a normal gpio pin.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”