Combination Lock MkII


Kits are available for this project from
Talking Electronics for $10.00 plus postage.
Plus you will need:
6pin to 5pin adapter @ $2.50

You will also need:
PIC2 USB Burner
(MPASM and MPLAB come with PIC2)
and it includes USB lead

PIC12F629 Data Sheet (.pdf  4,926KB)
Instruction Set for PIC12F629
blank12F629.asm template

See more projects using micros:
Elektor,EPE,Silicon Chip     Notepad2.exe 
Library of Sub-routines "Cut and Paste
Library of routines:   A-E   E-P    P-Z 

Here are the files you will need:


This project puts a new emphasis on an electronic combination lock.
With just 2 buttons, we challenge you to crack the combination.
It has been called Combination Lock MkII because it has 2 buttons. We will have a MkIII in the future with 3 buttons, but it will not have the same features.

Combination Lock MkII built on matrix board

This project is built on Matrix Board and can be put together in an evening.
The main purpose of the project is to teach programming, however it also offers a challenge to prove that a digital lock with just 2 buttons creates a very high level of security.
All sorts of tricks can be added to a design that uses a microcontroller, including a time-delay if the wrong code is entered and complete lock-out for 15 minutes if more than 5 incorrect codes are entered.
The project comes with a programmed chip but without an entry code. This is the first thing you do when setting up the project.
The combination is stored in EEPROM so it is retained, even when power is removed and  consists of 8 presses. After the 8th press, you must not press any more buttons as the program waits 1 second then turns on the green LED for 5 seconds. This output can be taken to a solenoid or latch to open a door etc.
To enter your combination, both switches are pressed when the project is off and kept pressed when power is applied. This puts the project into PROGRAMMING MODE.
Press the buttons eight times in an order (you can easily remember) and the green LED will come on for 5 seconds.
Turn the project OFF then ON again.
It is now programmed.
Press the buttons in the same sequence and the green LED will come on for 5 seconds.
If an incorrect sequence is pressed, the program will enter WAIT MODE and will not reset for 10 seconds. There is no indication that this is occurring, so anyone trying to break the combination will be unaware. This prevents someone pressing the buttons continually, in an attempt to discover the combination.
Even though there is only 256 different combinations, the WAIT MODE makes it virtually impossible to discover the combination as the "Lock Breaker" does not know how many presses are needed to create a sequence.
The project has a second LED (red LED) that turns on if the program detects 5 incorrect sequences. This is called a TAMPERING LED and can be connected to an alarm. This will discourage anyone who thinks they can work on the lock for hours. Wait for the red LED to turn off before entering the correct sequence. At the same time an output to a piezo produces a beep-tone and a buffer transistor can be connected to the output to drive a small speaker. It will produce a bee-beep-beep for 10 seconds.

The project is designed for all sorts of uses, including security for your house, garage or back-shed.
The normal digital lock has up to 13 keys or buttons and 5,500 combinations and by progressively going through the combinations, you will be able to open the door. The main reason why these locks are difficult to "break" is the psychological aspect of letters instead of numbers.
Simply replace the letters with numbers and the lock becomes much easier to work with.
On top of this, they all have a "C" cancel button at the lower corner so don't touch this when trying a code.

A digital lock such as this project, has a hidden number of sequences and the code is a complete mystery.
But the real reason for presenting this project is to teach programming.
This is another example of using an 8 pin chip to provide a number of features that would take many logic chips (such as counters and gates) and lots of components to duplicate.
It also highlights our method of hand-coding as an effective way to produce a program.
The project uses about 400 instructions to produce the effects and it uses the EEPROM to store the sequence produced by the user.
In this respect, some of the sub-routines in the program are quite complex and suitable for the advanced programmer. However, if you are a beginner, you can read through the program and most of the sub-routines will be easy to follow as each line of code is explained. You have to start somewhere and this project offers a challenge.
Most projects with a program of this complexity are only available as a pre-programmed chip or only the hex code is available. There is usually no attempt at educating the reader in programming.
That's the difference between our projects and all others.
We offer a learning curve.
For every hour of effort you put into reading, building and using one of our microcontroller projects, you get the experience of 100 hours of effort that has been put into the design to make it appear simple.

All you have to do is start . . .

The 8-button code is created by the user. The chip starts-off with no 8-button code.
To create a code, you need to get the chip into Programming Mode by pushing both buttons and hold them down WHEN THE PROJECT IS OFF and turn the project ON. This puts it into programming mode. 
Release the buttons. Both LEDs will flash to indicate the chip is ready to receive the 8-press sequence.
Press the two buttons in any sequence that you can easily remember.
After 8 presses, turn the project off. The code is now held in EEPROM.
Turn the project ON and repeat the 8 presses to "break the code." The green LED will flash 4 times then remains on for 10 seconds.
If 5 incorrect codes is entered, the red LED flashes 20 times and the piezo produces a beep between these flashes. The program then restarts.



The circuit is very simple. It is just 2 LEDs, 2 switches and a piezo diaphragm. All the work is done by the micro. It is powered by 3 watch cells.

You can build the circuit on any type of PC board and we have used a small piece of matrix board.
The kit of components comes with all the parts you need to get the project working, including a pre-programmed chip and the matrix board.
To modify the program you will need a PICkit-2 programmer and this comes with 2 CD's containing all the software needed for In-Circuit Programming.
You will also need a lead (comes with PICkit-2) to connect the programmer to your lap top via the USB port and an adapter we call 6pin to 5 pin Adapter to connect the PICkit-2 to your project.

The kit comes with a pre-programmed PIC chip but if you want to program your own chip or modify the program, the .hex file is available as well as the assembly file, so you can see how the program has been written and view the comments for each line of code.
The PIC12F629 is one of the smallest micros in the range but you will be surprised how much can be achieved with such a tiny micro.
The program contains sub-routines to produce delays, sequences on the display and both read and write EEPROM; jobs that require accurate code - including a special sequence - called a handshaking sequence that prevents the EEPROM being written due to glitches. 
Even a program as simple as this is not easy to put together and to assist in this area, we have provided a whole raft of support material.
Not only do we provide a number of programs with full documentation but our approach to programming is simple.
It involves a method of "copy and paste" whereby sub-routines are taken from previously written code and copied into your program. Any modifications are made in very small steps so that each can be tested before adding more code.
This is exactly how we produce a complex project. Each step is written and tested before adding the next step.
This saves a lot of frustration as it is very easy to add a line of code that is incorrect and get an unsuspected result. 
If you follow our suggestions you will buy a programmer ("burner") called a PICkit-2 if you are using a laptop. It is the cheapest and best on the market and comes with a USB cable and 2 CD's containing the programs needed to "burn" the chip. If you are using a desk-top and/or tower with a serial port, you can use a cheaper programmer called MultiChip Programmer from Talking Electronics. You will also need NotePad2 to write your .asm program. This can be downloaded from Talking Electronics website. You will use ComboLockMkII.asm or ComboLockMkII-asm.txt as a basis and it is best to change on a few lines at a time to see what effect is created. You will also need a 6 pin to 5 pin connector that fits between the burner and the project. This is also available on Talking Electronics website.
As we said before, this project is for medium-to-advanced programmers as it is very compact and does not have in-circuit programming pins.
To be able to modify the chip you will need a programming socket and this can be obtained from one of our other projects that contains the 5 pins for in-circuit programming. Or you can build a programming socket by adding a socket to a surface-mount PC board and solder 5 pins to one edge and connect the socket to the pins.
You can then put the chip into the socket and program it.

There are a number of kits, programs and courses on the market that claim and suggest they teach PIC Programming.
Most of these modules and courses use a PIC microcontroller as the chip carrying out the processes, but the actual programming is done by a proprietary language invented by the designer of the course.
Although these courses are wonderful to get you into "Programming Microcontrollers" they do not use any of the terms or codes that apply to the PIC microcontroller family.
All our projects use the 33 instructions that come with the PIC Microcontroller and these are very easy to learn.
We use the full capability of the micro and our pre-programmed chip is less than the cost of doing it any other way.
In addition, anything designed via our method can be instantly transferred to a PIC die and mass produced. And we use all the input pins and all the memory of the chip. The other approaches use less than 25% of the capability of the memory and one of the pins is not available.
In fact it would be difficult to reproduce this project via any of the opposition methods. It would require a larger chip and more expense. 
You can use our method or the opposition. Just be aware that the two are not interchangeable.
Ours is classified as the lowest "form" (level) of programming - commonly called machine code - invented in the early days of microprocessors - and now called mnemonic programming as each line of code is made up of letters of a set of words. The opposition uses a higher level language where one instruction can carry out an operation similar to a sub-routine.
But you have to learn the "higher level language" in order to create a program. And this requires a fair amount of skill and capability.  
It sounds great and it is a good idea. But if you want to learn PIC programming, it does not assist you. It is "a step removed" from learning PIC language. The other disadvantage of the opposition is the "overhead." The 1,000 spaces allocated for your program is filled with pre-written sub-routines. You may require only 10 of these sub-routines but ALL of them are loaded in the memory space. And they take up all the memory.
You have no room for your own program.
To get around this the opposition uses the 128 bytes in EEPROM to deliver instructions on how to apply the sub-routines. This provides about 30 powerful instructions using their language called BASIC (or a similar language). 
It's a bit like selling a diary filled with all the paragraphs you need to express yourself, and leaving a few blank pages at the back for you to write single lines such as: see page 24, paragraph 7, see page 63 paragraph 4, to create your diary entries.
It depends on how much you want to be in charge of writing a program. Using our method is like writing your own auto-biography. Using the opposition is like getting a "ghost writer."
When using a higher level language to create a program, you have absolutely no idea how the code is generated for the micro.
In some of the developmental kits, the code is "locked away" and you are NEVER able to access it.
Everything runs smoothly until a fault appears. With our method you can see the code. With the other methods, you cannot see the code - it's like doing key-hole surgery without the advantage of an illuminated endoscope to see what you are doing.
Everything has its place and our method of hand-assembly is only suitable for very small micros and you will eventually need to "learn a high level language."   The PIC12F629 has over 1,000 locations for code and this equates to more than 20 pages when printed, so this is about the limit to doing things by hand.
But our drive is to show how much can be done with the simplest devices on the market, at the lowest cost.
Anyone can show you high-technology at a high price but this is not where you start and this is not where you get enthusiasm.
We provide the things to get you started. That's the difference.

The program does a bit of detecting when turned on. It detects to see if both buttons are pressed. If both are pressed, the program goes to "entercode" subroutine  where 8 presses of the buttons is loaded into file "code8" then stored in location0 in EEPROM.
If buttons A and B are not pressed when the project is turned on, the program goes to Main and sub-routine "try" where it loops to create an 8 bit value in file "codetry."
The program then reads the EEPROM and compares the new value with the stored value and if they are the same, it flashes the green LED 5 times, keeps it on for 10 seconds, then waits for a new attempt at solving the code.
If the code is incorrect, the program waits 10 seconds and allows another code to the entered. If 5 incorrect codes are entered, the program flashes the red LED and beeps 20 times then resets.

All these features have been added to the program, one at a time, and it is important to add them in the correct order. Reading and writing to the EEPROM is a complex operation and the instructions must be laid out as shown in the program, as they include a hand-shaking sequence. When you need this code it is copied and pasted in its entirety, to prevent a mistake.
Nearly every instruction has a comment to explain not only what it does, but why it was chosen.
If you think you can start programming without reading programs from other developers, you are wasting your time.
This program was inspired from another project on the web, but many of the instructions were of a complex nature and the author simplified them to create this program.

There are some very "clever" instructions such as XOR where you can compare two files by using the XOR function and determine if they are the same. And very powerful instructions such as decfsz that decrements a file and if it is zero, the micro jumps over the next instruction.
You cannot be expected to know these "tricks" unless you study programming. That's why we are here.

Here are the files you will need:

