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 Distance 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!
A brief introductory video to this project can be found below.
This project uses parts and concepts included in Smart Sensors and Applications, designed to be a simple example of a 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? (#28123)
(1) HomeWork Board with BASIC Stamp 2 (#28158)
The BASIC Stamp 2 on a Board of Education (#28103) is also suitable for this activity
(1) Ping))) Ultrasonic Distance Sensor (#28015)
(1) Parallax Serial LCD (#27976)
(1) Servo/LCD Extension Cable (#805-00011)
(2) LEDs (any color)
(1) Piezospeaker (#900-00001)
(2) 470 Ω resistors (#150-04710)
(4) L-Shaped Mounting Brackets (#720-00011)
(6) 4-40 zinc plated nuts (#700-00003)
(4) 1/4" 4-40 pan-head screws (#700-00028)
(2) 1/2" 4-40 pan-head screws (710-00006)
(2) 1/4" round nylon spacers #4 (713-00005)
Source Code for Build Your Own Door Alarm [1]
The figures below provide the schematic and wiring diagram for the door alarm project.
Figure 1 - Door Alarm Schematic
Figure 2 - Door Alarm Wiring Diagram
If you take a look at the wiring diagram (Figure 2) above, you may notice that the Parallax Serial LCD is mounted to the board using 90-degree universal mounting brackets. These are are not required for the project; however, if you would like to use them to mount your Serial LCD, please use the optional mounting bracket parts and following assembly diagram:
Figure 3 - LCD Mounting Assembly
Before continuing, it’s always a good idea to check the wiring for any errors. This will help save valuable troubleshooting time as you will already know that each component is connected correctly.
' TestAlarmCircuit.bs2 ' Checks that each aspect of the alarm circuit is working. ' {$STAMP BS2} ' {$PBASIC 2.5} time VAR Word counter VAR Byte SEROUT 11, 84, [22, 12] ' Initialize the LCD PAUSE 5 FREQOUT 14, 500, 2000 ' Play a tone DO PULSOUT 15, 5 ' Get echo times PULSIN 15, 1, time HIGH 13 ' Turn LEDs on HIGH 12 ' Display echo times on LCD SEROUT 11, 84, [128, "time = ", DEC5 time] PAUSE 200 LOOP
Before moving on, test the circuit in the area you want to be monitored by the alarm device and make a note of the echo time of the Ping))) sensor when nothing is in its way. Then, 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.
Figure 4 - Alarm LCD Display
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:
' -----[ Title ]----------------------------------------------------------- ' DoorAlarm.bs2 ' Monitors a doorway until an object is in the way, then an alarm sounds, ' and the number of triggers are recorded and displayed to the LCD. ' {$STAMP BS2} ' {$PBASIC 2.5} ' -----[ Variables ]------------------------------------------------------- time VAR Word ' Round trip echo time intruderCnt VAR Byte ' Count of alarm triggers counter VAR Word ' Counter for FOR...NEXT loop ' -----[ Constants ]------------------------------------------------------- maxDist CON 2000 ' Maximum distance for alarm trigger ' -----[ Jolly Roger Custom Characters ]----------------------------------- SEROUT 11, 84, [248, %00000, %11000, %11000, %01100, ' Top left %01110, %11011, %11001, %00000] ' crossbone SEROUT 11, 84, [249, %00000, %11001, %11011, %01110, ' Bottom left %01100, %11000, %11000, %00000] ' crossbone SEROUT 11, 84, [250, %00000, %01110, %11111, %11111, ' Top half %10101, %10101, %11111, %01110] ' of skull SEROUT 11, 84, [251, %01110, %11111, %11111, %10001, ' Bottom half %10001, %11111, %01110, %00000] ' of skull SEROUT 11, 84, [252, %00000, %00011, %00011, %00110, ' Top right %01110, %11011, %10011, %00000] ' crossbone SEROUT 11, 84, [253, %00000, %10011, %11011, %01110, ' Bottom right %00110, %00011, %00011, %00000] ' crossbone ' -----[ Initialization ]-------------------------------------------------- SEROUT 11, 84, [22,12] ' Initialize LCD PAUSE 5 intruderCnt = 0 ' Reset intruder count to 0 ' -----[ Main Routine ]---------------------------------------------------- DO PULSOUT 15, 5 ' Send pulse PULSIN 15, 1, time ' Read echo time IF (time < maxDist) THEN ' If alarm is triggered... intruderCnt = intruderCnt + 1 SEROUT 11, 84, [128, 0, 129, 2, 130, 4, ' Display Jolly Roger 148, 1, 149, 3, 150, 5] SEROUT 11, 84, [132, DEC intruderCnt, ' Display number of alarm " intruders "] ' triggers on the LCD SEROUT 11, 84, [153, "detected!! "] FOR counter = 0 TO 3 FREQOUT 14, 500, 3800 ' Play alarm tone PAUSE 100 HIGH 13 ' Flash LEDs HIGH 12 FREQOUT 14, 500, 2700 ' Play lower tone for variety PAUSE 100 LOW 13 ' Flash LEDs LOW 12 NEXT ELSEIF (intruderCnt = 0) THEN SEROUT 11, 84, [128, 0, 129, 2, 130, 4, ' Display Jolly Roger 148, 1, 149, 3, 150, 5] SEROUT 11, 84, [133, "Alarm", 153, "Armed!"] SEROUT 11, 84, [141, 0, 142, 2, 143, 4, ' Display Jolly Roger 161, 1, 162, 3, 163, 5] ENDIF PAUSE 100 LOOP
First, the program stores the echo time measurement from the Ping))) sensor 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, [128, 0, 129, 2, 130, 4, 148, 1, 149, 3, 150, 5] SEROUT 11, 84, [132, DEC intruderCnt, " intruders "] SEROUT 11, 84, [153, "detected!! "] 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, [128, 0, 129, 2, 130, 4, 148, 1, 149, 3, 150, 5] SEROUT 11, 84, [133, "Alarm", 153, "Armed!"] SEROUT 11, 84, [141, 0, 142, 2, 143, 4, 161, 1, 162, 3, 163, 5]
Custom Characters at a Glance
Let’s take a look at the SEROUT statement needed to display the Jolly Roger:
SEROUT 11, 84, [128, 0, 129, 2, 130, 4, 148, 1, 149, 3, 150, 5]
Remember from Smart Sensors and Applications that the numbers 0–5 all correspond to the custom characters defined at the beginning of the program. The numbers 128–130 and 148–150 tell the LCD where to display those characters. You can find a full listing of both decimal and hexadecimal commands for the Parallax Serial LCD in Appendix B of the Smart Sensors and Applications PDF.
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))) sensor 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!
Links
[1] https://learn.parallax.com/sites/default/files/Files/Docs/Projects/Alarm/Mini-Project-Door-Alarm.zip