Prasantha Jayakody
Published © MIT

Enter the house like a Sith Lord

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

BeginnerFull instructions provided28,038
Enter the house like a Sith Lord

Things used in this project

Hardware components

Photon
Particle Photon
×2
Arduino UNO
Arduino UNO
×1
Adafruit Music Maker MP3 Shield
×1
Adafruit PIR Motion Sensor
×1
LED (generic)
LED (generic)
×2
Resistor 220 ohm
Resistor 220 ohm
×2
jumper wires
×1
Speaker: 3W, 4 ohms
Speaker: 3W, 4 ohms
×1
Breadboard (generic)
Breadboard (generic)
×2
USB Micro B Wall Charger
×1
USB-A to B Cable
USB-A to B Cable
×1
Male-Header 36 Position 1 Row- Long (0.1")
Male-Header 36 Position 1 Row- Long (0.1")
×1

Software apps and online services

Particle cloud

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Sensor wiring diagram

Trigger wiring diagram

Code

Sensor Code

C/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 Code

C/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 code

Arduino
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);
}

Credits

Prasantha Jayakody

Prasantha Jayakody

11 projects • 147 followers
Hobbyist Maker always looking to learn new things.

Comments