DIY Raspberry Pi Zero “Clapper”

“Clap on! Clap off! The Clapper!”

This project will walk you through the creation of a DIY “Clapper”.  It uses the Adafruit MAX4466 Microphone Amp to detect when the claps occur, then sends an RF signal to RF-controlled power plugs.

The script could probably be modified to use a standard USB microphone, which would simplify the wiring significantly.  I may revisit that later.

Parts:

Wiring Diagram:

Python Code:

Putting it all together

  1. First, wire everything up the way I’ve illustrated in the wiring diagram above
  2. Get Raspbian Jessie (I used the Lite edition, but I presume the full version is fine) and load it on an SD Card
  3. Once in Raspbian, I recommend doing the following commands to get everything updated (a good practice in general on most new installations of Raspbian):
    • sudo apt-get update
    • sudo apt-get dist-upgrade --yes
    • sudo raspi-config  – Configure your localization settings here.  You will also need to enable SPI in Interfacing Options (older versions had it under Advanced).
    • sudo reboot  – when it boots back up, log back in
  4. Install Git, the Python dev libs, and pip (Python package manager):
    sudo apt-get install git python-dev python-pip
  5. Install spidev (SPI Python libs): sudo pip install spidev
  6. Install WiringPi:
    • Clone the wiringPi repository into a directory (like “src” subdir under your home, example: mkdir ~/src; cd ~/src ):
      git clone git://git.drogon.net/wiringPi
    • cd wiringPi
    • git pull origin
    • ./build
    • Instructions and details were stolen from Gordon Henderson’s blog.
  7. Install Tim Leland’s web-manager code (which includes a modified version of 433Utils for using the RF hardware).  Note, Tim’s code is a part of a web-based project that he maintains.  I’m not going into details on setting up the web server, but if you would like to have the option of controlling the plugs from a web app, check out Tim’s excellent blog post
    • sudo git clone https://github.com/timleland/rfoutlet.git /var/www/rfoutlet
  8. Figure out what RF codes are used by the power outlet remote control and receiver:
    • Run: sudo /var/www/rfoutlet/RFSniffer
    • Press each button on the remote control and take note of the 6-digit code as well as the pulse that shows on the screen – for both ON and OFF.
    • Note, you may need to hold the remote control RIGHT next to the receiver on your breadboard – nearly touching, it doesn’t have much range.
  9. Create the python script that will control the clapper and ties everything together.There are several ways you can accomplish this:
    • Download it straight to the Pi with the command:
      wget -O ~/clapper.py https://gist.githubusercontent.com/aplocher/e83fdc72e705c099936cc187cee74768/raw
      (NOTE #1 – that’s an uppercase -O, NOTE #2 – if you’re typing this out by hand, there’s a tinyurl you can use instead: http://tinyurl.com/bc-clapper)
    • Or, edit a new file and paste the contents into that file:
      • nano ~/clapper.py
      • Copy the script (above) and paste it into your terminal window
      • Press CTRL+o to SAVE, and hit ENTER
      • Press CTRL+x to EXIT
  10. Configure the Python script if necessary.  Lines 20 – 40 can be tweaked to adjust the RF codes, the path to the codesend bin, and sensitivity of the clap detection.
    1. nano ~/clapper.py
    2. Change any necessary config variables between lines 20 and 40
    3. When done, press CTRL+o to SAVE, and hit ENTER
    4. Press CTRL+x to EXIT
  11.  Test it by running:
    1. python ~/clapper.py -h – should show help info
    2. python ~/clapper.py --on – should activate the power (and exit)
    3. python ~/clapper.py --off – should deactivate the power (and exit)
    4. python ~/clapper.py – should listen for claps to toggle power on and off (will remain running)

Leave a Reply