Introduction: Using a Sound Sensor With a Raspberry Pi to Control Your Philips Hue Lights

Use case: once my kitchen door is closed I'm not able to hear my doorbell - annoying when you throw a party with many guests!

Luckily, one guy at the partycame up with the excellent idea to "hook up some kind of microphone and ring a sound if the doorell rings".

The idea seemed nice, so we brainstormed the possiblities over a couple of beers with my tech-savy friends.
It hat to be a bit more flashy than just a doorbell sound, so instead of using my speakers we decided to use my Philips Hue lights instead to create a cool light effect to announce new guests!

All of the lights were set up in the kitchen already, so we went for a blink the lights if the doorbell rings approach.

Step 1: Hardware Requirements

  1. a Raspberry Pi 2 to run the software / scripts (every Raspberry generation should be feasible to implement this)
  2. a cheap sound sensor for a couple of bucks from ebay/amazon/etc - e.g. here or here
  3. some female-to-femalejumper wires to connect the sound sensor with the Pi
  4. Philipps Hue lights, you can go for every set up you want - I went for
    • Philips Friends of hue - LivingColors Bloom
    • Philips Hue Go
    • Philips hue - LED
    • one of the above needs to be the starter kit / you will need a bridge to control the lights after all

Step 2: Software Requirements

In my setup I used a Raspberry Pi 2 with Raspbian Wheezy with a few python libraries:

  1. Raspbian
  2. Python package python-dev
  3. Python library requests
  4. Python library qhue from Quentin Stafford-Fraser
  5. Python library RPI.GPIO

Step 3: Initial Setup

This will outline the main steps of the set up, as you can see it's pretty simple and should be straight-forward to understand.

  1. Connect the sound sensor to the Raspberry Pi via female-to-female jumpers, using 3 jumpers for:
  2. power on your Raspberry Pi
    • If your sound sensor has indicator LEDs, make sure it triggers when creating noise to test the basic sound sensor capabilities
  3. install Raspbian image (if not done already)
  4. setup up and connect via ssh
  5. install necessary software (python-dev) via

    apt-get install python-dev
  6. and install the python libraries requests, qhue and RPI.GPIO via

    pip install requests
    git clone https://github.com/quentinsf/qhue/
    cd qhue
    pip install RPI.GPIO

Step 4: Set Up the Script

Let's open a new script named sensor.py and put in the below code.

nano sensor.py

Content:

import time
import RPi.GPIO as GPIO from qhue import Bridge

GPIO.setmode(GPIO.BOARD) # use board pin numbers # define pin #7 as input pin pin = 7 GPIO.setup(pin, GPIO.IN)

b = Bridge("192.168.1.30", 'e254339152304b714add57d14a8fdbb') groups = b.groups # as groups are handy, I will contorll all

while 1: if GPIO.input(pin) == GPIO.LOW: i = 3 # number of iterations for l in range(1,i+1): # this is one of the temporary effects, see official docs # at http://www.developers.meethue.com/documentation/core-concepts b.groups[0].action(alert="select") #group 0 = all lights time.sleep(1) time.sleep(10)

Step 5: Fire It Up!

Just run the script via

sudo python sensor.py

And trigger the sound input - if all went well, your lights should blink 3 times..

Congratulations, you just created a listener script to listen to your doorbell / any sound you wish!

Step 6: Set Up Autostart for Your Listener Script

We will be utilizing the Linux rc.local functionality and create a new shell script that will run the python part we just created in the previous step:

nano /home/pi/qhue/sensor.sh

Content:

#!/bin/sh<br># sensor.sh

sudo python /home/pi/qhue/sensor.py

Now make this script executable by performing:

chmod +x sensor.sh

Open up the /etc/rc.local file

nano /etc/rc.local

and enter the following line before exit 0 to run the script at startup

sudo /home/pi/qhue/sensor.sh

Save the file and reboot your Raspberry Pi via

sudo shutdown -r now

Step 7: Summary / To-Do

This instructable outlines the very basics, I will add a few pictures and more details once I got the time to fine-tune this.

It is worth noticing that the distance to your sound source shouldn't be too far away, neither should you place the Pi in a spot where you would expect regular noise.

Hope you enjoyed the guide, any suggestions? Feel free to comment ;)

Cheers

Comments

author
Leizara (author)2017-01-27

I tried to do this but i'm having problems with the script.

the if condition if GPIO.input(pin) == GPIO.LOW: is always true! so it's detecting sound all the time i already adjusted the sensitivity but nothing happens it keeps throwing alerts

can you pls help me with this?

author
DavidB309 (author)Leizara2017-01-30

Hey there,

you could try to test if the sound sensor works first.
In the middle of the code try something like:

while 1:

Print GPIO.input(pin)

To print the status of the sensor. Next trigger the sensor witrh a clap or something and see if the status changes. Also make sure you have connected the pins correctly.

If found this tutortial, they have better pictures that show you how to connect the sensor to the GPIO from the Pi. Note that they are using pin #12 instead of #7 as I do. You could use their setup and change the pin number in my code to see if it works? :)
Here's the instructable: https://www.instructables.com/id/DIY-alternative-to-smart-door-bell-when-someone-kn/

Cheers

David

author
ilverz (author)2017-01-18

Really nice!
But I have a question, has the sensor an digital output? Or is just analog?
In the case that is just analog, is it safe to connect it directly to the raspberry?

author
DavidB309 (author)ilverz2017-01-18

Hey there! The sound sensor I've used is analog.

I'm no expert here but I guess you should NOT hook it up directly. My setup is up and running since 14 months now though :)

Cheers!

author
MarcelS21 (author)2016-03-17

Can i use it with my Milight bridge ? Or only coded for Hue?

author
DavidB309 (author)MarcelS212016-03-19

This should work with any hardware that has an API you can access.

I could think of a setup with FHEM (http://fhem.de/fhem.html ) that most definitely will be able to control your lights.

author
JeevithaG (author)2016-02-22

nice

author
tomatoskins (author)2015-11-06

This is so cool! I love finding ways to control internet connected things!