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

Published

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

Share

    Recommendations

    • Oil Contest

      Oil Contest
    • Clocks Contest

      Clocks Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    14 Discussions

    Be careful if you buy a 5V DC Module, it will output 5V and your raspberry wants to receive only 3.3V.

    It is safe to use a 'voltage divider' with only 2 resistors and one more wire:

    https://www.circuitlab.com/circuit/watfup/raspberr...

    different values of resistors are ok (and even better) as long as:

    R2/(R1+R2) = 3.3V/5V = 0.66

    (I'm not an expert, please check info on this subject)

    0
    user
    ilverz

    1 year ago

    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?

    2 replies

    I believe D0 is digital and A0 is analog... and both are outputs

    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!

    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?

    2 replies

    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

    Hey,

    I'm just beginning with the GPIO so I'm not an expert at all, but I had the same issue with a simple click button and I can confirm it is related to "floating pins" and the need of pull up or pull down resistors.

    See the beginning "background" part, it is pretty clear:

    https://www.cl.cam.ac.uk/projects/raspberrypi/tuto...

    The tutorial is very nice to read though, thx

    hello this is nice software . is it possible to use this sound card to recognize just alarm and performe some jobs like take picture from camera in case of alarm sound . please guide me . tnx

    2 replies

    Hello,

    for the sound part it is not possible to analyze sound, no.

    It would need an analog module and use the analog pin out from this sensor.

    Sure saeed, that should work too!


    In step 4 you could try to also import picamera library, replace the bottom part with the following:

    while 1:
    if GPIO.input(pin) == GPIO.LOW:
    filename = now.strftime("%Y-%m-%d %H-%M")
    camera = picamera.PiCamera()
    camera.capture('image.jpg')

    See some example code here:

    https://www.raspberrypi.org/blog/picamera-pure-pyt...

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

    1 reply

    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.

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