Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Nicholas FrittsBen Saunders
Published

Motion Activated Music Player

This project plays music when motion is detected.

IntermediateFull instructions provided5 hours6,782
Motion Activated Music Player

Things used in this project

Hardware components

Photon
Particle Photon
×2
SparkFun Speaker - PCB Mount
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
LED (generic)
LED (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Photon 1: Motion Detection

A particle photon is connected to an resistor, LED, and PIR motion sensor.

Photon 2: Speaker

Particle Photon 2 is attached to a speaker using an analog pin and ground.

Code

Photon 1: PIR sensor Code

Arduino
This code publishes an event when motion is detected by the PIR sensor. This code includes graphing capabilities through ThingSpeak using a webhook.
// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

// LED on pin D0
int led = D0;

// PIR on D7
int pir = D7;

// tracks if the publish was successful
bool published;

//initializes count
int count=0;

void setup()
{
    // sets the LED pin as output
    pinMode(led, OUTPUT);
    
    // sets the PIR sensor input pin as input
    pinMode(pir, INPUT);
    
    // ensures the LED is off
    digitalWrite(led, LOW);
    
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/MotionDetected", myHandler);
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
}


void loop()
{
    if (digitalRead(pir) == HIGH)
    // if motion is detected
    {
        // turn on the LED to indicate that motion was detected
        digitalWrite(led, HIGH);
        published = Particle.publish("MakePhotonsGreatAgain", "true");
        count++;
        Particle.publish("MotionDetected", String(count), 60);
        delay(10000);
        // Trigger the integration
        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);
        
delay(2000);
}

Photon 2: Speaker Code

Arduino
This code subscribes photon 2 to photon 1. When an event is detected the speaker plays the tones at set frequencies and duration.
int speakerPin = D1;
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};
int noteDurations[] = {4,8,8,4,4,4,4,4 };

void setup()
{                
    pinMode(D1, OUTPUT);  // buzzer on pin D1
    analogWrite(D1,0);
    
Particle.subscribe("MakePhotonsGreatAgain", myHandler);
}

void myHandler(const char *event, const char *data)
{
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(speakerPin, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(speakerPin);
  }
}

Extra Photon 2: Jingle Bells Speaker Code

Arduino
This code plays Jingle Bells to welcome your holiday guests and ignite the Christmas spirit.
int speakerPin = D1;
int melody[] = {650,650,650,0,650,650,650,0,650,750,612,630,650,0,0,0,690,690,690,690,690,650,650,650,650,630,630,650,630,0,750,0,650,650,650,0,650,650,650,0,650,750,612,630,650,0,0,0,690,690,690,690,690,650,650,650,750,750,690,630,611};
int noteDurations[] = {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4};

void setup()
{                
    pinMode(D1, OUTPUT);  // buzzer on pin D1
    analogWrite(D1,0);
    
Particle.subscribe("MakePhotonsGreatAgain", myHandler);
}

void myHandler(const char *event, const char *data)
{
  for (int thisNote = 0; thisNote < 61; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(speakerPin, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.50;
    delay(pauseBetweenNotes);
    noTone(speakerPin);
  }
}

Credits

Nicholas Fritts

Nicholas Fritts

1 project • 1 follower
Ben Saunders

Ben Saunders

1 project • 1 follower

Comments