Generally speaking, motion sensor is made of thermoelectric sensor, and it can detect body motion. The motion sensor module generally uses the BISS0001 ("Micro Power PIR Motion Detector IC") to convert the analog signal from the sensor to digital output.
Experimental principle of this project is that, when the detection of the human body motion, GPIO output get high level and light is on; else, GPIO output would get low level and LED would be off. In this project, Python Language will help us achieve this experiment. Enjoy Raspberry Pi Python programming tour with us.
|Raspberry Pi board||x1|
|Jumper wire||centain amount|
|T-type GPIO Extension Board40 Pin Ribbon Cable||optional|
Motion sensor Schematic
Motion sensor Interface Layout
The two potentiometer can adjust delay time and sensitivity, as the follow picture:
(1)Before Python programming, Install GPIO library and Python library file in raspberry pi:
open terminal, update the apt-get software installation package list (Note: Network must be connected), and then enter installation command to install the raspberry gpio-python package.
Follow the next commands to complete installation, after entering each commands in terminal, press "enter" to complete:
a) update software list command: sudo apt-get update
b) install python command: sudo apt-get install python-dev
c) install python-pip command (python-pip is a manager for python software package): sudo apt-get install python-pip
d) Install rpi.gpio with pip command: sudo pip install rpi.gpio
e) testing Command: sudo python
After all steps, you will see the following photo. It means it is successful to install GPIO library and Python library file. You can begin to program this project via Python
You can compile code not only through connecting monitor with your Pi, also through SSH. You can choose two methods to compile the project code.
A. File editor to compile code:
1)create a new file "led.py" in any direction (such as "/home/pi/"）: enter the command sudo touch led.py and then press enter
2)Open file "led.py": enter the command sudo nano led.py and then press enter
3)paste the following code in the file "led.py"
#turns on and off a light emitting diode(LED) depending on motion sensor import RPi.GPIO as GPIO #importing the RPi.GPIO module import time #importing the time module GPIO.cleanup() #to clean up at the end of your script led_pin = 37 #select the pin for the LED motion_pin = 35 #select the pin for the motion sensor def init(): GPIO.setmode(GPIO.BOARD) #to specify which pin numbering system GPIO.setwarnings(False) GPIO.setup(led_pin,GPIO.OUT) #declare the led_pin as an output GPIO.setup(motion_pin,GPIO.IN,pull_up_down=GPIO.PUD_UP) #declare the motion_pin as an input print("-----------------------------------------------------------------------") def main(): while True: value=GPIO.input(motion_pin) if value!=0: #to read the value of a GPIO pin GPIO.output(led_pin,GPIO.HIGH) #turn on led time.sleep(2) #delay 2ms print "LED on" #print information else: GPIO.output(led_pin,GPIO.LOW) #turn off led time.sleep(2) #delay 2ms print "LED off" #print information init() main() GPIO.cleanup()
4) after compile the project, press "Ctrl" + "X" to save the code, enter "Y" to confirm saving, and press "enter" to exit the file editor
B. Command to compile code
Enter the command wget http://osoyoo.com/driver/led.py, and press "enter" to complete compiling code.
Enter the following command: sudo python ./led.py, and press "enter" to run the project. When detection body motion, the LED turns on, else, the LED turns off, and the monitor shows as the photo:
DownLoad Url osoyoo.com