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. 

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.

(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

Soldering Iron
Hot Glue Gun
Razor Blade
*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.

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, HIGH);
    digitalWrite(green, HIGH);
    digitalWrite(green, HIGH);
    digitalWrite(green, HIGH);
  digitalWrite(green,HIGH);      //Keeps the green led on until interrupted

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

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(red, HIGH);    //Shows that somebody has opened the door or entered the room


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. 

<p>I have a problem.When i upload the code the green led blinks,then afeter 5 seconds the red led turns on,even if the distance is &gt; 70 .How can i solve this problem?</p>
First of all I have to say that I'm pretty excited that you have a question about my little project. I'm glad that somebody could use it as a little inspiration. <br><br>Here is one of the things that came across my mind:<br><br>It could possibly be that your sensor isn't working correctly. Well it could be working correctly, but it may throw a false reading initially. I have had this problem before. <br><br>You can monitor the reading from the ping sensor and see if the first number it outputs is 0. If so you'll have to add a delay, or basically take that first reading and throw it out and use the second reading for comparison. <br><br>Also verify that your sensor is connected to the ATtiny correctly. It's amazing how something so small can be overlooked and ruin everything. <br><br>If nothing above works, feel free to email me and I will do my best to get the ball rolling.
It was a sensor problem.I tried to use another HC-SR04 and it works really fine now.
Can you email me at ultimatesoccer14@gmail.com?
Thanks,i managed to solve the problem.Thank you for the very useful project.
Not a problem. What'd you do to fix it? <br><br>Can you post pictures?
<p>so was the girlfriend's roommate intruding or not?</p>
Assuming that it was working perfectly 100% of the time....<br>We concluded that the roommate was not entering the room uninvited. <br><br>I appreciate you asking though.
<p>working awesome bro</p>
<p>Nice project! I'm curious to know does the red light only come on once? If the &quot;intruder&quot; leaves &amp; another enters does the light change to green again? or flash the # of visitors? </p>
It starts to flash 5 seconds after the &quot;intruder&quot; enters the room. This 5 seconds is the buffer time for the user to check and see if the lights are flashing. It won't tell you how many have been through, only that somebody had been in there.

About This Instructable




More by rsoileau2:Altoid Tin Quiet Alarm 
Add instructable to: