The code (available at the article link) is fully
annotated so it should be easy enough to follow what is
going on. I would like to point out a couple of items,
though. As I said, you will need to set CODE to whatever
four-digit disarm code you would like. You can also
change ALARM_DELAY to any value (in multiples of five)
to give a longer or shorter timeframe in which to leave or
enter the building before triggering the alarm. The tunes
played are stored in the arrays Ode ToJoy[], FuneralMarch[],
and Charge[] in this format:
to the keycode hard-coded into the variable CODE (which
will need to be set before the processor is programmed).
If a key isn't pressed within five seconds, the previous
digits entered are erased and the code must be entered
again from the beginning. This prevents you from entering,
say, three of the four digits and walking away. Someone
could possibly get lucky and hit the last digit by accident.
(99 x 1000) + (88 x 100) + (0 x 10) = 99880
// in this case only three digits are collected
{note, duration, …}
where note = a, b, c, d, e, f or g on the
treble clef and a3, b3, c3, d3, e3, f3 or g3
on the bass clef
and duration = whole, half, dquarter (dotted
quarter), quarter, eighth, deighth (dotted
eighth) or sixteenth.
When the correct code is entered (entered = CODE),
the alarm is disabled (by setting the variable alarmState to
DISABLED). The siren relay is energized (turning the siren
off) by pulling P1.7 high. Then, this is where I got a little
creative. Stored in the ROM are the musical notes for
Beethoven's "Ode to Joy" — a favorite of mine. When you
enter the correct code to disarm the system, the function
PlayMusic will sound the first few bars of Ode to Joy using
pulse width modulation (PWM). The key is assigned a
value of 99 in the lookup table and the key is assigned
These can be tailored to suit your preferences, but
remember that space is limited so try to keep tunes short.
Final Assembly
88. This allows the software to test for a result of 99880,
which occurs when the code *0# is input:
*0# is the three-digit code used to arm the system.
When this code is entered, alarmState is set to ENABLED
and the tune "da-da-da-da ... da-da ... Charge!" is played.
You then have 30 seconds (as defined by the constant
ALARM_DELAY) to exit the building and close the doors
before the alarm is triggered. If you leave a door open
after enabling the system, the siren will sound to alert you,
in which case you will have to re-enter the disarm code.
In my application, the 6 VDC in the schematic is
supplied by a 500-1,000 mA wall-wart transformer located
in the attic. Also in the attic is the siren, the siren relay,
and an uninterruptable power supply (UPS). The wall-wart
transformer and the siren relay contacts (labeled AC on
the schematic) are plugged into the UPS which is plugged
into an available AC outlet. In case of a power outage (or
a burglar cutting the power lines), the alarm is still
powered. Using a UPS allowed for a simpler circuit design
since I didn't have to incorporate a battery backup feature.
I replaced the built-in battery in my UPS with a
lawnmower battery for a longer run time, but that is
optional.
A Door/Window is Opened
When a sensor is interrupted — a door or window is
opened — then P1.3 is pulled by an internal pull-up
resistor. A software debounce tests P1.3 over a half-second period to see if the signal goes low again. If it
does, the system assumes it was a glitch and goes back to
sleep. This is necessary to prevent false triggers.
The keypad interface is connected to the siren relay
and power supply with Cat- 5 cable (or any multi-strand
cable). I had Cat- 5 on hand and the additional leads allow
for future upgrades.
Improvements
You then have 30 seconds (ALARM_DELAY) to enter
the correct code and disarm the system. If the correct
code is not entered in time, the siren relay is de-energized,
sounding the siren. The siren will continue to sound until
the disarm code is entered.
Attempts to find the code by brute-force (randomly or
sequentially trying numbers) are limited to three tries at a
time. After three incorrect attempts, the "Funeral March" is
played and the keypad is locked (or rather ignored) for
five minutes. During this time, the controller will not
respond to any further key presses.
Important Notes
There is room for improvement in any project and this
one is no exception. For starters, I would like to include an
LCD display to show the code as it's entered and to
provide feedback to the user (in conjunction with the
LEDs that are being used now). This would make it easier
to change the disarm code in real time as opposed to it
having to be hard-coded. Without the visual feedback an
LCD would offer, the possibility of error would be greater
when changing the key code. I also plan to incorporate an
auto-dialer so that the police can be called with a prerecorded message when the alarm is tripped. This could
be either an old-fashioned landline dialer or any unused
cell phone could be re-purposed (since even unactivated
cell phones can dial 911 free of charge).
With up to seven free I/O lines still available, the sky
is the limit as to what can be done with this basic alarm
system — all built around a $4 computer chip! NV
46 September 2013