The basic problem behind this project was that keys are something very easy to forget or lose, and they are the only way into my dorm room. This is especially a pain when leaving the room for a short time, such as to go check mail or give something to a friend. It's a bad idea to leave the room unlocked, but a pain to take keys. A solution to this would be to provide another method of access with something easier to carry, or not even needing anything to carry anything at all. This must also be accomplished without modifying the door in any unauthorized way.
Step 1: Solution
A device will be attached to the door that is able to lock and unlock the door by pulling the lock. It will do this when the correct number is typed in, or an ID is scanned. A school idea is much easier to carry around, and is required to get into the building anyway. And even without it, typing in the correct number requires carrying nothing additional.
Step 2: Parts List
SX48 Proto Board ($15) - http://www.parallax.com/Store/Microcontrollers/SXProducts/tabid/138/CategoryID/15/List/0/SortField/0/Level/a/ProductID/362/Default.aspx - This is the microcontroller that is the 'brain' of the project. Almost any microcontroller would work just as well.
Magnetic Card Reader ($3.50) - http://www.allelectronics.com/make-a-store/item/MCR-12/MAGNETIC-CARD-READER/-/1.html - This is the card reader that was used to read the school IDs.
Keypad ($7.50) - http://www.allelectronics.com/make-a-store/item/KP-12/12-BUTTON-KEYPAD/-/1.html - This is the keypad that is used to type in passwords.
Servo ($5) - This is what unlocks the door.
Screen (???) - The screen to display the currently typed code. This was recycled from an older device and therefore not purchased.
Step 3: Magnetic Card Reader
The first step in this project is establishing how the card reader actually works. The datasheet can be found here: http://www.allelectronics.com/mas_assets/spec/MCR-12.pdf
The two most important data lines are RDT and RCT, which are data and clock respectively. The way this card reader sends what it's reading is using these two pins. When the clock line goes from 1 to 0, that means the next bit of the card is present on RDT. There is an example in the first picture.
The RDT line is inverted, meaning when the clock goes from 0 to 1, if a 0 is present on RDT, that means there's a 1 on the next bit of the card. The example above shows what might happen if the data "1101" was on the card. Note that the frequency of the clock pulses are purely dependent on the rate in which the card is scanned. They are not necessary uniform width.
The next part after grabbing the data is interpreting it. Most magnetic cards use the same format, including the school IDs. There is a picture of example data provided.
The data is in groups of 5 bits with 4 data bits and one odd parity bit. The first bit is a semi-colon, or a 'start sentinel'. It has the value of 11, and signifies the beginning of the card. There is also one parity byte at the end of the card. For this simplified design, the parity bits are ignored, because any misread card will simply result in an access denied error.
Step 4: Keypad
The keypad was probably the simplest thing to interface. It has a pin for every button plus one for ground. When a button is pressed, that pin is grounded. Connecting every pin to the microcontroller along with the microcontrollers internal pull-up resistors was enough to easily get which keys were pressed.
Step 5: Servo
This is what connects directly to the doors lock. This specific servo was controlled by sending specific frequency pulses into one of its pins. The appropriate frequencies were found by experimentation and then programmed into the microcontroller. Thus this was a very simple solution to unlocking the door.
Step 6: Screen
The screen was recycled from another old device. The protocol for the screen is luckily very common and very well documented. It even has its own Wikipedia page here: http://en.wikipedia.org/wiki/HD44780_Character_LCD. It works similar to the magnetic card reader in that there's a clock pin and data pins. The difference is there are more than one data pins that are used so that entire bytes can be sent at once. Also, communication is only one way; from the microcontroller to the screen. There are a few 'command' bytes and a data bytes. Command bytes are used for things like moving the cursor or clearing the screen. Whether or not a byte is a command is controlled by the R/S or Register Select pin of the LCD. If sending data, the byte is written to the screen where the cursor is and it uses standard ASCII text. I won't go too much into the details of the screen as it's very thoroughly documented elsewhere. For further reading, see http://home.iae.nl/users/pouweha/lcd/lcd.shtml
Step 7: Software
The last part of it all is creating the software that ties it all together. Unfortunately, the SX has a very limited amount of memory to work with so it required a few tricks to make the software work.
There is a flow chart of the design.
Some important things to keep in mind: Because of the limited memory of the SX48, it's not a good idea to attempt to store the entire data of the card or the entered key. The simple solution to this is to have a byte that remembers what 'place' it's on in the key or card. When a mistake is made, that is remembered, and the place variable is increased. That way, it only needs to remember if there was ever a mistake, and if there wasn't, and the end of the key/card is reached, the data must be OK. For security reason, it's very important that the system keep letting you scan a card or type a key once it determines a mistake has been made. Otherwise, it would be easy to break such a system by trying different keys. With all of these design considerations in mind, the system will remain completely secure while at the same time not need to worry about the limits of the hardware.
Step 8: Conclusion
The end result of the project is a fully working door that can be opened from without a key. The total cost of the project is under $40 which is a very good price for the final result. A lot of enhancements could easily be made. For example, the system currently is not able to relock your door, but such a thing would be trivial to add into the code. It wouldn't be too difficult to add some different ways of unlocking the door, such as texting a number or making a microphone to listen for a secret knock.