Nifty Car: Security system: Version 2

Updated 2002-11-3
Source code and binaries
Old page - has more ideas
[Nifty car]Version 2 of the security system is a partial implementation of what I hope to eventually have. It exists as a stand alone system on a PIC16F84. Version 3 or 4 will be part of the larger embedded system I am working toward. Version 1 is a simple microcontroller-less alarm system that protected my car.

The Version 2 system is has been protecting my car since early August. So far the only problem has been a poor choice for the relay that powers the sensors.

All the code is now available. Its made for the CCS C compiler, version 2.7x or so. This code is provided without support or schematics (I never made any). The text below describes the system, but it may not exactly match the code.

Schematics

As stated above, I never made any. Short of sketching something by hand and scanning it in (with my non-existent scanner), I wouldn't know how to do this. The tables below will give you an idea of how connect things. Of course, you've got to figure out the connection between the system and your car. The connections required are generic, so it should be workable with any car. I can only offer specific help for 4th generation Honda Civics.

Making connections to the car's power supply

Cars provide power with messy voltages while the engine is running. For this reason, it is imperative that protection be added to power supply connections. If the security system's electronics are connected to a car without such protection, the system will not survive for more than a few hours of engine operation.

This is what I recall doing:

System states

Connections to the microcontroller

Inputs

Item Signal description Action description
Two sensor modules with minor and major triggers The trigger lines require a weak pull-up. They will be brought to ground when the sensor is triggered. Multiple sensors can be connected to a single input on the controller. The minor triggers will sense an annoyance and can trigger the system's annoyed state to warn away evils.
The major triggers will trigger the regular alarm.
In the future, the system will log events to EEPROM, including which sensor was triggered.
One sensor module with a single trigger The trigger line requires a weak pull-up. It will be brought to ground when the sensor is triggered. Multiple sensors can be connected to a single input on the controller. This trigger is used to sense when an intruder has definitely, without doubt, entered the car. It will put the system in its compromised state.
Car power Goes high when the car's ignition is in the accessory or run position. Otherwise the line is low or needs a pull-down resistor. It will pass through a 4050 and then connect to the PIC. The system is deactivated when this line is high. When the key is removed from the ignition, the system starts a timer for passive activation of the alarm. Passive activation is aborted when the user commands the doors to unlock.
Lock & unlock pulses The separate lines are normally high and go low to lock or unlock the doors. The pulse stays low for almost a second. When it goes high again, it can reach 26 volts before going back to 12 volts. I'm told a capacitor can fix this, but I haven't tried. A 4050 will be employed to bring the voltage down to the levels the PIC needs. It should handle inputs of 20 volts just fine. When the car is stopped, the system will activate when the doors are locked and deactivate when the doors are unlocked.
User operated switch True (Vcc) to clear state, false (ground) normally. Used to clear the compromised state. The security system will/should act extra nasty while the compromised flag is set, so there's got to be a way to clear it that the criminal will likely not know.

Outputs

Item Signal description Usage description
Warning LED Anode connection The warning LED is lit while the in the initialization state and after a sensor has been triggered.
Alarm and warning siren power High to activate Activates a siren. There are separate connections for a warning siren and an alarm siren. When the alarm siren is activated, the warning siren will also be activated. I'm will use a loud, high-pitched siren I have put inside my car for the warning siren. I'm going to try to connect the car horn for the alarm siren.

Pins for PIC16F84

