0

I folowing this tutorial https://bhavyanshu.me/tutorials/raspberrypi-to-detect-movement-using-pir-motion-sensor-hc-sr501/11/21/2014/

and a little bit simplified my code.

#!/usr/bin/env python

# Info - This program is for PIR sensor. Contionusly checks for state, 
#        set LED if movement detected and plays wav file on detection.

import RPi.GPIO as GPIO
import time


PIR = 7     # On-board pin number 7 (GPIO04)

state = False
val = False

GPIO.setmode(GPIO.BOARD)    # Change this if using GPIO numbering
GPIO.setup(PIR, GPIO.IN)    # Set PIR as input

try:
    while True:
        val = GPIO.input(PIR)       # read input value
        if (val == True):       # check if the input is HIGH
            print("Hi there!")
            time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()

I also have wiringpi library, so when i do gpio readall I see that pin 7 gets 0 or 1 dependes on sensor data. but when i run python code, it just gets 1 all the time, and prints "Hi there" all the time. Any suggestions?

  • How do you know low is not being detected? Why not print something out if val is not True? – joan Jul 6 '15 at 19:44
2

Even though the PIR device should assert a voltage or ground depending on the state of the device, you should try to add pull_up_down=GPIO.PUD_DOWN to your GPIO.setup call as does one of the examples I found using Python.

In fact, the C code you link to appears to also do this (although the C code sets PID_UP):

bcm2835_gpio_set_pud(SENSOR, BCM2835_GPIO_PUD_UP);

See this and this for more information.

import RPi.GPIO as GPIO
import time

sensor = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

while True:
    time.sleep(0.1)
    previous_state = current_state
    current_state = GPIO.input(sensor)
    if current_state != previous_state:
        new_state = "HIGH" if current_state else "LOW"
        print("GPIO pin %s is %s" % (sensor, new_state))

Reading more on this you may need to set GPIO.PUD_UP.

This this comment for more information on when to use GPIO.PUD_UP versus GPIO.PUD_DOWN:

by joan » Thu Sep 18, 2014 7:07 am An input gpio will float between 0 and 1 if it's not connected to a voltage.

The pull-up/downs supply that voltage so that the gpio will have a defined value UNTIL overridden by a stronger force.

You should set a pull-down (to 0) when you expect the stronger force to pull it up to 1.

You should set a pull-up (to 1) when you expect the stronger force to pull it down to 0.

Otherwise the gpio will not change state and you'll never know about the external event.

  • I do not see that adding a pull-up or down will make any difference. The PIR will be driving the line high when an object is detected and low when nothing is seen, it will not be floating. – joan Jul 6 '15 at 21:29
  • If it's not connected to a voltage. The PIR is supplying a voltage. – joan Jul 6 '15 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.