I set up a simple curcuit and tried a pullup and a pulldown scenario, but both lead to a lot of "false positives". Currently I ended up with the circuit you can see in the image. I have no button attached, but jumpingwires i connect and disconnect to a breadboard. Even when I don't connect it there is a level increase.
This is the code I used for the setting:
import RPi.GPIO as GPIO
import time
AmountMotionsDetected = 0
PIRinPin = 17
def setup_gpio():
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIRinPin, GPIO.IN, GPIO.PUD_DOWN) # Set pin to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(PIRinPin, GPIO.RISING, callback = MotionDetectedCallback, bouncetime = 300)
def MotionDetectedCallback(channel):
global AmountMotionsDetected
print ("We have {} motions detected ".format(str(AmountMotionsDetected)))
AmountMotionsDetected = AmountMotionsDetected + 1
def endprogram():
GPIO.cleanup()
if __name__ == '__main__':
setup_gpio()
try:
while True:
button_state = GPIO.input(PIRinPin)
if button_state == GPIO.HIGH:
print ("HIGH")
else:
print ("LOW")
time.sleep(1)
except KeyboardInterrupt:
print ('keyboard interrupt detected')
endprogram()
Here is a photo of the setup (I use the orange jumoing wire to simulate the switch)
Update 2: I found a hint why there might be a problem. Therefor I had to modify the script a little bit as you can see above. When I now run the script and connect/disconnect the jumping wire periodically, I see that sometimes (but not always) there is false "rising" detection when I disconnect the wire. This seems to be a common issue since 2014?
- https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=59457&sid=6c90fcb0b8841d3ee0b57496d0af161d&start=25
- How does python GPIO bouncetime parameter work?
- How does python GPIO bouncetime parameter work?
If that's true, that function would be useless.