oliver
Posts: 26
Joined: Sun Mar 30, 2014 4:53 pm

motionn sensor turn led on

Tue Apr 15, 2014 2:05 pm

hi
i have been trying to make an LED turn on when i move but i dont know what is wrong with the code i am very new to python and am only 12 please help

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
GPIO.setup(11, GPIO.OUT)
GPIO.output (11, False)

try:
    print "PIR Module Test (CTRL+C to exit)"
    time.sleep(2)
    print "Ready"

    while True:
        if GPIO.input(PIR_PIN):
            print "Motion Detected!"
            GPIO.output(11, True)
        time.sleep(1)

except KeyboardInterrupt:
    print "    Quit"
    GPIO.cleanup()


thanks
olly :)

User avatar
paddyg
Posts: 2554
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: motionn sensor turn led on

Tue Apr 15, 2014 4:05 pm

Olly,

If I was trying to do this and it didn't do what I wanted I would try to find which bit of the system wasn't doing what I wanted. First I would check that I could turn the led on and off every second. Then I would test what was being read from pin 7 by just having a print(GPIO.input(PIR_PIN)) in the loop and no if statement.

Is your movement detector set up correctly to normally give 0V then 3.3V when it sees movement?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
joan
Posts: 14959
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: motionn sensor turn led on

Tue Apr 15, 2014 4:09 pm

There are at least three ways of referring to the gpios. At a guess you are using the pin numbers, P1-7 and P1-11. The code is set up to use the Broadcom numbers (where P1-7 is 4 and P1-11 is 17).

See http://elinux.org/RPi_Low-level_peripherals

oliver
Posts: 26
Joined: Sun Mar 30, 2014 4:53 pm

Re: motionn sensor turn led on

Wed Apr 16, 2014 5:58 am

thanks :D

Return to “Python”