a guest Jul 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import RPi.GPIO as GPIO             #importing the RPi.GPIO module
  2. import time                        #importing the time module
  3. GPIO.cleanup()                     #to clean up at the end of your script
  4. led_pin = 37                       #select the pin for the LED
  5. motion_pin = 35                    #select the pin for the motion sensor
  6. def init():
  7.   GPIO.setmode(GPIO.BOARD)         #to specify which pin numbering system
  8.   GPIO.setwarnings(False)  
  9.   GPIO.setup(led_pin,GPIO.OUT)                             #declare the led_pin as an output
  10.   GPIO.setup(motion_pin,GPIO.IN,pull_up_down=GPIO.PUD_UP)  #declare the motion_pin as an input
  11.   print("-----------------------------------------------------------------------")  
  13. def main():
  14.   while True:
  15.     value=GPIO.input(motion_pin)  
  16.     if value!=0:                             #to read the value of a GPIO pin
  17.       GPIO.output(led_pin,GPIO.HIGH)                #turn on led
  18.       time.sleep(2)        #delay 2ms
  19.       print "LED on"                           #print information
  20.     else:
  21.       GPIO.output(led_pin,GPIO.LOW)                #turn off led
  22.       time.sleep(2)       #delay 2ms
  23.       print "LED off"                         #print information
  25. init()
  26. main()
  27. GPIO.cleanup()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand