I am trying to figure out how to create a combination lock using logic gates, so that a password must be input (let's say pressing numbered buttons) in the correct order.

I understand logic gates completely, and have some of a grasp on flip-flops and latches, but I'm definitely getting stuck on how to use them in the right combination.

So as an example, if there are 3 buttons (call them 1, 2, & 3), I would like to be able to set a password such as 1 2 3 1 (hardcoded is fine), where they have to be input in that exact order, otherwise you have to start at the beginning again (1 2 3 3 3 3 1 will not work).

Any tips, links, hints on this?

  • 1
    \$\begingroup\$ Tangential comment: You can most likely implement the whole thing in a CPLD quicker, easier and cheaper than using discrete logic components. Added benefit is that the attacker cannot see how the components are wired together. The question is still very relevant though, as you have to construct the whole thing out of logic gates one way or the other. \$\endgroup\$
    – drxzcl
    Feb 16, 2011 at 9:24
  • \$\begingroup\$ @drxzcl - If the attacker has acces to your evaluation circuit you have lost anyway. There is a reason why decent access control systems transfer the output of the keyboard or badge reader to a secure place and evaluate it there. A CPLD? OK, as long as you have the programming equipment lying around anyway. \$\endgroup\$
    – Kitana
    Nov 16, 2014 at 10:52

3 Answers 3


I would probably use two bits to encode the three switches, and the general approach would be to have a string of pairs (one for each bit) of D or JK flip-flops to accept the sequence of input digits, essentially a shift register for pairs of bits. Then there would have to be de-bounce circuitry for the switches, and some way to generate a clock signal from the key presses (probably on release) to shift the digits through the register. Finally, XOR gates between the stages of the shift register and the digits of the key (hardwired to 1231 or use switches, etc) would produce signals to indicate when each digit is a correct match (logic Low on match), and then a multi-input NOR of the XOR outputs would give a final output signal that would indicate when the entire sequence in the shift register matches the key.

  • \$\begingroup\$ So you're saying make 1,2,3 into 00, 01, 10, then cycle through the password press by press, testing each press against the stored code in the shift register? \$\endgroup\$ Feb 16, 2011 at 3:38
  • \$\begingroup\$ not exactly- first press would enter a pair of bits to the 1st stage of the shift register. 2nd press would shift the 1st code to the 2nd stage, and enter the new code in the 1st stage, and so on, so that at any point in time, if you have say 5 stages of shift register, you have the last 5 button presses represented. This 'latest-5' would be checked against the key value all the time. I.e., the key is not stored in the shift register, the key is just hardwired inputs to the XORs, or some hidden switch inputs to them; the shift reg stores the user input. \$\endgroup\$
    – JustJeff
    Feb 16, 2011 at 11:53
  • \$\begingroup\$ Makes sense, thanks a lot! The shift register is the component I needed. \$\endgroup\$ Feb 16, 2011 at 18:38

A long long time ago I made a 'combination' lock with one button. It used a shift register. A long button press would shift in a 0, a short one would shift in a 1. (I used some resistors and capacitors to do this, but there are more elegant ways). The outputs of the shift register were connected to two inhibit lines using diodes. To open the lock, one inhibit line had to be pulled high (by a resistor), the other low. The code was determined by how the diodes connectd to the inhibit lines.

At that time I lived in a caravan. No toilet inside the caravan. The door automatically fell into the lock when I closed it behind me. Without the electronic lock I would have to dig up the spare key burried behind the 7th tree every time I forgot to take the keys with me when I went for a nightly visit to the loo.


(look at the circuit below)In this case you have to push E,F,G,H in sequence without touching Button B,C,D

The diode for logic gate are for self-locking after one logic gate get out put.

enter image description here

*from Make: electronics experiment 21


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.