Introduction
This program is a simple combination lock that I designed for an old Sentryยฎ fire safe that was given to me without any lock electronics. I created a front panel with a keyboard and microprocessor with a six-digit combination sequence.
Details
The LED flashes to show itโs alive after power is applied.
The lock solenoid requires 6VDC at approxmately 2 amps. Given that a small 9V battery is used and wouldnโt easily provide that current for very long, I used the PICโs PWM output to vary the current in two phases:
- It starts with a 30mS โpickโ phase that delivers full current by providing a 100% duty cycle to the FET driver which puts a solid 9V across the solenoid.
- Thatโs followed by a โholdโ phase of 1.4 seconds at a reduced duty cycle that reduces current to significantly less.
The initial combination is โ123456โ. Regardless of any immediately prior key sequence, when that correct key sequence is entered and completed, the lock solenoid is actuated for 1.5 seconds. If a key isnโt pressed for two or more seconds, the firmware resets to again look for the first digit.
Caveats
Since this circuit is battery powered, some way is required to turn on the battery when needed. I used a toggle switch below the keypad to accomplish this, but more innovative methods would be more elegant. For instance, one could employ some sort of mechanical cover over the keypad whereby lifting the cover would actuate the switch to connect the battery.
Code:
/**************************************************************************** SAFE_COMBO_02.C PIC 16F628 Combination Lock โ resets to first expected digit two seconds after last key is pressed โ uses PWM to reduce current to coil after initial pulse โโโ โโโ- +5 โ14-|VCC B0|-17โโ|C1 | | B1|-18โโ|C2 | Gnd โ5-| B2|-1โโ|C3 | 6MHz XTAL โ16-| B4|-2โโ|R1 KBD | XTAL โ15-| B5|-6โโ|R2 | | B6|-7โโ|R3 | | B7|-8โโ|R4 | | 16F628 | โโโ- | | | B3|-6โ LOCK SOLENOID | A1|-18โ LED โโโ ========= | 1 2 3 | R0 | 4 5 6 | R1 | 7 8 9 | R2 | * 0 # | R3 ========= C0 C1 C2 Keyboard connector is (backside): C2 C1 C0 R3 R2 R1 R0 Oscillator = 6MHz crystal Jon Fick 03/09/07 ***************************************************************************/ #include <16F628A.h> #include // Set configuration bits in the PIC processor #fuses HS, NOPROTECT, NOWDT, PUT, BROWNOUT, NOMCLR, NOLVP #define COMBO_1 โ1โ #define COMBO_2 โ2โ #define COMBO_3 โ3โ #define COMBO_4 โ4โ #define COMBO_5 โ5โ #define COMBO_6 โ6โ #define KBD_C1 PIN_B0 #define KBD_C2 PIN_B1 #define KBD_C3 PIN_B2 #define KBD_R1 PIN_B4 #define KBD_R2 PIN_B5 #define KBD_R3 PIN_B6 #define KBD_R4 PIN_B7 #define LOCK_OUT PIN_B3 #define LED PIN_A1 #define NOKEY 0xFF #define RESET_KBD_COUNT 46 // at 23 counts/second #define LOCK_HIGH 255 // full PWM duty cycle #define LOCK_HIGH_TIME 30 // mS at full coil current #define LOCK_MED 20 // partial PWM duty cycle #define LOCK_MED_TIME 1450 // mS at partial coil current #define LOCK_OFF 0 // no PWM current #use delay ( clock = 6000000 ) // sets appropriate compiler constants #use standard_io ( A ) #use standard_io ( B ) char GetKey ( void ); // prototypes static char cKbdTimeoutFlag, cLedCount; #int_rtcc void TimerInterrupt ( void ) // 43mS tic, 23/second { if ( cKbdTimeoutFlag != 0 ) { cKbdTimeoutFlagโ; // count down to zero } if ( cLedCount++ > 12 ) { cLedCount = 0; } if ( cLedCount > 6 ) { output_high ( LED ); } else { output_low ( LED ); } } void main ( void ) { delay_ms ( 100 ); // power up delay output_low ( LOCK_OUT ); // turn off lock solenoid */ output_low ( LED );
For more detail: COMBINATION LOCK FOR SAFE using PIC16F628