Section 6 Testing of the Motion Sensor motionsensor.py Next, let’s write a simple Python script that will print out a message whenever motion is being detected by the PIR sensor. Task a) Change directory to CA2 cd ~/CA2 b) Create a python script motionsensor.py with the code sudo nano motionsensor.py c) from gpiozero import Buzzer from gpiozero import MotionSensor from time import time from time import pir = MotionSensor(26, sample_rate=5,queue_len=1) bz = Buzzer(5) while True: bz.off() old_time = time() pir.wait_for_motion() new_time = time() if new_time - old_time > 1: print("Motion detected after {:.2f} seconds".format(new_time- old_time)) bz.on() sleep(1) bz.off() old_time = new_time pir.wait_for_no_motion() new_time = time() print("Room is quiet now...") Finally, let’s run our Python program and observe how our PIR sensor prints out the “motion detected” message whenever it detects motion. Run the program
Section 6 Testing of the Motion Sensor motionsensor.py Next, let’s write a simple Python script that will print out a message whenever motion is being detected by the PIR sensor. Task a) Change directory to CA2 cd ~/CA2 b) Create a python script motionsensor.py with the code sudo nano motionsensor.py c) from gpiozero import Buzzer from gpiozero import MotionSensor from time import time from time import pir = MotionSensor(26, sample_rate=5,queue_len=1) bz = Buzzer(5) while True: bz.off() old_time = time() pir.wait_for_motion() new_time = time() if new_time - old_time > 1: print("Motion detected after {:.2f} seconds".format(new_time- old_time)) bz.on() sleep(1) bz.off() old_time = new_time pir.wait_for_no_motion() new_time = time() print("Room is quiet now...") Finally, let’s run our Python program and observe how our PIR sensor prints out the “motion detected” message whenever it detects motion. Run the program
iot_ca2_documentation_G6jzfAS5CQ.docx