Peter's electronic projects

Electronic combination lock based on PIC

version 2, 023 - 2001/10/22 PIC16F84
version 2, 025 - 2004/10/16 PIC16F628
designed by Peter JAKAB

NOTE for beginners: PICs are general purpose microcontrollers which have to be programmed before you can use them in the actual circuit! Check out this link to learn more.

You can find part of this page translated to French at PC-Electronique

description

This is my electronic combination lock to use with an outdoor gate. The functionality is implemented in software. It turns on a relay (usually to open a door) for a few seconds if someone enters the valid code. Alternatively, it works as an ON/OFF switch, which toggles the relay each time the code is entered. This relay can operate a power-to-open type electric strike with a shorting contact or a power-to-hold type electromagnetic lock with a breaking contact (we need the relay because these locks usually work with AC, not DC). The code can be changed any time after entering the current code.

Current consumption of the circuit is low, because the PIC sleeps most of the time, and wakes up only for processing key presses. If you don't have a crystal, you can use the RC oscillator of the PIC16F84 as well, just check the PIC datasheets for details on oscillator configurations. The 16F628 already contains an internal RC oscillator, so no crystal is needed.

choices of lock versions:
v2-023: written for PIC16F84
v2-025: written for PIC16F628, optional LCD output

These versions share many features in common, the differences are detailed below.

operation

type in the correct code and use # as 'enter'. The initial code is 1234 after programming the un-modified HEX file. You could activate the output with typing in:

1 2 3 4 #

* is used to change the code. Type in the actual code then press *. If you didn't miss the actual code, the code change indicator LED will light up. Then type in the new code twice. Eg:

1 2 3 4 * 1 9 9 8 # 1 9 9 8 #

will change the code to 1998. The code changes immediately and permanently after typing the new code twice. If you miss entering the new code twice, the original code is kept.

software

Essentially, all software versions work the same. You can download and customize the source code for the version you choose. The first few lines of the code contain definitions of changeable parameters. If you are lazy, you can simply download the HEX files already compiled with default values. The definitions:

mhz EQU D'4'
this value is used for delay calibration. Of course, the code will run with different speed hardware as well, but faster or slower than intended.
pulsewidth EQU 'D'150'
this value is used to set the delay of the output pulse. To calculate the delay in seconds, use pulsewidth * 20 ms
clen EQU 4
this value sets the length of the code. The length is always fixed, and you can set it much higher, until you have available RAM on the chip

More details on the source code can be found here.

hardware

The keypad is actually a collection of push-buttons, organized into a matrix. It looks like this:

  COL0 COL1 COL2 COL3
ROW0 1 2 3 A
ROW1 4 5 6 B
ROW2 7 8 9 C
ROW3 * 0 # D

If you have a different layout keypad, you can change the definition of the buttons (keytable) in the source code. The keytable contains four lines, each consisting of four characters:

dt "123a"
dt "456b"
dt "789c"
dt "*0#d"

You can use a 3x4 keypad or a 4x4 keypad. The advantage of the bigger keypad is that you can use the extra letters in codes. Connect the keypad wires to the lock by the labels (ROW0 to ROW0, COL0 to COL0, and so on). In case of using a 3x4 keypad, simply leave the COL3 input unconnected, no other modification is required. If you don't have a keypad, you can even wire one from push-buttons.

Use a power source with DC output between 8 and 30 volts, min 200 mA (or higher, depending on your relay current) to power the lock.

The relay specifications are not given here, because you can choose from many. Choose the relay voltage based on your incoming voltage. If you use 12VDC to power your lock, choose a 12V DC relay. Choose the relay switch ratings based on what you intend to switch with it. For an electric strike, use one capable of switching at least 2A/24V.

The component labeled 'BUZZER' is a passive ceramic piezo sounder. Do not use buzzers with internal (sound generator) electronics and/or with low-impedance speaker coils in it.

power-up for the first time

If you are stuck with a problem, please check the FAQ first.

FAQ

Q: I don't want anyone to change the code. What should I do?

A: replace the '*' key in the keytable to '#'.

A: place the 3rd column of your keypad inside the box containing the PCB and change the keytable so as only the 3rd column contains the key '*'

Q: I lost the code for the lock. What should I do?

A: Re-program the PIC chip with the latest HEX file you used. The code will be reset to the value in the HEX file

A: read out the contents of the PIC with a programmer. You can find the actual code stored in the DATA EEPROM. Depending on the programmer software, the code can be modified and written back as well

Q: Is there a way to make this design work as an on/off switch (toggle) instead of a momentary switch? In detail, enter code to turn a switch on, enter the code again to turn the switch off

A: yes. The modified PIC16F84 code is downloadable here, name cl2b.asm

Q: The combination lock seems to work (it beeps once for every keypress), but it doesn't operate the relay!

