Electronic Password Lock System

One entry, from finger to verdict

A finger closes one contact on the keypad. At that instant the scanner is driving one row of the pad and reading its columns, and the column that reads low, together with the row being driven, names the key. The contact is still bouncing, so nothing happens yet.

On the next few passes, the same column reads the same way. Only then does the scanner accept the key, and it emits one event, on the edge of the press, no matter how long the finger stays down. The entry logic appends the digit and writes it to the display register. The display picks up the change on its own schedule; the lock does not wait for the digit to appear.

When the entry is submitted, it is compared with every stored password at once, and the comparison yields two independent answers: whether it matched a user password, and whether it matched the administrator password. Those two answers choose what happens next.

That path is the whole design of a combination lock I built on a Xilinx FPGA, written in VHDL and developed in Vivado. Input comes from a matrix keypad, and everything the lock says goes out on a multiplexed seven-segment display driven directly from the logic fabric. Each decision along the way is made in exactly one place: the scanner decides what a press is, the comparison decides what an entry matched, and one state machine decides what follows.

Every capability is an edge

The lock's behavior is small enough to state in full. It accepts one digit at a time and shows what it has taken. It holds several user passwords and one administrator password, and it tells them apart: any user password opens the lock, while the administrator password opens it and also leads to the state where passwords are set. A new password is entered twice and saved only when the two entries agree. Three wrong entries in a row latch an alarm, and only the administrator password clears it. Everything that has been set survives a power cycle.

The lock's state machine. Locked goes to Entering on a key press, Entering goes to Checking when the entry is submitted. A wrong entry with fewer than three tries returns to Entering; the third wrong entry goes to Alarm. A correct user password goes to Open, which returns to Locked on timeout. A correct administrator password goes to Admin, from Checking or from Alarm. Admin can unlock, or go to Set new and back once the new password has been entered twice and matched.
Every capability of the lock is a transition in this graph, and the alarm has exactly one way out.

I turned that list into a single state machine, and stating it completely is what made the rest of the design fall into place. A key press moves the machine out of Locked into Entering, where digits accumulate. Submitting moves it to Checking.

What happens next depends on the comparison's two answers, and keeping them separate is what makes the machine work. A user match opens the lock. An administrator match opens it and reaches the administrator state, where passwords can be set and changed. A wrong entry raises the attempt count and returns to Entering, and the third one moves the machine to Alarm.

Alarm is a latched state, and the only edge leaving it is guarded by the administrator match. A user password does not clear it, retrying does not clear it, and letting the attempt counter roll over does not clear it, because none of those transitions exists. The security of the lock is the shape of the graph, not a rule enforced somewhere in the logic, and because every capability is an edge of the same graph, none of them can be satisfied in one place and quietly undone in another.

Three circuits on three clocks

On a microcontroller, a lock like this would be a main loop that scans the keypad, refreshes the display and decides what to do, one after another. An FPGA has no loop. The keypad scanner, the display multiplexer and the lock's own logic are three separate pieces of hardware running at the same time.

Their timing comes from one counter running off the system clock and tapped at several points. Each block takes its tick from a different tap, so the rates are fixed by wiring rather than by anything that could be recomputed while the lock runs. Data moves in one direction, from scanner to state logic to display, through a handful of registers.

This structure is the design, not an implementation detail. The entry logic never waits on the display, so a key press cannot be missed while a digit is being refreshed, and the display never waits on the lock, so it stays lit and steady while a password is being compared. The state logic is the only block that decides anything; the other two only report what they see and show what they are given.

The keypad on the headers

The development board carries the seven-segment display, a row of indicators and the expansion headers, but no keypad. A matrix keypad module is wired to the header on the board's right-hand edge, a row line for each row and a column line for each column. That is the lock's entire external interface, and it is why the scanner had to be part of the design rather than something the board provided.

The development board: a six-digit seven-segment display along the lower left, four user keys and a reset key across the bottom, the FPGA in the middle, and two forty-pin expansion connectors down the left and right edges.
The board the lock runs on, with its seven-segment display, its user keys and the two expansion connectors that everything else attaches to.

The scanner drives one row low at a time and reads all the columns, so a key costs no wire of its own. It runs on a divided clock slow enough that a mechanical contact has stopped bouncing by the time it is sampled, and a key counts only once its column reads the same way on consecutive passes.

A key is registered once, on its edge, not for as long as it is held, so holding a key produces a single digit. That one choice keeps everything downstream simple: the entry logic receives a stream of key events, never a picture of which keys happen to be down.

A display as the only voice

The lock has no screen and no serial port, so the seven-segment display is the whole of its language. The decoder covers letters as well as digits, which lets the same display show both an entry and the lock's state without a second indicator.

Only one digit is lit at a time, and the digits are cycled fast enough to look continuously lit. The segment lines are blanked between digits, so one digit's pattern never flashes faintly on the next. The multiplexer runs from its own tap of the clock divider and never pauses for anything the rest of the design is doing.

Where the passwords live

The user passwords are stored together with the administrator password, so checking an entry means comparing it against the whole set at once, and setting or changing a password rewrites one slot while leaving the others alone. A new password is taken twice and saved only when both entries agree, so a typo cannot lock a user out of their own lock. Everything set is written to the board's flash memory, so after a power cycle the lock comes back as it was configured, not reset to a default.