Category Archives: Scripting

Make some noise

Adding a push-button and connecting it up the a GPIO port is as simple as writing a tiny pyhon script to initialize the IO port for input and utilizing a built-in pull-Up button. The Raspberry PI button is then capable to act as a Doorbell button.

After I connected the loudspeaker to the USB soundcard and soldered the USB connection from the Raspberry PI to the sound card I was then able to use alsaconfig to select it and then use:

speaker-test -c2 -twav

to actually hear some audio through the soundcard.

External USB sound card
External USB sound card

Now the next step was to connect GPIO port 17 to GND and use the built in bull-down-resistors to detect a button-press event.

Here is the push_button.py – python script which I use.

#!/usr/bin/python2.7

import time
import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def my_callback(channel):
    subprocess.call ( "/root/bin/ding_dong.sh" )

# add falling edge detection on a channel, ignoring further edges for 400ms for switch bounce handling
GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=400)

while True:
    # Loop once a day
    time.sleep(24*60*60)

GPIO.cleanup()

This script is straightforward and is simply waiting for a button press on GPIO pin 17 ( to GND ). The final loop is a forever loop with a one-day delay which will help keep the responsiveness of the Raspberry PI button at a good level. If during this time a button press was detected, then the ding_don.sh – script, which in turn plays a sound on the speaker.

I plugged it into /root/bin/push_button.py and made sure it is executable

sudo mkdir /root/bin
sudo vi /root/bin/push_button.py
sudo chmod +x /root/bin/push_button.py

Below is the pin-out of the Raspberry PI Zero W.

GPIO Ports
Raspberry PI Zero W
GPIO ports

In this case I connected GPIO17 ( orange wire ) and GND ( blue wire ) ( PIN 11, and PIN 14 ) to the doorbell button.

Video Doorbell
Video Doorbell wired up

Instead of packing all the functionality into one python script I decided to have python call a simple bash – script to do the actual work. Here is the ding_dong.sh script, which is called by the python script and also resides in /root/bin/

#!/bin/bash

mpg=`pidof mpg123`
if [ "x${mpg}" == "x" ];
then
  mpg123 /root/SoundHelix-Song-14.mp3 &
else
  killall -9 mpg123
fi
echo "Ding Dong"

sudo vi /root/bin/ding_dong.sh
sudo chmod +x /root/bin/ding_dong.sh

As the last step I wanted to have this script run all the time and to make sure it is started at startup of the PI :

[Unit]
Description=Push Button Service

[Service]
ExecStart=/root/bin/push_button.py
StandardOutput=null

[Install]
WantedBy=multi-user.target
Alias=ipush_button.service
sudo vi /lib/systemd/system/push_button.service
sudo systemctl enable push_button

The Raspberry PI button is now all set to act as the Doorbell button for my Video-Doorbell project.

Cheap $7,- external usb sound card

I bought the following external usb sound card from Amazon for $7,- to integrate into my Video Doorbell project.

 

External USB sound card
External USB sound card

So why did I chose to go this route. Simple, I was looking at some audio pHATs  for the Raspberry PI and found that they will cost about $4,- a pop, then of course you also need a microphone, and bang you are at around $9,- to $10,-. So paying $7,- for a audio I/O ( 7.1 sound card ) is a smart choice. Another thing I saw which won me over was that the setup is quit simple.

Note: This link above will point to the same sound card for only $4,-.

In order to do the setup I connected a micro-USB male to USB female adapter to my PI zero and made two small changes.
Please note though that I do intend to remove the USB plug all together and solder things together in the final setup.

1: Find the US sound card number ( in my case the USB card is card # 1 )

aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 8/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: Set [C-Media USB Headphone Set], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

2: create a file /etc/asound.conf with the following contents

pcm.!default {
        type hw
        card 1
}

ctl.!default {
        type hw
        card 1
}

3: and finally test the speaker :

  speaker-test -c2 -twav

This concluded the setup and testing. To use this external USB sound card in my project it had to fit tightly into the chassis which I built from scratch. So the USB plug had to come off and I had to solder some wires onto the card to connect t up to the Raspberry PI.

Video Doorbell assembly
external USB soundcard after removal of USB plug