A1: There can be a problem with operating your relay. To check, disconnect resistor from pin#8 (RB2) and touch it to the VDD (pin#14). If it does not operate the relay, check your transistor and the diode polarity. Measure the output of pin#8 (RB2) when you entered the correct code. It should change to +5VDC for the preset time interval then back to 0VDC

A2: you possibly misconnected some row/column lines so incorrect keycodes are generated. Eg, if you swap col0 and col1 wires, and press the keys 1 2 3 4, it gets interpreted as 2 1 3 5

Q: I don't have a 22pF capacitor. Can I use another value?

A: yes, any value between 10-25pF should work

combination lock v2-023 for PIC16F84

schematic

code

description written by download
source code and HEX file with default settings (10 MHz) Peter JAKAB cl2.asm
cl2.hex
HEX file with default settings for 4MHz Peter JAKAB cl2-04.hex
details of the source code internals Peter JAKAB combination_lock-alg.txt
experimental source and HEX file modified for toggle (ON/OFF) relay operation Peter JAKAB cl2b.asm
cl2b.hex
experimental source modified to accept 8 different 6-digit codes Alan Carvalho de Assis cl2c.asm
experimental source and HEX file modified to accept two different 6-digit codes for activating two pulse outputs
the second output is RB0
Melvic S. Punzalan cl2d.asm
cl2d.hex

part list

part number description
IC1 PIC16F84-10 or
PIC16F84A - Microchip microcontroller, pre-programmed
IC2 78L05 or
7805 - 5V output voltage regulator
Q1 BC301 or similar npn bipolar transistor, Ic(min)=500 mA
D1 1N4001, 1N4004 or 1N4007 diode
LED1 any color LED or
3mm green LED
R1 1 kohm 1/4W resistor
R2 220 ohm 1/4W resistor
C1 22 pF ceramic capacitor
C2 22 pF ceramic capacitor
C3 100 nF ceramic capacitor
C4 100 nF ceramic capacitor
X1 10MHz or 4MHz crystal
BUZZER ceramic piezo sounder
(also called 'piezo diaphragm', 'piezo audio transducer', 'piezo audio indicator')
keypad 3x4 or 4x4 matrix keypad
K1 relay, see text for specifications
misc power supply with 8-30V DC output - suggested voltage is 12VDC
connectors for the power supply and the keypad
Printed Circuit Board (PCB), strip board or bread-board
flexible wires for connecting relay, LED, buzzer, power supply, keypad
housing for the electronics

PCB

Please note that the component numbering and connector pin-out on the PCB won't match the schematic!

PCB name Postscript PDF
standard PCB copper
stuffing
copper
stuffing
PCB with power supply copper
stuffing
copper
stuffing

combination lock v2-025 for PIC16F628 with optional LCD

Newer version of the combination lock, adapted to the PIC16F628. You can connect an optional 2x16 character LCD to this circuit.

software

In addition to the standard definitions, you may want to change these variables:

#define HIDDEN_CODE '*'
this is the definition of what you see on the LCD instead of the digits (default is asterisk). If you delete this line, the digits are shown as entered
msg_line EQU 0x80
this value contains the LCD position for messages. By default it contains the address of the first LCD line (documented in the LCD datasheet)
code_line EQU 0xc0
this value contains the LCD position for code entry. By default it contains the address of the second LCD line (documented in the LCD datasheet)

schematic

This is the main schematic, not showing the optional serial LCD interface, which connects to the 6-pin connector 'serial LCD'.

code

description written by download
source code and HEX file with default settings Peter JAKAB cl2-025.zip
cl2-025.hex
source code and HEX file, toggle output operationPeter JAKABcl2-025c.zip
cl2-025c.hex
HEX file, show entered digits in clear Peter JAKAB cl2-025b.hex

part list

The parts for the LCD interface are not included in this list.

part number description
IC3 PIC16F627 or
PIC16F628 or
PIC16F627A or
PIC16F628A - Microchip microcontroller, pre-programmed
IC2 78L05 or
7805 - 5V output voltage regulator
Q1 BC301 or similar npn bipolar transistor, Ic(min)=500 mA
D1 1N4001, 1N4004 or 1N4007 diode
LED1 any color LED or
3mm green LED
R1 1 kohm 1/4W resistor
R2 220 ohm 1/4W resistor
C3 100 nF ceramic capacitor
C4 100 nF ceramic capacitor
BUZZER ceramic piezo sounder
(also called 'piezo diaphragm', 'piezo audio transducer', 'piezo audio indicator')
keypad 3x4 or 4x4 matrix keypad
K1 relay, see text for specifications
lcdif serial LCD interface: an HD44780-compatibe 2x16 char LCD and some more electronics
optional, see text
lcdconn 6-pin header for connecting the LCD interface
misc power supply with 8-30V DC output - suggested voltage is 12VDC
connectors for the power supply and the keypad
Printed Circuit Board (PCB), strip board or bread-board
flexible wires for connecting relay, LED, buzzer, power supply, keypad
housing for the electronics