Introduction: Arduino Door Alarm

We were challenged to create a device with a useful application in our engineering STEM skills class at Governor's School for Science and Engineering 2017. This instructable will guide the reader through the process of making an Arduino controlled door alarm. This device will play a song when the designated microswitch is triggered. The device is encased in a wooden box to hide and protect from its outside environment.

Step 1: Materials and Tools

Materials

  1. Arduino UNO
  2. Breadboard
  3. 4 330 omega resistors
  4. 1 10K omega resistors
  5. 12 jumper cables
  6. 2 Female jumper cables
  7. 4 multicolored LED's
  8. 1 DICGU Microswitch
  9. 1 piezzo buzzer
  10. 1 Battery pack
  11. 4 AA Batteries
  12. Arduino Computer Cable (To upload code)

Step 2: Preparation

Make sure that the Arduino Application is installed on your computer.

Find here: https://www.arduino.cc/en/Main/Software

Step 3: Making the Project: Wiring the Arduino

The first major step of the project is to connect each component in the correct corresponding location on the Arduino and breadboard.

There are five major steps to wiring the Arduino correctly:

  1. Resistors
  2. Cables
  3. LEDs
  4. Buzzer
  5. Power

Step 4: Resistors

1. 10K Resistor

A. Place one end in H14 and the other in a "+"

2. 330 Resistor #1

A. Place one end in B3 and the other in any "-"

3. 330 Resistor #2

A. Place one end in B6 and the other in any "-"

4. 330 Resistor #3

A. Place one end in B9 and the other in any "-"

5. 330 Resistor #4

A. Place one end in B12 and the other in any "-"

Step 5: Cables

Each cable will have listed its two locations on the Arduino/breadboard.

  1. GND to any "-"
  2. 5V to "+" on right
  3. PIN 7 to E4
  4. PIN 8 to G14
  5. PIN 9 to J3
  6. PIN 10 to E10
  7. PIN 12 to E1
  8. PIN 13 to E7
  9. From 1 on switch to F14
  10. From 3 on switch to any "-"
  11. From any left "-" to any right "-"

Step 6: LEDs

There are four LEDs that can be employed if light is desired.

  1. Blue LED
    1. Positive (long leg) to C1
    2. Negative (short leg) to C3
  2. Red LED
    1. Positive to C4
    2. Negative to C6
  3. Green LED
    1. Positive to C7
    2. Negative to C9
  4. Yellow LED
    1. Positive to C10
    2. Short to C12

Step 7: Buzzer

This is the final step of the physical assembly.

  1. Buzzer "-" to G1
  2. Buzzer "+" tp G3
  3. Right "-" to J1

Step 8: Power

A portable source of power is used to ensure that the project will not require a constant computer connection to stay running. A battery holder is attached to the Redboard. Then, four AA batteries are inserted, giving a constant power of about 6V.

Step 9: Making the Project: Coding

The following code gives the Arduino the ability to perform the intended task. This code is based of the Arduino tutorial for a buzzer. However, it also has included the code for lights and a designated song. Insert this code into an open Arduino page to insert the code for the project.

/******************************************************************

* SparkFun Inventor's Kit * Example sketch 11 * * BUZZER * * This sketch uses the buzzer to play songs. * The Arduino's tone() command will play notes of a given frequency. * * This sketch was written by SparkFun Electronics, * with lots of help from the Arduino community. * (This sketch was originally developed by D. Cuartielles for K3) * This code is completely free for any use. * Visit http://learn.sparkfun.com/products/2 for SIK information. * Visit http://learn.sparkfun.com/products/2 to learn about the Arduino. * * Version 2.0 6/2012 MDG * Version 2.1 9/2014 BCH *****************************************************************/

const int buzzerPin = 9; // connect the buzzer to pin 9 const int songLength = 27; // sets the number of notes of the song // Notes is an array of text characters corresponding to the notes // in your song. A space represents a rest (no tone) char notes[songLength] = { 'b', 'a', 'g', 'a', 'b', 'b', 'b', 'a', 'a', 'a', 'b', 'd', 'd', 'b', 'a', 'g', 'a', 'b', 'b', 'b', 'b', 'a', 'a', 'b', 'a', 'g'}; // beats[] is an array of values for each note. A "1" represents a quarter-note, // "2" a half-note, and "4" a quarter-note. // Don't forget that the rests (spaces) need a length as well. int beats[songLength] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}; int tempo = 200; // The tempo is how fast to play the song (beats per second). int buttonPin= 8; int buttonState= 1; void setup() { pinMode(buzzerPin, OUTPUT); // sets the buzzer pin as an OUTPUT pinMode(10, OUTPUT); pinMode(7, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); pinMode(8, INPUT); } void loop() { int i, duration; // int val; buttonState = digitalRead(8); if (buttonState == 0){ for (i = 0; i < songLength; i++) // for loop is used to index through the arrays { duration = beats[i] * tempo; // length of note/rest in ms if (notes[i] == ' ') { // is this a rest? delay(duration); // then pause for a moment } else // otherwise, play the note { tone(buzzerPin, frequency(notes[i]), duration); if (notes[i] == 'd') digitalWrite(10, HIGH); if (notes[i] == 'b') digitalWrite(7, HIGH); if (notes[i] == 'a') digitalWrite(12, HIGH); if (notes[i] == 'g') digitalWrite(13, HIGH); delay(duration); digitalWrite(10, LOW); digitalWrite(7, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); // wait for tone to finish } delay(tempo/10); // brief pause between notes }} //while(true){ // We only want to play the song once, so we pause forever //} // If you'd like your song to play over and over, remove the while(true) // statement above } int frequency(char note) { int i; const int numNotes = 8; // number of notes we're storing char names[numNotes] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int frequencies[numNotes] = { 262, 294, 330, 349, 392, 440, 494, 523 }; // Now we'll search through the letters in the array, and if // we find it, we'll return the frequency for that note. for (i = 0; i < numNotes; i++) // Step through the notes { if (names[i] == note) // Is this the one? { return(frequencies[i]); // Yes! Return the frequency and exit function. } } return(0); // We looked through everything and didn't find it, // but we still need to return a value, so return 0. }

Step 10: Final

Compile the code on the computer. Then, send the code to the Arduino. Afterwards, disconnect the computer, allowing the device to depend on the battery source. If wanted, the device can be encased in a box. In several of our pictures, there are ideas where the device is in a capsule to protect it. However, this is not required for the purpose of the door alarm. This concludes the tutorial.

Comments

author
Swansong (author)2017-06-16

That's a neat project, I hope you get an A :)