;;ComboLockMkII.asm         *
;  16-5-2011                                                            *
;Press A and B  8 times to unlock a combination       *
;       --+--------------+---------------------- +5v     
;		  |              |   |  |
;         |              +---|[]|----+
;         |Vdd ---v---       |  |    |
;         +---|1   Gnd|      piezo   |
;             |    GP0|----+         |
;   +---------|GP5    |    |         |
;   |         |       |   | |100R    |        
;   |     +---|GP4 GP1|---| |-------------+    
;   |     |   |       |    |         |    |
;   |     |   |GP3 GP2|--------------+   | |100R 
;   |     |    -------     |             | |
;   o     o   PIC12F629   _|_            _|_
;  A /   B  /             \ / green      \ / red
;   /      /              ---  lock      --- 5 incorrect
;  |      |                |   open       |   tries
; -+------+----------------+--------------+--------------- 0v

	list	p=12F629
	radix	dec
	include	""
	errorlevel	-224	; Don't complain about tris
	errorlevel	-302	; Don't complain about BANK 1 Registers

         & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.

;       Configuration Bits

D1		equ	20h	;delay value
D2		equ	21h	;delay value
temp1		equ	22h 	;
code_EEPROM	equ	23h 	;
code8		equ	24h 	;SwA&B puts new code into "code"
count		equ	25h 	;to count 8 presses
codetry  	equ	26h	;holds current "try" of 8 presses
_40loops    	equ     27h 	;creates 10 sec delay
_5wrong		equ     28h 	;counts 5 wrong tries
tone		equ     29h 	;loops for piezo tone
alarmloops  	equ     2Ah 	;loops for alarm before resetting

;Beginning of program
	org	0x00				
SetUp	bsf	status, rp0 	;Bank 1			
       	movlw	b'00111000'	;Set TRIS  
	movwf	TRISIO	   	;GP0,1,2 outputs GP4,5 input
	bcf	option_reg,7	;pull-ups enabled		
	bcf	status, rp0	;bank 0		
	movlw   07h         	;turn off Comparator 
        movwf   CMCON       	;must be placed in bank 0 
        clrf	gpio		;clear gpio of junk
        movlw   5
        movwf   _5wrong         ;put 5 into the wrong-try file
        btfsc	gpio,5		;Is swA pressed?  pressed=0
	goto	Main					
	goto	entercode	;both sw pressed at turn-on 
				;then waits for project turn-off
;* Delays 	    *
_1mS	decfsz 	D1,f
	goto 	$-1			
	retlw 	00	

_10mS	movlw	0Ah
	movwf	D2
	decfsz 	D1,f
	goto 	$-2
	decfsz 	D2,f
	goto 	$-4
	retlw 	00
	;250mS second delay
_250mS	decfsz 	D1,1
	goto 	_250mS
	decfsz 	D2,1
	goto 	_250mS		
	retlw 	00	
_10Sec	movlw	.40
	movwf	_40loops
	call	_250mS		
	decfsz  _40loops,f
	goto	$-2
	retlw 	00	

;* Subroutines		*
	;entercode puts 8-bit code into EEPROM via "code8"
	call    _10mS
	btfss	gpio,5	;Is swA not pressed?
        goto	$-2							
	btfss	gpio,4	;Is swB not pressed?
	goto	$-4
	movlw	08
	movwf	temp1    ;4 loops of flashing LEDs
	clrf	gpio
	movlw	0ffh	    
	xorwf	gpio,f
	call    _250mS
	decfsz	temp1,f
	goto	$-4
	clrf	gpio	;turn off LEDs
	clrf	code8   ;code8 holds new code	    
	movlw	8
	movwf	count	;to count 8 presses
	rlf	code8,f	;shift "count" to the left. 
	call    _10mS   ;
	btfss	gpio,5	;SwA produces 0
        goto	$+5			
	btfsc	gpio,4	;swB produces 1
	goto	$-4	
	bsf	code8,0 
	goto	$+2
	bcf	code8,0 
	call    _10mS
	btfss	gpio,5	;Is swA not pressed?
        goto	$-2					
	btfss	gpio,4	;Is swB not pressed?
	goto	$-4		    
	decfsz	count,f
	goto	$-15
	movf	code8,w  ;move code8 to w
	call	write   ;put code8 into EEPROM location 0
	goto	$	;waiting for project to be turned off
	;read code from EEPROM 
	bsf	status,rp0			
	clrf	EEADR	;to read first location in EEPROM !!!
	bsf	EECON1,0 ;starts EEPROM read - result in EEDATA
	movf	EEDATA,w	;move read data into w
	bcf	status,rp0
	movwf	code_EEPROM ;put code from EEPROM into "code_EEPROM"  
	retlw	00
	;try generates value in "codetry" for 8 presses
try	movlw	8
	movwf	count	;to count 8 presses
	rlf	codetry,f	;shift "codetry" to the left
	call    _10mS   ;
	btfss	gpio,5	;SwA produces 0
        goto	$+5					
	btfsc	gpio,4	;swB produces 1
	goto	$-4	
	bsf	codetry,0 
	goto	$+2
	bcf	codetry,0 
	call    _10mS
	btfss	gpio,5	;Is swA not pressed?
        goto	$-2					
	btfss	gpio,4	;Is swB not pressed?
	goto	$-4	
	decfsz	count,f
	goto	$-15
	retlw	00	;returns with 8 presses in codetry

write	bsf	status,rp0	;select bank1
	clrf	eeadr		;to load into first location
	movwf	eedata		;w will have new code value
	bsf	eecon1,wren	;enable write		
	movlw	55h 		;unlock codes
	movwf	eecon2
	movlw	0aah
	movwf	eecon2
	bsf	eecon1,wr	;write begins
	bcf	status,rp0	;select bank0		
    	btfss	pir1,eeif	;wait for write to complete
	goto	$-1
	bcf		pir1,eeif
	bsf		status,rp0	;select bank1
	bcf		eecon1,wren	;disable other writes
	bcf		status,rp0	;select bank0		
	retlw	00		

;*Main			*

Main	call	try	     ;creates value in "codetry" for 8 presses
	call    readEEPROM	;first value in EEPROM into code_EEPROM
        movf	codetry,w       ;try from 8 presses is compared with
	subwf	code_EEPROM,w   ;value in EEPROM
	btfss	status,z   	;zero set if yes
	goto	$+.15		;no
	movlw	.10
	movwf	temp1    	;5 loops of flashing green LED
	clrf	gpio
	movlw	01		;to flash green LED   
	xorwf	gpio,f
	call    _250mS
	decfsz	temp1,f
	goto	$-4
	bsf	gpio,0		;turn on green LED for 10 seconds
	call	_10Sec
	bcf	gpio,0
	movlw   5
        movwf    _5wrong        ;put 5 into the wrong-try file
        goto	Main
	call    _10Sec	    ;wait 10 seconds
	decfsz  _5wrong     ;decrementing the 5 wrong tries file
	goto	Main
	movlw	.20             ;20 Alarm loops before resetting
	movwf   alarmloops					 
	bsf	gpio,1		;turn on red LED
	call    _250mS
	bcf	gpio,1		;turn off red LED
	call    _250mS	
	movlw   0ffh
	movwf	tone
	call    _1mS
	bsf	gpio,2
	call    _1mS
	bcf		gpio,2
	decfsz	tone,f
	goto	$-5
	decfsz  alarmloops,f		
	goto	$-.13	
	goto	SetUp

;*EEPROM     						*
	org	2100h			
	de	33h  ;initial value of location0 = b'00110011'
			; = Sw "A A B B A A B B" as file is rlf

You can supply the project with larger cells and connect the red output to a speaker to detect tampering. We have provided the hardware and software for you to do this.
Now it's now up to you.


Combination Lock MkII
Parts List

Cost: au
$10.00 plus postage
Kits are available

2   -  82R SM resistors

1  -  100n SM capacitor
1  -  10u  SM electrolytic

1  -  3mm red LED
1  -  3mm green LED
1  -  SPDT mini slide switch
2  -  mini tactile switches
1  -  8 pin IC socket 
1  -  PIC12F629 chip (LockMkII routine)
1  -  piezo diaphragm
1  -  5cm PCB strip for piezo
1  -  double sided tape for piezo
3  -  watch cells
1  -  3v coin holder battery holder
1  -  10cm tinned copper wire
1  -  10cm fine tinned copper wire
1  -  30cm fine enamelled wire
20cm very fine solder 
1  -  Matrix PC board