Extend Your Program to Add an Alarm Sound Effect

Add extra code to your Python program to trigger an alarm sound when motion is detected.

Step 1

Make sure you have the alarm.wav file on your Raspberry Pi desktop.

Step 2

Add this code above the first line of your program:
import os

Step 3

Add one more line at the end of your code so it looks like this:

import os from gpizero import MotionSensor pir = MotionSensor(4) while True: if pir.motion_detected: print("Motion detected!") os.system('sudo aplay /home/pi/Desktop/alarm.wav')

Now your Raspberry Pi will sound an alarm if motion is detected. 

Remember you can press 'Ctrl+F6' to exit and stop the alarm.

Step 4

Test your Raspberry Pi alarm in different parts of the room.

Try and work out how sensitive your PIR sensor is and where it works best.