Arduino Keypad
Arduino Keypad
Y KIT (V2)
SSEMBLY
Order Codes:
PIC Lock Self-Assembly Kit
6V
#
0
8
7
0V
O +
U
T
CHI008
LK
LK 1
2
Features
- AA batteries
- soldering iron and solder
- side cutters and small cross-head screwdriver
Contents:
R1
R2
R3
RP1
LK3
C1
C2
CT1
LED1
D1
FET
PZ1
SW1
IC1
IC1
CT
BT1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
revolution
PICLOCK.P65
Assembly Instructions
1. Solder all the resistors in position. The values of the resistors are shown on
the pcb, and the colour codes are given in the table on page 1. The zero-ohm
resistor wire link (marked with a single black band) is connected across LK3
2. Using a wire off-cut from a resistor leg, connect either LK1 or LK2 (not both).
Use LK1 to connect the piezo sounder to PIC pin RA4 (e.g. for the Chip
Factory) or LK2 to connect the piezo sounder to PIC pin RB6 (e.g. for PICAXE
or PIC-Logicator).
3. Solder the diode D1 in position. The grey bar should face away from the FET.
4. Solder the IC socket in position. Solder the 5pin SIL resistor in position,
ensuring the dot marked on the resistor aligns with the dot on the PCB.
5. Solder the FET in position. The text should be visible on the FET when it is laid
in position. Solder the PICAXE stereo connector in position
6. Solder the rectangular polyester capacitor C2 in position. It can be used either
way around.
7. Solder the reset switch in position - it will only fit one way around.
8. Solder the cylindrical electrolytic capacitor C1 in position. The long leg is the
+ve leg.
9. Solder the bicolour LED in position. The LED can be soldered directly to the
pcb or connected via wires (not supplied). Make sure the flat on the LED aligns
with the footprint on the pcb.
10. Solder the piezo sounder PZ in position. Before soldering, the two wires should
be woven through the large hole to help strengthen the joint. The red wire
connects to the + symbol.
11. Solder the 4 way screw terminal block in position. Make sure the contacts face
out. Note that the 4 way terminal block may be supplied as two 2 way
terminals which clip together.
12. Screw the battery clip into the bottom two contacts of the terminal block. The
red wire is connected to the 6V contact, the black wire to the 0V contact.
13. The keypad may be mounted directly onto the pcb, or connected via an
optional ribbon cable (not included). If mounting the keypad on the pcb use
wire off-cuts from the resistor legs to solder the 9 keypad pads to the pcb.
14. Push the PICAXE-18 chip into its socket. Make sure pin 1 faces the four
resistors.
15. Insert the AA batteries (not supplied) into the battery pack and then connect
to the battery clip.
DO NOT USE A 9V PP3 BATTERY WITH THIS PRODUCT.
ONLY USE THE 4.5V or 6V (AA CELLs) BATTERY BOX SUPPLIED.
Important Note:
The 100uF capacitor will keep the unit powered for approximately 20 seconds after the
battery box is removed. Therefore to reset the unit press the reset switch.
Resonator position X1 is not used. It is provided for users who may wish to use the
older PIC16F84A rather than the (cheaper) PIC16F627. In this case a 4Mhz 3-pin
ceramic resonator should be soldered into the X1 position.
revolution
PICLOCK.P65
Initial Testing
The reprogrammable PICAXE-18 microcontroller should be programmed with the
sample programs given at the rear of this datasheet. See the programming
systems help files for further information on how to do this.
If
Input/Output Configuration
Keypad Column 1
Keypad Column 2
Keypad Column 3
Keypad Row 1
Keypad Row 2
Keypad Row 3
Keypad Row 4
Piezo via LK1
Piezo via LK2
Bicolour LED
FET Output
- RA0
- RA1
- RA2
- RB0
- RB1
- RB2
- RB3
- RA4
- RB6
- RB4/5
- RB7
- Input 0
- Input 1
- Input 2
- Output 0
- Output 1
- Output 2
- Output 3
- Output 4/5
- Output 7
Other Notes
The IC may be damaged if any power supply other than 6V DC (centre +ve) is
applied to the power contacts. A 9V PP3 battery must never be used.
Alkaline AA cells are recommended for this application.
The PIC16F627 provided is pre-programmed to act as a PICAXE-18
microcontroller. However if re-programmed in a traditional ZIF socket
programmer it will loose the PICAXE bootstrap code and no longer act as a
PICAXE microcontroller.
Safety
This product is designed as an educational teaching aid. It is not a toy and should
not be handled by young children due to sharp edges and small parts.
THIS PRODUCT IS NOT A TOY.
revolution
PICLOCK.P65
row 1
row 2
row 3
row 4
col 2
col 3
col 1
9
0
Introduction
SECURIT
Y LOCKS
ECURITY
Keypad Scanning
The illustration below shows the basic keypad electronic configuration. The four
rows are connected to controller output lines, and the three columns are connected
to controller input lines.
output 1
output 2
output 3
output 4
input 1
input 2
input 3
all 10k
0V
When no switches are pressed, there is no electrical connection between any row
or column. Therefore all the inputs are at the logic level 0 (0V) status, as they are
pulled low by the 10k resistors connected to these lines. This means the input
signal to the controller is 0-0-0 along the three input lines.
revolution
PICLOCK.P65
If all the output lines are switched off (at logic level 0) and you press a switch,
the input signals will still remain at logic level 0. This is because there is no
high level signal anywhere on the keypad, and so the only possible input signal
to the controller is still 0-0-0.
However if you switch one, and only one, output line high, and then press a switch
on the corresponding row, a high input signal will be generated. For instance, if
you press button 1 when row 1 is switched high, the input signal to the controller
will be 1-0-0. As you have switched one, and only one, output row high, the
controller therefore knows that the 1-0-0 pattern can only have been produced by
switch 1. Switches 4,7, and * could not have produced the signal because their
output rows are still low. This means that if you continuously scan the keypad, by
switching each individual row high in turn, you can uniquely identify each single
switch. This is the basic principle used in almost every keypad in the world!
LED
keypad
sounder
microcontroller
solenoid
When using a keypad, it is important to have some feedback to the user to
acknowledge that a key has been pressed. Commonly this is achieved with a buzzer
or sounder, and so in this project we will use a piezo-sounder. In microcontroller
projects a piezo-sounder is always preferable to a buzzer as it is low cost, it can be
directly connected without any interfacing circuits, and can be used to produce a
variety of different sounds.
revolution
PICLOCK.P65
To show the status of the lock it can be useful to have a red (locked) and green
(open) indicator LED. Naturally two separate LEDs could be used, but a single, bicolour LED is a neater solution. This type of LED is often used on televisions or
computer monitors to show standby (red) or in use (green) status.
Microcontroller pins can source (give out), and sink (take in), electric current.
This means that if you connect a bi-colour LED as shown in figure 5, the LED will
light when either output pin is high as current fill flow out of the high pin and
into the low pin. The LED will go out when both output pins are in the same
condition (both high or both low) as no current can flow. Which pin is which
colour is best found by experimentation! A 330R current limiting resistor should
be used in series with the LED.
output pin
330R
output pin
The final electronic interfacing is the solenoid bolt in real applications this
would normally be a 12V electronic door strike plate, but as these cost around 40
we will use a 6V solenoid for modelling purposes.
The instinct of many teachers would be to use a relay for this interfacing. However
this is an expensive, and complicated, way of achieving such a simple interfacing
task. A Field Effect Transistor (FET) is much more appropriate in this situation, as it
can be directly wired to the microcontroller output pin.
For those unfamiliar with FETs, they are most commonly used as electronic
switches. The drain and source connections act as a switch, which is activated
when the gate connector sees a voltage above 2V. Unlike bipolar transistors FETs
are not dependent on the size of the base current, and can also generally switch
much higher currents. Whenever possible I would always use a FET as opposed to,
for instance, a Darlington driver or relay circuit, as it is generally cheaper and
always much easier to wire up!
+6V
1N4001
solenoid
Pin
0V
revolution
PICLOCK.P65
4k7
10k
10k
row 1
row 2
row 3
row 4
keypad
PIC 16F627
col 3
col 2
col 1
10k
1N4001
1
2
3
4
5
6
7
8
9
18
17
16
15
14
13
12
11
10
330R
solenoid
IRF 530
piezo
0V
For clarity the PICAXE-18 download circuit (22k resistor and stereo socket) is not
shown - see PICAXE manual for further details. 10k resistors are provided in SIL
format to reduce pcb size.
+5V
1k
pin 3
piezo
0V
Software Program
The programming of the lock can be illustrated by the simplified flowchart
below. The two main software tasks are the keypad scanning and the PIN
testing.
start
solenoid off
LED red
scan keypad
key hit?
yes
no
beep
pin sequence
correct?
no
yes
solenoid on
LED green
wait 5 sec
revolution
PICLOCK.P65
col 3 on?
yes
let y = y+1
no
col 2 on?
yes
let y = y+1
no
col 1 on?
yes
let y = y+1
no
let y = 3
switch row 2 high
yes
col 3 on?
let y = y+1
no
col 2 on?
yes
let y = y+1
no
col 1 on?
yes
let y = y+1
no
etc
revolution
PICLOCK.P65
x = 1?
yes
yes
no
x = 3?
yes
no
no
x = 2?
y = 9?
y = 3?
yes
no
yes
no
y = 5?
yes
no
y = 1?
solenoid on
LED green
let x = 0
wait 5 sec
continue
revolution
PICLOCK.P65
10
let x
let y
if 0 on
if 1 on
if 2 on
let y
if 0 on
if 1 on
if 2 on
let y
if 0 on
if 1 on
if 2 on
let y
=
=
if 0 on
if 1 on
if 2 on
let y = y
let y = y
let y = y
let x =
if x =
if x =
if x =
if x =
x
001
002
003
004
if y = 009
if y = 003
if y = 005
if y = 001
revolution
000
000
out
goto
goto
goto
003
out
goto
goto
goto
006
out
goto
goto
goto
009
out
goto
goto
goto
goto
040
25
24
23
01
reset counter X
preload Y value
switch row 1 on
test column 1
test column 2
test column 3
preload Y value
switch row 2 on
test column 1
test column 2
test column 3
preload Y value
switch row 3 on
test column 1
test column 2
test column 3
preload Y value
switch row 4 on
test column 1
test column 2
test column 3
scan again
plus
plus
plus
beep
plus
goto
goto
goto
goto
goto
001
001
001
150
001
34
36
38
40
00
add 3 to Y (1+1+1)
add 2 to Y (1+1)
add 1 to Y
make a noise
always add 1 to X
PIN digit 1 test
PIN digit 2 test
PIN digit 3 test
PIN digit 4 test
back to start
goto
goto
goto
goto
goto
goto
goto
goto
01
00
01
00
01
00
43
00
PIN
not
PIN
not
PIN
not
PIN
not
out
wait
goto
144
050
00
solenoid on
wait 5 seconds
back to start
033
25
24
23
034
25
24
23
036
25
24
23
is 9
9 so
is 3
3 so
is 5
5 so
is 1
1 so
- continue
reset X
- continue
reset X
- continue
reset X
- finished
reset X
PICLOCK.P65
11
PICAXE-18 Program
A full program listing for the PICAXE-18 programming system is as follows,
(this file is saved in the PICAXE samples sub-folder as CHI008 - Keypad.bas):
' Keypad Lock For PICAXE-18
symbol b1 = key_pos
symbol b2 = key_value
' *** reset position to zero ***
init:
let key_pos = 0
' *** now scan each row in turn ***
' *** by setting only 1 row (and LED) high ***
' *** if a switch is hit jump call score sub below ***
scan:
let key_value = 0
let pins = %00010001
gosub key_test
let key_value = 3
let pins = %00010010
gosub key_test
let key_value = 6
let pins = %00010100
gosub key_test
let key_value = 9
let pins = %00011000
gosub key_test
goto scan
' *** Score sub procedure. ***
' *** return straight away if no key pressed ***
key_test:
if pin0 = 1 then add1
if pin1 = 1 then add2
if pin2 = 1 then add3
return
' *** key value will already be 0, 3, 6, or 9 ***
' *** so add 1, 2 or 3 to this value ***
add3: let key_value = key_value + 1
add2: let key_value = key_value + 1
add1: let key_value = key_value + 1
' *** Make a beep ***
sound 6,(60,50)
revolution
(continued overleaf)
PICLOCK.P65
12
'
if
if
if
if
key_pos
key_pos
key_pos
key_pos
=
=
=
=
1
2
3
4
then
then
then
then
test1
test2
test3
test4
' *** Now test the value for each position individually ***
' *** If value is wrong restart, if correct loop until ***
' *** fourth go. If fourth is correct open lock! ***
' *** Key code is set to 9-3-5-1 ***
test4: if key_value = 1 then open
goto reset
test3: if key_value = 5 then continue
goto reset
test2: if key_value = 3 then continue
goto reset
test1: if key_value = 9 then continue
goto reset
' *** Got here so open lock and set LED green for 5 sec ***
open: let pins = %10100000
wait 5
goto reset
' *** Not correct value so reset position counter then return ***
reset:
let key_pos = 0
' *** Okay so continue by returning back to main loop ***
continue:
return
revolution
PICLOCK.P65