Things You'll Need
Instructions
Build a doorway that you wish to have a locking mechanism on if you haven't got one already. To do this, build a wall with a two-block-high and one-block-wide opening in it. Place a door in this opening. The side you place it from will be considered the "outside," or the side you are trying to lock out. If you place your door from the inside, the mechanism will not work properly. Your doorway should have a two-block width of wall clear on both the outside and inside as well to place the locking mechanism.
Place a button two blocks to the left or right of the door on the outside, on the second block up from the base. On the inside, and on the other side of where you placed the button, place a redstone torch, also on the second block up from the base.
Place three blocks extending out of your wall below the redstone torch. On top of the second block, place redstone dust and on the side of the second block facing toward your door, place another redstone torch. On top of the third block out from the wall, place a final redstone torch, and on the side facing the door again, a lever. Lastly, place redstone dust on the ground next to the second block and next to the first block.
Test your mechanism to make sure that you have placed everything correctly and that your lock works. While the lever is flipped up, the door is locked and will not open when you press the button on the outside. While the lever is flipped down, the door is unlocked and will open when you press the button.