Pin Use Description (also see above tables)
A0 Warning LED Connect to the anode of the warning LED.
A1 Warning siren The output will be high to signal activation of the siren, and will go low when the siren should quit. The siren will also sound whenever the alarm siren sounds. This siren will be given a constant on signal during trouble. A piezo electric siren inside the car works well.
A2 Alarm siren The output will be high to signal activation of the siren, and will go low when the siren should quit. If you don't have a special siren in mind, you could connect this to the car's horn. The siren is not given a constant on signal; instead, a distinctive pattern is used (at least I haven't heard the pattern used on other security systems).
A3 Sensor power The output will be high to turn on the sensors. In most cases, this output should indirectly drive a relay that will supply power to the sensors. This is a TTL style output without the ability to directly drive a relay coil.
A4 Compromise clear switch Connect to a well hidden, but accessible without tools, switch. It needs to provide a normally grounded input to pin A4, and a high input when pressed.
B0 Sensor 0 minor Connect to one or more sensor outputs and a weak pull-up resistor. Sensor 0 is the primary sensor. The minor input is used to inform the system that the car is being bothered, but not broken into.
B1 Sensor 0 major Connect to one or more sensor outputs and a weak pull-up resistor. Sensor 0 is the primary sensor. The major input is used to inform the system that the car is likely being broken into.
B2 Sensor 1 minor Connect to one or more sensor outputs and a weak pull-up resistor. Sensor 1 is the secondary sensor and is ignored when a siren is in use. This allows sensor 1 to take input from a glass breakage sensor while a loud high pitched piezo electric siren is in the car's interior. The minor input is used to inform the system that the car is being bothered, but not broken into.
B3 Sensor 1 major Connect to one or more sensor outputs and a weak pull-up resistor. Sensor 1 is the secondary sensor and is ignored when a siren is in use. This allows sensor 1 to take input from a glass breakage sensor while a loud high pitched piezo electric siren is in the car's interior. The major input is used to inform the system that the car is likely being broken into.
B4 Sensor 2 major Connect to one or more sensor outputs and a weak pull-up resistor. Sensor 2 is a fail-safe sensor that should only detect break-ins and not give false alarms. When this sensor is triggered, the security system's compromised flag is set so the system will act rather nasty until the compromised clear button is pressed. If you don't have such a sensor, then connect this pin to Vcc.
B5 Lock pulse Connect indirectly to the relay input that switches power to the motors that lock the car's doors, or something similar. The input to the microcontroller should be normally high, and go low to indicate that the doors are being locked. From the pulse going to the PIC, I use a resistor, then a capacitor, then a circuit on a 4050.
B6 Unlock pulse Connect indirectly to the relay input that switches power to the motors that unlock the car's doors, or something similar. The input to the microcontroller should be normally high, and go low to indicate that the doors are being unlocked. From the pulse going to the PIC, I use a resistor, then a capacitor, then a circuit on a 4050.
B7 Car power Connect indirectly to power from the car that is applied only while the car is running. The input state should be high for a running car, and ground for a stopped car. To connect this, from the power input going to the PIC, I use a resistor, then a capacitor, then a circuit on a 4050.
The sensors used should provide output that is normally high impedance / floating, and is grounded to indicate that the sensor senses something bad. This allows multiple sensor outputs to be combined just by tying their output lines together. Each sensor input pin on the PIC16F84 needs a pull-up resistor (1K ohm should work).

Configurable code options

These are items that are configurable in the header file with no need to change C code. Not listed here are the I/O pin definitions. The code always uses defines for the pins in case they are changed later.
Item Define Usage description
Debug state DEBUG When defined, a debugging console is compiled in. It uses the CONSOLE_OUT pin (normally C4) to send text via RS-232 that shows the execution of the code. The output is 4800 baud, 8 data bits, no parity, and is sent using inverted TTL levels so no level converter is needed.

This option slows down the code some, but it should still be fast enough for actual use. It also greatly increases the code size.

PIC varies The microcontroller used. The code will run on a PIC16F84 and on a PIC16F87x. When the debug option is enabled, the PIC16F84 cannot be used.
LED blink period LED_BLINK_PERIOD This is used to tell when to blink the warning LED when it is blinking. For efficiency reasons, its value is more related to bit fields than time. The proper values that can be chosen are from (2^x - 1) where x is a whole number between 1 and 8. See the timerOverflow() function code to gain an understanding why.

The default value of 31 provides a blink rate of approximately 1Hz with all other defaults unchanged.

Length of second COUNTS_PER_SECOND This describes how long a second is and must be changed if the clock rate or prescalar is changed to maintain proper timings. All other time defines, save for LED_BLINK_PERIOD, use COUNTS_PER_SECOND to make it easier for a human to see the length of time described and to prevent the need to manually recalculate the other time defines.

The value of COUNTS_PER_SECOND should be:

    (clock rate) / 4 / (timer0 prescalar) / 256
The value is divided by 256 because the timer0 count is used as a prescalar -- the passage of time is recorded only when the timer0 count overflows. This allows higher clock rates to be used and was necessary to support the debugging console.
Passive activation delay PASSIVE_ACT_TIME After the car is stopped, the security system waits for passive activation. The amount of time it waits is defined by PASSIVE_ACT_TIME.
Passive activation defeat minimum delay PASSIVE_DEFMIN_TIME This is the minimum amount of time that must pass before the user can defeat passive activation. This is included because some power door locks will unlock the car after it stops, sometimes when the security system will be timing for passive activation. By requiring a minimum time to elapse first, passive activation will not be defeated by the car itself. That would render passive activation useless.
Initialization time INIT_TIME The time required for sensor power up. This is the time between when power is applied to the sensors and when the system will respond to sensor triggers. Some sensors will output a triggered state initially for several seconds after being turned on.
Annoyance time threshold ANNOYANCE_TIME If a minimum of annoyances, defined by ANNOYANCE_THRESHOLD, occur within the time defined by ANNOYANCE_TIME, the security system will become annoyed. This prevents the system from becoming obnoxious when people walk by or accidentally bump the car. Useful in parking garages with narrow parking spots.
Annoyance count threshold ANNOYANCE_THRESHOLD If a minimum of annoyances, defined by ANNOYANCE_THRESHOLD, occur within the time defined by ANNOYANCE_TIME, the security system will become annoyed. This prevents the system from becoming obnoxious when people walk by or accidentally bump the car. Useful in parking garages with narrow parking spots.
Annoyance alarm time ANNOYANCE_ALARM The duration of the alarm used to indicate an annoyance. Only the warning siren will be used for annoyances. This time should be short because it is meant only as a warning and indication that this alarm is no bluff.
Angry alarm time ANGRY_ALARM The duration of the alarm used to indicate that the system is angry. This is used when a sensor's major trigger has been tripped. This condition is considered a possible break in or break in attempt, so the duration should be long enough to drive away the evil ones. Both the warning and alarm sirens are used.
Compromised alarm time COMPROMISED_ALARM The duration of the alarm used to drive away a criminal when the system is certain that the car has been broken into.
Compromised initialization time COMPROMISED_INIT_TIME The duration of the initialization state (see INIT_TIME) when the compromised flag is set. This time should be greater than INIT_TIME to allow the driver time to clear the compromised state without being rushed, or time to drive off away from any legitimate owners before they realize what happens.

The user's view

The user interface consists of two LEDs. One is the obligatory blinking red LED. It lights up when power is applied to the sensors. The other is the warning LED (I'm going to suggest yellow). It blinks at things that don't bother the car ;-)
Blinking red LED Warning LED Meaning
off off The system is inactive and will remain so until something happens.
off blinking Passive activation is about to occur.
blinking blinking The sensors are powering up. The alarm will not trigger.
blinking off The system is active and the alarm will trigger.
blinking on A sensor input was triggered. The system may be annoyed (brief sound from warning siren), or angry (long sound from all sirens), or neither (no sound).