Project tutorial
Enter the house like a Sith Lord

Enter the house like a Sith Lord © MIT

With a pair of Photons and a MP3 shield, I can have the Imperial March start playing in the house when I arrive home.

  • 20,553 views
  • 10 comments
  • 74 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

My daughter is a huge Star Wars fan (even though she hasn't seen any of the movies yet). Not surprisingly, she was all over it when I asked if she wanted to build a Star Wars themed project. I don't know how many times she has made me play the Imperial March (Darth Vader's theme song) when she is wearing her Darth Vader costume so that she can make a grand entrance into the room. So, we quickly settled on this project idea - automatically detect someone coming home and then trigger the playing of the Imperial March just before the person walks into the house.

This is a fairly simple project so it's well suited for beginners. I found it interesting because it's the first time I have used the publish / subscribe pattern to / from the cloud. It was also the first time I integrated different types of micro controllers into a single project. I love the Photon's simplicity and ease of use when it comes to creating a cloud connected solution. And you can't beat the variety and magnitude of add-on hardware available for the Arduino. Being able to put them together so easily enabled this project to come together quickly.


Step 1: Assemble the sensor

Time: 10 minutes

Parts: Photon, 220 Ohm resistor, LED, PIR motion sensor, breadboard, male / male jumper wires, USB micro B cable

Tools: N/A

First, connect the LED to the Photon. Mount your Photon into the breadboard. Then connect the resistor between a Photon's ground pin and an open row on the breadboard. Finally, connect the long leg of the LED to a digital PIN on the Photon (I used pin D0) and the short leg of the LED to the open row to which the resistor is connected. Frankly, the LED is optional but I use it to indicate the state of the sensor as the PIR sensor itself provides no indication of when motion is detected nor does the Photon provide any indication when it publishes the event to the cloud.

Next, connect the PIR sensor to the Photon. Mount the PIR sensor to the breadboard - I use right angled female header pins to connect the PIR sensor to the breadboard as this allows me to "secure" the PIR sensor to the breadboard vertically. (If you don't have right angle header pins, then use some double sided tape or some sticky tack to mount your PIR sensor to the table or whatever you are mounting it to and use female / male jumper wires straight from the PIR sensor's male pins to the breadboard.) Use jumper wires to connect the PIR's GND to a Photon's GND pin, the PIR's 5V pin to the Photon's Vin pin, and the PIR's signal pin to a digital pin on the Photon (I used pin D7).

Double check the data sheet for your sensor to make sure you correctly identify the sensor's 5V, GND, and signal pin as their order varies by manufacturer. Even though my PIR sensor operates at 5 volts, it's signal pin operates at only 3.3V which is why it can be connected directly to a digital pin on the Photon. Again, double check the data sheet for your sensor to ensure that the signal's voltage is 3.3V.

Power up the assembly via the Photon's USB Micro B connector to either a PC or a wall outlet. Open up the Particle IDE and paste in sensor code provided below. Then flash the code to the Photon.

Now, if you wave your hand in front of the sensor, the LED should light up. Also, you can check your logs on the Particle Dashboard to see that the event was published.

Step 2: Assemble the trigger

Time: 10 minutes

Parts: Photon, 220 Ohm resistor, LED, breadboard, male / female jumper wires

Tools: NA

Connect the LED and resistor to the photon just as you did in the prior step. Again, the LED is optional but I find it useful to indicate when an event is received from the Particle cloud. Next, connect the male end of the jumper wires to Photon's Vin, GND, and a digital pin (I used pin D7).  The digital pin from the Photon will connect to one of the Music Maker's GPIO pins to trigger the music. The Photon's Vin and GND connections will be used to power the Photon from the Arduino.

But first, before connecting to the Arduino, power up the assembly via the Photon's USB Micro B connector to either a PC or a wall outlet. Open up the Particle IDE and paste in trigger code provided below. Then flash the code to the Photon - ensure you have selected the correct device to flash the code to now that you have two devices connected to the Particle IDE.



Step 3: Assemble the Arduino and Music Maker shield

Time: 20 minutes

Parts: Arduino Uno, Adafruit Music Maker shield, Photon, 220 Ohm resistor, LED, mini breadboard, jumper wires, USB cable

Tools: soldering iron

Header pins are included with Music Maker shield, however you must solder them. In addition to the header pins to connect the shield to the Arduino, I added male header pins on top of the shield for GPIO and power connections from the Photon. (If I had female header pins, I would have use them to so that I would only need male / male jumper wires. But, I only had male header pins on hand (in addition to male / female jumper wires) so I went that route instead.)

Connect the jumper wire from the Photon's digital pin to a GPIO pin on the shield (I used pin 7 on the shield). Connect the jumper from the Photon's Vin to the 5v pin on the shield and the GND jumper to the shield's GND pin.

Finally, connect your speaker(s) to the terminal block(s) on the shield - there is one terminal block for a right speaker and one for a left speaker. I just connected one speaker - I salvaged a small speaker from one of my daughter's broken toys before we threw it out.

Connect the Arduino to your development machine, paste the Arduino sketch below into the Arduino IDE, and upload it. 

That's it. Go walk in front of the sensor and sit back and wait for the music to start.

About the Code

As I mentioned earlier, this was the first time I used the publish / subscribe pattern. It was quite easy to do with the Photon. In the sensor code, when the PIR sensor's signal pin goes HIGH, I turn on the LED and publish an event to the Particle cloud. Publishing an event just requires one line:

published = Particle.publish("motion-detected", NULL, 60, PRIVATE);

Initially, I didn't make the publish private - I didn't care as there is no personally identifiable information. But, when I went to subscribe to the event, I was getting a lot of false alarms - apparently, a lot of other users are publishing "motion" oriented events. So, I switched the subscribe call to only subscribe to events from my devices by adding the "MY_DEVICES" parameter:

Particle.subscribe("motion", soundAlarm, MY_DEVICES);

And in order for the subscribe function to limit itself only to my devices, I had to make the publish private.

The trigger code simply calls the event handler - soundAlarm - when the motion event occurs. All the event handler does is turn on the LED and turn the GPIO pin on the Music Maker shield HIGH. The Arduino sketch detects the GPIO pin being HIGH and then waits 60 seconds (for me to park my car, gather my things, and enter the house) before playing the MP3 file on the SD card.


Deploy the Project

I setup the sensor in the garage on the shelves right next to where I park my car. I placed the trigger and the Arduino with Music Maker shield and speaker on the kitchen counter as my daughter is usually at the kitchen table when I get home. Both locations have wall outlets nearby which made it easy to power both parts of the project.

Code

Sensor CodeC/C++
Sample code for the Photon which publishes an event to the Particle cloud whenever motion is detected.
// define the LED on pin D0
int led = D0;

// define the PIR sensor input on pin D6
int pir = D6;

// track whether or not the publish was successful
bool published;

void setup()
{
    // set the LED pin as output
    pinMode(led, OUTPUT);
    
    // set the PIR sensor input pin as input
    pinMode(pir, INPUT);
    
    // ensure the LED is off
    digitalWrite(led, LOW);
}

void loop()
{
    if (digitalRead(pir) == HIGH)
    // if motion is detected
    {
        // turn on the LED as an indicator that I was detected
        digitalWrite(led, HIGH);
        published = Particle.publish("motion-detected", NULL, 60, PRIVATE);
        if (!published)
        // if the publish was unsuccessful
        {
            // flash the LED 10 times as an indicator of the publish failure
            for (int i = 0; i < 10; i++)
            {
                digitalWrite(led, LOW);
                delay(500);
                digitalWrite(led, HIGH);
                delay(500);
            }
        }
    }
    else
        // if no motion is detected, turn off the LED
        digitalWrite(led, LOW);
    
    // wait 2 seconds before taking another reading    
    delay(2000);
}
Trigger CodeC/C++
Sample code for the Photon which subscribes to motion events in the Particle cloud published by my sensor and then triggers the Music Maker shield to play the MP3.
// define the LED on pin D0
int led = D0;

// define the trigger for the Arduino on pin D7
int trigger = D7;

void soundAlarm(const char *event, const char *data)
{
    // when the motion event has been triggered
    // turn on the LED
    digitalWrite(led, HIGH);
    
    // turn on the trigger
    digitalWrite(trigger, HIGH);
    
    // wait two seconds
    delay(2000);
    
    // turn off the trigger
    digitalWrite(trigger, LOW);

    // turn off the LED
    digitalWrite(led, LOW);
}

void setup()
{
    // subscribe to motion events from my devices only
    // define soundAlarm as the event handler
    Particle.subscribe("motion", soundAlarm, MY_DEVICES);
    
    // set the LED pin as output
    pinMode(led, OUTPUT);

    // set the trigger pin as output
    pinMode(trigger, OUTPUT);
}

void loop()
{

}
Arduino and Music Maker shield codeArduino
Sample code for the Arduino to play the MP3 via the Music Maker shield based on the trigger from the connected Photon.
/************************************************************ 
 This sketch plays a MP3 from the SD card using the
 Adafruitt Music Maker shield when triggered by a
 Photon which is connected to on of the shield's GPIO pins.
 
 Thanks to Adafruit for the well written tutorial and library.
 https://learn.adafruit.com/adafruit-music-maker-shield-vs1053-mp3-wav-wave-ogg-vorbis-player/pinouts
 *************************************************************/

// include SPI, Music Maker and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// Define the pins used for the music maker shield
#define SHIELD_RESET  -1     // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define CARDCS        4      // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ          3      // VS1053 Data request, ideally an Interrupt pin

// define the music player
Adafruit_VS1053_FilePlayer musicPlayer = 
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  
// define the GPIO pin on which the photon connects
int photonPin = 7;

void setup()
{
  // initialise the music player
  musicPlayer.begin();
  
  // initialise the SD card
  SD.begin(CARDCS);
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(1,1);

  // If DREQ is on an interrupt pin (on uno, #2 or #3)
  // we can do background audio playing but it is not
  // being used in this initial phase of the project
  // initialize the DREQ interrupt
  // musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);

  // initialize the GPIO pin to which the photon is connected as an input
  musicPlayer.GPIO_pinMode(photonPin, INPUT);
}

void loop()
{
  if (musicPlayer.GPIO_digitalRead(photonPin) == HIGH)
  {
    // wait 60 seconds before playing so that I am
    // walking into the house just after it starts
    delay(60000);
    
    // Play the file, don't return until complete
    musicPlayer.playFullFile("track001.mp3");
  }

  delay(500);
}

Schematics

Sensor wiring diagram
sensor.fzz
Trigger wiring diagram
trigger.fzz

Comments

Similar projects you might like

Use the Force... Or your Brainwaves?

Project tutorial by Tamas Imets

  • 29,213 views
  • 18 comments
  • 101 respects

Home Smart Home

Project tutorial by Syed Sanoor

  • 68,539 views
  • 10 comments
  • 193 respects

Room temperature sensor with display on Mobile

Project showcase by Kaustubh Agarwal

  • 23,931 views
  • 8 comments
  • 54 respects

Colors of Photons

Project showcase by Eva Konya and Janos Magyar

  • 3,908 views
  • 4 comments
  • 19 respects
Add projectSign up / Login