User Guide
Table Of Contents
194
• In LXTerminal, type the following command and then
press Enter:
sudo python3 Doorbell/doorbell.py
Press the switch to hear the audio file play. If you hold the button
down, the script repeatedly plays the sound.
To stop the script from looping the sound, you can make sure
that the button is released before the script checks for another
press:
• Add the following statements to your script file, in the
Ring() function, after the call to os.system():
while RPi.GPIO.input(2) == RPi.GPIO.LOW:
pass
The while loop continually runs the pass instruction (which does
nothing) until the button is released and GPIO2 goes high.
Starting the Doorbell Script Automatically
If you want your doorbell script to run when the Raspberry Pi
starts up, you can use a feature built-in to Linux called cron. Cron
is a way of starting programs in the background.
First, create a shell script that starts your Python program:
1. In LXTerminal, type the following command and
then press Enter:
nano Doorbell/doorbell.sh
2. Add the following text to the file:
#!/bin/sh
cd /home/pi/Doorbell
sudo amixer cset numid=3 1
sudo python3 doorbell.py
3. Press Ctrl + O, and then press Enter.
4. Press Ctrl + X.
5. Type the following command and then press Enter:
sudo crontab -e