Skip to content

Instantly share code, notes, and snippets.

@topshed
Last active January 11, 2022 08:49
Show Gist options
  • Save topshed/234dc7144c031d7914c71a1271be2a76 to your computer and use it in GitHub Desktop.
Save topshed/234dc7144c031d7914c71a1271be2a76 to your computer and use it in GitHub Desktop.
PIR example

Reading the value attribute from a gpiozero MotionSensor

This example shows how to use the .value attribute. Note that it is not a function and so does not use paranethseses. Here pir.value is treated in the same way as you would a variable.

from gpiozero import MotionSensor
from time import sleep

pir = MotionSensor(12)

while True:
    sleep(0.2)
    if pir.value == 1:
        print('motion detected')

This is functionally the same as:

from gpiozero import MotionSensor
from time import sleep

pir = MotionSensor(12)

while True:
    sleep(0.2)
    if pir.motion_detected:
        print('motion detected')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment