Shop OBEX P1 Docs P2 Docs Learn Events
Build Your Own Door Alarm — Parallax Forums

Build Your Own Door Alarm

Jessica UelmenJessica Uelmen Posts: 490
edited 2012-01-16 21:06 in Learn with BlocklyProp
Build Your Own Door Alarm
·
Tired of family, co-worker or roommate troublemakers breaking into your room or office·while you’re away?· This project will deter those sneaky folks from messing with your stuff by demonstrating how to program a simple door alarm.· This alarm will use the Ping))) Ultrasonic Sensor to monitor the doorway, and will go off each time it detects an object within its range.· Just in case someone enters while you’re away, it will also record the number of times the alarm has been “tripped” and display it on the Parallax Serial LCD so you can check it when you get home!
·

·
Download Source Code (.zip)

Getting Started

This project uses parts and concepts included in·Smart Sensors and Applications, designed to be an example of a simple solution to a real world problem.· If you are new to the BASIC Stamp microcontroller or to programming, it would be a good idea to review the following before continuing:
·
······· Complete the following chapters in What’s a Microcontroller:
o······· Chapter 1: Getting Started with the BASIC Stamp
o······· Chapter 2: Lights On – Lights Off
o······· Chapter 8: Frequency and Sound
······· Complete the following chapters in Smart Sensors and Applications:
o······· Chapter 1: The Parallax Serial LCD
o······· Chapter 2: The Ping))) Ultrasonic Distance Sensor

Parts Required

(1) ·HomeWork Board with BASIC Stamp 2
····· The BASIC Stamp 2 Board of Education is also suitable for·this activity
(1) Ping))) Ultrasonic Distance Sensor
(1) Parallax Serial LCD
(1) Servo/LCD Extension Cable
(2) LEDs (any color)
(1) Piezospeaker
(2) 470 Ω Resistors

Optional Mounting Bracket Parts

(2) L-Shaped Mounting Brackets
(6) 4-40 zinc plated nuts
(4) 1/4" 4-40 pan-head screws
(2) 1/2" 4-40 pan-head screws
(2) 1/4" round nylon spacers #4


Schematics and Building the Circuits

Figure 1 shows the schematic and Figure 2 shows wiring diagram for the door alarm.· If you take a look at Figure 2, you may notice that the Parallax Serial LCD is mounted to the board using 90-degree universal mounting brackets.· These are included in the Smart Sensors and Applications Kit, but are not required for the project.· However, if you would like to use them, please see Figure 3 for an assembly diagram.

Figure 1 – Door Alarm Schematic

attachment.php?attachmentid=61537

Figure 2 – Door Alarm Wiring Diagram

attachment.php?attachmentid=61538

Figure 3 – Parallax Serial LCD Mounting Bracket Assembly Diagram
attachment.php?attachmentid=61539

Testing the Circuit

