How the Laser Tripwire Works



Like most PIC projects, the circuitry for the laser tripwire is very basic, with the complexity of this project being the PIC program, itself.

The laser and buzzer in this circuit are controlled by the PIC via a transistor, as PICs are not able to source or sink large amounts of current. Therefore, the transistors Q1 and Q2 act like switches and can enable/disable power to their respective load. Voltage regulation is provided by the LM7805, and various passive components ensure decoupling, pulling up/down logic lines, and reverse polarity protection.



The switch matrix uses four rows of four switches, making a total of 16 switches. If each switch was connected to its own IO pin on the PIC, 16 IO pins would be needed, which is not entirely desirable. However, by using a matrix where four switches are connected together with a 4-bit output, you only need eight IO pins instead of 16.



The downside is that you have to scan through each row as all four buttons on each row share a common bus. Thankfully, this is a PIC and so such a scanning technique is incredibly easy in code, especially C!

The code for the PIC is rather simple, with the following key features:

Password protection

Armed or reset state

Laser trigger detection

Key detection

The first check that is made in the main program loop is if the security system is in the armed state. If it is, then the analog pin RA0 is read to see if the LDR has sufficient light falling on it (from the laser). If it does not, then the buzzer is sounded. For the sake of simplicity, the code also turns off the buzzer if the light level goes beyond the trigger level. However, a real alarm would not disable the buzzer until the reset code was entered.



The second check is the key scanning system. This is done by first writing 0b00010000 to LATC, which enables the first row. The data from this row is read into a buffer and then LATC is shifted once left. This selects the next row, as LATC would now be 0b00100000 and the data from the matrix is read in.



Eventually, all rows have been read in, which brings us to the next part—the key checks. Switch statements are used for each 4-bit value read-in to determine the key that was pressed. If the key is a number key (0 – 9), then that value is loaded into key buffer, which holds the combination entered by the user. If the key is a function key, then one of the following happens as a result: