Introduction: Altoid Tin Quiet Alarm

Altoid Tin ultrasonic silent room alarm
This is my first instructable, so please excuse any mistakes that I may have made. My girlfriend was having trouble with her roommate and was not sure if the roommate was going into her room without permission. I decided to build a small sensing device to tell if somebody had entered the room. It is powered via USB. This project was built out of parts that I had from previous.
*I apologize in advance for the poor photography

[Edit (2-25-14)]
Here is the datasheet for the ultrasonic sensor. 
http://www.micropik.com/PDF/HCSR04.pdf

Video Coming Soon

Step 1: Step 1: Materials

Like I said, I had built this out of things that I already had, but all the parts can be bought from electronic distributors.  This project could be built for about $10; I didn’t have to spend any money on it.

Components:
(1) HC-SR04 Ultrasonic Distance Sensor
(1) ATtiny85 microchip
(1) 8 pin socket
(2) 220Ω resistors
(2) LED (I chose one red and one green)
(1) Pushbutton Switch
(1) Mounting Bracket
(1) Altoid Tin
Arduino Uno or ISP programmer
Various wires, screws, a USB cable and a small section of perfboard
You will also need the library for the ultrasonic sensor to communicate with the ATtiny chip
https://drive.google.com/folderview?id=0B6s_UhmkBLQfSXFPVHFZS190dEU&usp=sharing

Tools:
Soldering Iron
Hot Glue Gun
Razor Blade
Ruler
Dremel
*Don't laugh at my ISP programmer, my friend is borrowing my breadboard so I had to use my boe-bot.

Step 2: Step Two: Schematic

Step 3: Step 3: Programming the ATtiny85 Chip

I used my Arduino Uno as the ISP to program the tiny chip I will not go through the steps to do this, but here is the site that I used to do it.
http://42bots.com/tutorials/how-to-program-attiny85-with-arduino-uno-part-1/
http://42bots.com/tutorials/how-to-program-attiny85-with-arduino-uno-part-2/

Code Overview
The code is designed so that a person can both set and check the lights without setting it off on accident. The user will plug the sensor in, it will give 5 seconds before it starts testing for distance. This gives the user time to leave the room after setting it. A similar effort was put into checking the sensor, naturally how it is programmed is that whenever it reads the value below 70cm it’ll go red. This means that every time the door is opened the red LED would be turned on. However the way that it is programmed the user has about 5 seconds before it switches from green to red.
Within the code there is a portion to filter out 0 values, this comes from a problem with the sensor. Every now and then it’ll throw a random 0, so the code filters out any value that is equal to 0 to prevent a false entry.

[Edit (2-25-14)]
The ultrasonic sensor works by sending out a pulse signal out of the sending side, collecting the pulse signal in the other side which is essentially a microphone and calculating the time it takes to receive in microseconds(which is 1/1,000,000 of a second). It will take this time that was collected and divide by the speed of sound measured in microseconds. This returns the distance in centimeters. The ultrasonic sensor works best when pointed perpendicular to the object that it is measuring. It will work when it is at an angle, but the maximum reading comes from the perpendicular reading. 
This program is constantly sending out ping signals and checking for interruptions in the distance. This way it has an absolute chance of catching somebody entering the room(unless of course they run faster than the speed of sound). 

The Code
#include <TinyNewPing.h>

// ---------------------------------------------------------------------------
// Code that was written for the Arduino based silent alarm
// 2-23-14
// Posted on instructables
// Based on NewPing example
// ---------------------------------------------------------------------------

#define TRIGGER_PIN  2  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     3  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum      sensor distance is rated at 400-500cm.
int buffer, distance;
int green=0, red=1;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum          distance.

void setup() {

    pinMode(green, OUTPUT);    //Sets the pins as outputs for the LEDs
    pinMode(red, OUTPUT);

    digitalWrite(green, HIGH); // This section is to make the light flash for about 5 seconds
    delay(500);                //Also gives you a chance to leave the room without setting the sensor off.
    digitalWrite(green,LOW);
    delay(500);

    digitalWrite(green, HIGH);
    delay(500);
    digitalWrite(green,LOW);
    delay(500);

    digitalWrite(green, HIGH);
    delay(500);
    digitalWrite(green,LOW);
    delay(500);

    digitalWrite(green, HIGH);
    delay(500);
    digitalWrite(green,LOW);
    delay(500);

    digitalWrite(green, HIGH);
    delay(500);
    digitalWrite(green,LOW);
    delay(500);

  digitalWrite(green,HIGH);      //Keeps the green led on until interrupted
  buffer=200;

}

void loop() {

  delay(250);                      // Wait 250ms between pings. 29ms should be the shortest delay between      pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  buffer=uS / US_ROUNDTRIP_CM;

  if(buffer!=0){              //Sensor Gives false readings this was to get rid of 0 values
    distance=buffer;
  }
  else
  {}

if(distance<70)              //Measuring distance, to show that someone has been into the room
{

  delay(5000);                //Waits 5 seconds to give you a chance to check the flashing lights.
  digitalWrite(green,LOW);
  do
  {
  digitalWrite(red, HIGH);    //Shows that somebody has opened the door or entered the room
  delay(1000);
  digitalWrite(red,LOW);
  delay(1000);
  }while(1);

}

}

Step 4: Step 4: Drilling the Holes

The bracket that was used in this project was built about a year ago for a school project, I didn’t have to modify it. It’s just a bent piece of sheet aluminum.
The project was meant to be mostly contained into an Altoid tin so a few holes had to be drilled to mount all of the parts.
1. The hole for the pins of the ultrasonic distance sensor
2. Mounting holes for the bracket
3. Holes for the LEDs
4. Pushbutton hole
5. Entrance for the USB power supply
6. Not a drilled hole, but line the inside with electrical tape to prevent shorting

Step 5: Step 5: Soldering and Mounting

I didn’t take a good picture of the circuit that was built (probably because I’m a bit ashamed of how bad it looked) but the schematic should be clear enough to do the soldering if you are interested in building this also.

Solder a RESET switch between pin 1 and 4 of the chip. (one being top right by the indention circle)
Solder the red wire from the USB to pin 8 and the black wire to pin 4 also.
There needs to be a 220Ω resistor and the red resistor connected to pin 6 then grounded to pin 4.
There also needs to be a 220Ω resistor and the green resistor to pin 5 and grounded to pin 4.
The Distance Sensor:  The pin labeled VCC should be connected to the pin 8 of the chip.
Echo pin should be connected to pin 2.
Trig pin should be connected to pin 7.
Finally GND should be connected to the common ground or pin 4.

I soldered wires to the LEDS to be attached to the lid and hot glued into place. The push button and USB wire were also hot glued into place.
*Apologies for the wired mess.

Step 6: Step 6: Final Product

The project can be tested at this point, now just turn it on and it will let you know if you have unwanted intruders without alerting them.

[edit March 3rd ] Pictured: green and red lights on

Just leave any questions, comments, or suggestions below. Thanks for taking the time to read this instructable. 

123D Circuits Contest

Participated in the
123D Circuits Contest