Before continuing, it’s always a good idea to check the wiring for any errors.· This will help save value troubleshooting time when you already know that each component is connected correctly.· Run TestAlarmCircuit.bs2 and verify that the following occurs:
·
······· The piezospeaker emits a tone
······· The LEDs turn on
······· The echo time of the Ping))) sensor is displayed on the Serial LCD
[color=#008000]' TestAlarmCircuit.bs2[/color]
[color=#008000]' Checks that each aspect of the alarm circuit is working.[/color]
 
[color=#008000]' {$STAMP BS2}[/color]
[color=#008000]' {$PBASIC 2.5}[/color]
 
[color=#000000]time      VAR   Word[/color]
[color=#000000]counter   VAR   Byte[/color]
 
[color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]22, 12]          [/color][color=#008000]' Initialize the LCD[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 5[/color]
 
[color=#020FC0]FREQOUT[/color][color=#000000] 14, 500, 2000            [/color][color=#008000]' Play a tone[/color]
 
[color=#020FC0]D[/color][color=#000000]O[/color]
  [color=#020FC0]PULSOUT[/color][color=#000000] 15, 5                  [/color][color=#008000]' Get echo times[/color]
  [color=#020FC0]PULSIN[/color][color=#000000]  15, 1, time[/color]
 
  [color=#020FC0]HIGH[/color][color=#000000] 13                        [/color][color=#008000]' Turn LEDs on[/color]
  [color=#020FC0]HIGH[/color][color=#000000] 12[/color]
 
  [color=#008000]' Display echo times on LCD[/color]
  [color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]128, [/color][color=#ff0000]"time = "[/color][color=#000000], [/color][color=#000080]DEC5[/color][color=#000000] time][/color]
 
  [color=#020FC0]PAUSE[/color][color=#000000] 200[/color]
[color=#020FC0]LOOP[/color]


Before moving on, test the circuit in the area you want to monitor and make a note of the echo time of the Ping))) when nothing is in its way.· That way, when you program the alarm later, you can tell it to activate if the echo time is less than the unobstructed distance of the area you are monitoring.

Custom Characters and the Final Program

This example alarm makes use of the custom characters that are set aside for the Parallax Serial LCD, specifically to display a Jolly Roger – or skull and crossbones.· Smart Sensors and Applications, Chapter 1 - Activity #4 demonstrates how to define your own custom characters to display as you please.· If you don’t like the Jolly Roger, replace it with whatever you want!
·
Once you’ve decided on a final display for your alarm, it’s time to run the final program.· Before programming the alarm, there are two parts of the program that you will have to replace:
·
······· maxDist:· This constant is the echo time of the Ping))) when placed, unobstructed in the area you want to monitor.
······· Jolly Roger Custom Characters: Replace this section with the custom characters you want to use.

[color=#008000]' -----[noparse][[/noparse] Title ]-----------------------------------------------------------[/color]
[color=#008000]' DoorAlarm.bs2[/color]
[color=#008000]' Monitors a doorway until an object is in the way, then an alarm sounds,[/color]
[color=#008000]' and the number of triggers are recorded and displayed to the LCD.[/color]
 
[color=#008000]' {$STAMP BS2}[/color]
[color=#008000]' {$PBASIC 2.5}[/color]
 
[color=#008000]' -----[noparse][[/noparse] Variables ]-------------------------------------------------------[/color]
 
[color=#000000]time         VAR   Word            [/color][color=#008000]' Round trip echo time[/color]
[color=#000000]intruderCnt  VAR   Byte            [/color][color=#008000]' Count of alarm triggers[/color]
[color=#000000]counter      VAR   Word            [/color][color=#008000]' Counter for FOR...NEXT loop[/color]
 
[color=#008000]' -----[noparse][[/noparse] Constants ]-------------------------------------------------------[/color]
 
[color=#000000]maxDist      CON   2000            [/color][color=#008000]' Maximum distance for alarm trigger[/color]
 
[color=#008000]' -----[noparse][[/noparse] Jolly Roger Custom Characters ]-----------------------------------[/color]
 
[color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]248, %00000, %11000, %11000, %01100,       [/color][color=#008000]' Top left[/color]
[color=#000000]                %01110, %11011, %11001, %00000]            [/color][color=#008000]' crossbone[/color]
 
[color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]249, %00000, %11001, %11011, %01110,       [/color][color=#008000]' Bottom left[/color]
[color=#000000]                %01100, %11000, %11000, %00000]            [/color][color=#008000]' crossbone[/color]
 
[color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]250, %00000, %01110, %11111, %11111,       [/color][color=#008000]' Top half[/color]
[color=#000000]                %10101, %10101, %11111, %01110]            [/color][color=#008000]' of skull[/color]
 
[color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]251, %01110, %11111, %11111, %10001,       [/color][color=#008000]' Bottom half[/color]
               [color=#000000]%10001, %11111, %01110, %00000]             [/color][color=#008000]' of skull[/color]
 
[color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]252, %00000, %00011, %00011, %00110,       [/color][color=#008000]' Top right[/color]
[color=#000000]                %01110, %11011, %10011, %00000]            [/color][color=#008000]' crossbone[/color]
 
[color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]253, %00000, %10011, %11011, %01110,       [/color][color=#008000]' Bottom right[/color]
                [color=#000000]%00110, %00011, %00011, %00000]            [/color][color=#008000]' crossbone[/color]
 
[color=#008000]' -----[noparse][[/noparse] Initialization ]--------------------------------------------------[/color]
 
[color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]22,12]             [/color][color=#008000]' Initialize LCD[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 5[/color]
 
[color=#000000]intruderCnt = 0                   [/color][color=#008000] ' Reset intruder count to 0[/color]
 
[color=#008000]' -----[noparse][[/noparse] Main Routine ]----------------------------------------------------[/color]
 
[color=#020FC0]DO[/color]
  [color=#020FC0]PULSOUT[/color][color=#000000] 15, 5                              [/color][color=#008000]' Send pulse[/color]
  [color=#020FC0]PULSIN[/color][color=#000000]  15, 1, time                        [/color][color=#008000]' Read echo time[/color]
 
  [color=#020FC0]IF[/color][color=#000000] (time < maxDist) [/color][color=#020FC0]THEN[/color]                   [color=#008000]' If alarm is triggered...[/color]
[color=#000000]    intruderCnt = intruderCnt + 1[/color]
 
    [color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]128, 0, 129, 2, 130, 4,  [/color][color=#008000]' Display Jolly Roger[/color]
[color=#000000]                    148, 1, 149, 3, 150, 5][/color]
    [color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]132, [/color][color=#000080]DEC[/color][color=#000000] intruderCnt,    [/color][color=#008000]' Display number of alarm[/color]
                    [color=#ff0000]" intruders "[/color][color=#000000]]           [/color][color=#008000]' triggers on the LCD[/color]
    [color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]153, [/color][color=#ff0000]"detected!! "[/color][color=#000000]][/color]
 
    [color=#020FC0]FOR[/color][color=#000000] counter = 0 [/color][color=#020FC0]TO[/color][color=#000000] 3[/color]
      [color=#020FC0]FREQOUT[/color][color=#000000] 14, 500, 3800                  [/color][color=#008000]' Play alarm tone[/color]
      [color=#020FC0]PAUSE[/color][color=#000000] 100[/color]
      [color=#020FC0]HIGH[/color][color=#000000] 13                                [/color][color=#008000]' Flash LEDs[/color]
      [color=#020FC0]HIGH[/color][color=#000000] 12[/color]
      [color=#020FC0]FREQOUT[/color][color=#000000] 14, 500, 2700                  [/color][color=#008000]' Play lower tone for variety[/color]
      [color=#020FC0]PAUSE[/color][color=#000000] 100[/color]
      [color=#020FC0]LOW[/color][color=#000000] 13                                [/color][color=#008000] ' Flash LEDs[/color]
      [color=#020FC0]LOW[/color][color=#000000] 12[/color]
    [color=#020FC0]NEXT[/color]
 
  [color=#020FC0]ELSEIF[/color][color=#000000] (intruderCnt = 0) [/color][color=#020FC0]THEN[/color]
    [color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]128, 0, 129, 2, 130, 4,  [/color][color=#008000]' Display Jolly Roger[/color]
[color=#000000]                    148, 1, 149, 3, 150, 5][/color]
    [color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]133, [/color][color=#ff0000]"Alarm"[/color][color=#000000],[/color]
[color=#000000]                    153, [/color][color=#ff0000]"Armed!"[/color][color=#000000]][/color]
    [color=#020FC0]SEROUT[/color][color=#000000] 11, 84, [noparse][[/noparse]141, 0, 142, 2, 143, 4,  [/color][color=#008000]' Display Jolly Roger[/color]
[color=#000000]                    161, 1, 162, 3, 163, 5][/color]
  [color=#020FC0]ENDIF[/color]
 
  [color=#020FC0]PAUSE[/color][color=#000000] 100[/color]
[color=#020FC0]LOOP[/color]


After you’ve set the alarm, place it in the area you want to monitor and make sure it doesn’t activate when nothing is in its way.· If it does, try decreasing the maxDist constant until the alarm only activates as someone walks by.

How it Works

First, the program stores the echo time measurement from the Ping))) into a variable named time.·

· PULSOUT 15, 5
· PULSIN· 15, 1, time
·
That value is then compared to the maxDist constant defined at the beginning of the program.· If the echo time is less than that value, it displays the total number of times the alarm was triggered and sounds the alarm.

· IF (time < maxDist) THEN
··· intruderCnt = intruderCnt + 1
·
··· SEROUT 11, 84, [noparse][[/noparse]128, 0, 129, 2, 130, 4,
··················· 148, 1, 149, 3, 150, 5]
··· SEROUT 11, 84, [noparse][[/noparse]132, DEC intruderCnt,
··················· " intruders "]
··· SEROUT 11, 84, [noparse]/noparse]153, [/size][/font][font=Times New Roman][size=2][color=#ff0000]"detected!! "[/color][/size][/font][font=Times New Roman][size=2
·
··· FOR counter = 0 TO 3
····· FREQOUT 14, 500, 3800
····· PAUSE 100
····· HIGH 13
····· HIGH 12
····· FREQOUT 14, 500, 2700
····· PAUSE 100
····· LOW 13
····· LOW 12
··· NEXT
·
If no intruders have been detected, the LCD displays that the alarm is armed to deter people from entering the room.
·
· ELSEIF (intruderCnt = 0) THEN
··· SEROUT 11, 84, [noparse][[/noparse]128, 0, 129, 2, 130, 4,
··················· 148, 1, 149, 3, 150, 5]
··· SEROUT 11, 84, [noparse][[/noparse]133, "Alarm",
··················· 153, "Armed!"]
··· SEROUT 11, 84, [noparse][[/noparse]141, 0, 142, 2, 143, 4,
··················· 161, 1, 162, 3, 163, 5]

attachment.php?attachmentid=73703

Get Creative!

This project gives you a simple application for this alarm.· However, there are several things to keep in mind when implementing this design.· Take a moment to consider each one and come up with your own solution!· It could be as simple as disconnecting parts or adding another extension cable to the Ping))) so you can mount it away from the board.· In any case, each situation is different, and you’ll have to find the solution that best works for you!
·
······· If you aren’t home when the alarm goes off…
o······· Problem: What if the alarm is triggered and the intruder finds the board and unplugs the battery?· Is there a stealthier way to hide the alarm, or a way to modify the code to add a “Stealth Mode”?
······· If you have pets…
o······· Problem: How will you make sure that a person is triggering the alarm and not your pet?
······· If a persistent intruder knows where the alarm is hidden…
o······· Problem: What if you have a consistent intruder who knows the location of the alarm and constantly disconnects power to the board?· Is there a way to write the number of intrusions to memory so you can recover it even if power has been disconnected?· (Hint: See Smart Sensors and Applications Chapter 6.)
······· If you’re a grammar nut…
o······· Problem: You may notice that the word “intruders” is displayed even if only one intruder is detected.· (And the statement “1 intruders detected” just isn’t proper English.)· Is there a way you can modify the code to do the following:
§········ If 0 intruders are detected, the LCD displays: No intruders detected!
§········ If 1 intruder is detected, the LCD displays: 1 intruder detected!
§········ 2 or more intruders detected, the LCD displays: 2 intruders detected!

__________________________________________________ ___________________________
··
(c) 2009·by Parallax Inc - all rights reserved.··

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jessica Uelmen
Education Department
Parallax Inc.

Post Edited (Jessica Uelmen (Parallax)) : 8/25/2010 6:32:10 PM GMT
595 x 239 - 22K
368 x 347 - 29K
582 x 191 - 23K
727 x 160 - 26K

Comments

  • EverQuriousEverQurious Posts: 48
    edited 2009-06-13 07:32
    Interesting use of the PING sensor. Some may want to play with revamping the hardware / code to use a PIR sensor. This would give a wider area for a whole room alarm system.
    Just an idea, if one is inspired to tinker on various simple projects in between the head-busters like I do..

    Good luck yeah.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    EverQurious

    Post Edited (EverQurious) : 6/13/2009 8:03:10 AM GMT
  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2009-06-15 20:00
    Hi EverQurious,

    That's a great idea, and would only take a little modification of the original code. We definitely support tinkering here at Parallax! [noparse]:)[/noparse]

    Jessica

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax, Inc.
  • SRLMSRLM Posts: 5,045
    edited 2009-06-15 21:26
    The code looks pretty good, but a suggestion: make all the 'magic numbers' into constants. It helps keep the code organized and easy to maintain. Also, what is the goal (as the educator)? Do you want to teach how to make a project? Do you want to explain and demonstrate some principle? Do you want to simply give a how-to guide? If the reader can extract some principles from the page then I think it will be much more useful.

    I like the text based intruder in the video..

      0
     /|\
     / \
    
    



    Also, I couldn't download the ZIP. I got this:
    You have requested something that does not exist, or you do not have permission to access.
    

    Post Edited (SRLM) : 6/15/2009 9:41:27 PM GMT
  • WhitWhit Posts: 4,191
    edited 2009-06-17 15:41
    Jessica,

    More fun! Maybe I could use this to keep my kids in bed!

    By the way the ZIP is working now.

    Another fun coding problem might be...

    For no intuders - the LCD displays

    No·intuders
    detected!

    For 1 intruder - the LCD displays
    1 intruder
    detected!

    For more that 1 intruder, the LCD returns to the plural - "intruders."
    2 intruders
    detected!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Whit+


    "We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keeps leading us down new paths." - Walt Disney

    Post Edited (Whit) : 6/17/2009 3:51:14 PM GMT
  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2009-06-17 16:54
    Whit,

    Funny you should mention the additional code problem! We got the idea for this project from a local middle school we visited. The kids suggested this application after we we let them play with the PING, and so we thought it would be fun to demonstrate a simple solution to a real world problem that students deal with every day. If kids can relate to applications, they get all the more excited about programming and microcontrollers!

    As I was prototyping this, I was trying to decide whether to use the DEBUG Terminal and EEPROM to display the intruder count or to keep it simple with the Serial LCD. When I finished the DEBUG application, I did address that issue since I am a bit of a grammar nut. But in an effort to keep the code as simple as possible, I neglected to address that issue again when I decided to stick with Smart Sensors parts. I'll add that to the end of the post, since that is definitely a good habit for students to get in to!

    Jessica

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax, Inc.

    Post Edited (Jessica Uelmen (Parallax)) : 6/17/2009 4:59:28 PM GMT
  • WhitWhit Posts: 4,191
    edited 2009-06-17 17:42
    Jessica Uelmen (Parallax) said...

    As I was prototyping this, I was trying to decide whether to use the DEBUG Terminal and EEPROM to display the intruder count or to keep it simple with the Serial LCD. When I finished the DEBUG application, I did address that issue since I am a bit of a grammar nut. But in an effort to keep the code as simple as possible, I neglected to address that issue again when I decided to stick with Smart Sensors parts. I'll add that to the end of the post, since that is definitely a good habit for students to get in to!

    LOL! My Mom was an English major and a school teacher!

    I also saw an article yesterday on the web that said that "definitely" is the most ofter misspelled word. You, of course, spelled it correctly! wink.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Whit+


    "We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keeps leading us down new paths." - Walt Disney

    Post Edited (Whit) : 6/17/2009 5:47:47 PM GMT
  • phil kennyphil kenny Posts: 233
    edited 2009-06-18 00:54
    The link to the Zip file seems to be broken, again.

    I just got the same error message as SLRM.
  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2009-06-18 14:45
    How odd. I recopied the link, and it appears to be working now. Let me know if you have any other problems!

    Jessica

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax, Inc.
  • phil kennyphil kenny Posts: 233
    edited 2009-06-18 17:50
    Thanks.

    It seems okay now, but it wasn't working for a few days.
  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2009-06-18 18:01
    Thank you for pointing it out! When I was testing, it appeared to work in IE but not in Firefox, but after re-copying the link, it seemed to be OK. Very strange. Hopefully it will work from here on out.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax, Inc.
  • JR_301JR_301 Posts: 22
    edited 2009-06-22 02:13
  • SlimsitoSlimsito Posts: 1
    edited 2012-01-16 21:06
    I was actually curious is it possible do this project without a lcd dispay and dispay it in a debug window on the pc side?

    That's a great idea, and would only take a little modification of the original code. We definitely support tinkering here at Parallax! [noparse]:)[/noparse]

    Jessica

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax, Inc.[/QUOTE]
Sign In or Register to comment.