Page 1 of 1

PIR Sensor Codding Issues

Posted: Tue Apr 28, 2020 3:08 pm
by solodrama
Hello guys. I have just start to do some basic projects with Pi 4 and I just stucked at the beginning of everything. I started to work with a PIR sensor and I couldn't make it worked.

I simply use that code;

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

try:
    while True:
        if GPIO.input(4):
            print("Motion")
            time.sleep(1)
        else:
            print("No Motion")
            time.sleep(1)

finally:
    GPIO.cleanup()
Even If I do not connect PIR sensor, when I run the code, it says "Motion" which is not true. Is that normal? I also used several ready to use projects for PIR sensor but none of them worked. What am I doing wrong?

Re: PIR Sensor Codding Issues

Posted: Wed Apr 29, 2020 9:22 am
by pcmanbob
Hi.

So assuming you have the pir sensor connected up correctly and you have gone through the set up procedure ,

I would suggest you use a different gpio pin , try 16,20,21 as they don't have any other functions assigned to them , were as gpio 4 is also the 1-wire interface which can cause problems if it is enabled.

One point , if you are following tutorials be aware some use a different make/version of the sensor which has the Vcc and ground pins reversed, so check your actual sensor to see which is which and don't blindly follow the tutorial.

If after doing this you still cant get it to work post a picture showing how you have it connected and the back of your sensor.

Re: PIR Sensor Codding Issues

Posted: Wed Apr 29, 2020 10:43 am
by solodrama
pcmanbob wrote: Hi.

So assuming you have the pir sensor connected up correctly and you have gone through the set up procedure ,

I would suggest you use a different gpio pin , try 16,20,21 as they don't have any other functions assigned to them , were as gpio 4 is also the 1-wire interface which can cause problems if it is enabled.

One point , if you are following tutorials be aware some use a different make/version of the sensor which has the Vcc and ground pins reversed, so check your actual sensor to see which is which and don't blindly follow the tutorial.

If after doing this you still cant get it to work post a picture showing how you have it connected and the back of your sensor.
Actually the first thing that I wonder is that, even if I do not connect PIR sensor to the Pi, when I run the code it still says there is motion. Is that normal?

Re: PIR Sensor Codding Issues

Posted: Wed Apr 29, 2020 10:51 am
by pcmanbob
solodrama wrote:
Wed Apr 29, 2020 10:43 am

Actually the first thing that I wonder is that, even if I do not connect PIR sensor to the Pi, when I run the code it still says there is motion. Is that normal?
With nothing connected to the gpio pin be it gpio 4 or one of the ones I suggested you will get random results because the gpio pin is floating ,
a pin is said to be floating when there is no connection to it so it can fluctuate between high or low.