This project was aimed at creating a device that plays music when my roommate enters the home.
Problem Statement
I decided to design for my roommate, whom I am currently living with in Pittsburgh. I chose to design for her because her problem was unique - she comes home drained and exhausted and is looking for a way to let off steam and relax. Melissa is a full time graduate student and works part time at Google. She is also the lead on the Hyperloop team on campus and is also involved in several other organizations. She exercises daily and eats very healthy. In short, she leads a very busy lifestyle. IOT can significantly benefit her because there are many opportunities to make a relaxing environment through IOT.
Goal
My goal is to create a solution that makes the home more relaxing when Melissa comes home. She placed importance on the fact that she is most stressed when she first arrives at the apartment because she has a lot of thoughts going through her head at the end of the day. In order to alleviate this stress, I decided to build a system that plays music, which has beneficial properties when it comes to stress relief, when she enters the home. I hope that by being exposed to music as soon as she walks in, she can divert her mind and focus on the music instead of all the thoughts in her head.
Process
To complete this I used a motion sensor, a piezo speaker, an LED, breadboard, and all the appropriate wires. I first started out by wiring the piezo using this online tutorial. However after speaking with classmates and researching online, I found that I could eliminate the need for 2 wires by directly wiring to the ground and digital inputs (shown in images below).
As for the code, I started off with the code from the online tutorial:I then wanted to add in the functionality of only playing this music when motion is detected. So I started off with another document using code from this online tutorial. I then modified my code to include this functionality by combining these two codes correctly. Once I was done with my code and deployed it, it wasn’t working which is when I realized that my wiring was wrong. I had wired things to analog that were supposed to be digital. After fixing those issues, I was able to get it to work!
Outcome
I ended up with the final code below:
This diagram depicts my final wiring of the components:
Bill of Parts:
Motion sensor
Piezo speaker
LED light
10kOhm resistor
Appropriate wiring
Future work: My next step would be to code it to be able to play the “discover weekly” Spotify playlist that Melissa listens to when she walks into the door.
Reflection
I learned quite a lot from this project especially because I came into this with no background in electrical engineering or computer science. This activity definitely demystified how electronics work for me, because before I just considered the process to be a big black box in which I had no idea what went on. This was a great introductory project into circuits and the useful applications. I look forward to more projects in the future.
Final Code:
int ledPin = D1; // Pin LED is connected to
int piezoBuzzerPin = D2; // Pin Piezo Buzzer is connected to
int pirSensorPin = D0; // PIN PIR Sensor is connected to
int motionDetected = 0; // Start MotionDetected as low
int speakerPin = D2;
// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};
// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,4,4,4 };
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int calibrateTime = 10000; // wait for the thingy to calibrate
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirSensorPin, INPUT); // declare the PIR sensor as input
pinMode(piezoBuzzerPin, OUTPUT); //declare buzzer as output
Serial.begin(9600); //Set serial out if we want debugging
/*delay(5000); //Allow time for the PIR Sensor to calibrate*/
Particle.variable("motion", &motionDetected, INT);
pinMode( speakerPin , OUTPUT );
}
void loop(){
motionDetected = digitalRead(pirSensorPin); // Read the PIR sensor
if(motionDetected == HIGH) //If motion detected
{
digitalWrite(ledPin, HIGH);
digitalWrite(piezoBuzzerPin, 200);
delay(100);
digitalWrite(ledPin, LOW);
digitalWrite(piezoBuzzerPin, 25);
delay(100);
playNotes();
}
digitalWrite(ledPin, LOW);
digitalWrite(piezoBuzzerPin,LOW);
}
void playNotes()
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(speakerPin, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(speakerPin);
}
}
Click to Expand
This project was aimed at creating a device that plays music when my roommate enters the home.
January 31st, 2017