Introduction: Magnetic Smart Lock With Secret Knock, IR Sensor, & Web App

About: I like to build cool stuff

If you like this project please follow me on Instagram and YouTube.

In this project I’m going to build a magnetic lock for my home office, that opens if you know the secret knock. Oh… and it’s going to have a few more tricks up it’s sleeve too.

Magnetic locks are common in office buildings, and kits can be purchased easily online. However, I wanted to build a custom setup, sense this will be installed on an interior door in my house.

Initially there will be three ways to open the door: an IR sensor on the inside, a web app, and a piezo transducer that can detect vibration in the door.

Parts (Affiliate Links)

Microcontroller Code & Wiring Diagram:

Web App Code:

3D Models

49mm Electromagnet Mounting Bracket: Sensor Cap/Cover:

Project Box:

Step 1: The Circuit

Basically, the ESP8266 dev board takes in 9 volts from a power supply to its onboard regulator. The positive from the power supply goes to the magnet, and the ground goes to the source on the mosfet. The drain from the fet goes to the magnet, and the gate on the FET is opened by pin 5 on the micro controller. This lets the 9v flow to the magnet when the pin is on. The opamp takes in the analog signal from the transducer, amplifies it, and sends it to the analog pin. The IR sensor sends a digital signal (In other words on or off) to pin 14. The opamp, and the IR sensor both get 3.3v power from the micro controller. Oh and everything gets grounded. I found that using 9v instead of the magnets rated 12v lets in run cooler, while still being plenty strong, especially seance I’m using that thick steel plate. Also the regulator on the micro controller can’t handle much more than 9v. You will also need to add a resistors, and the diodes where they are shown in the diagram.

I want to note here that depending on were you are placing the piezo vibration sensor, and how long the wires are to it, you may not need the op-amp. You can just run the outer ring of the sensor to ground and the other wire to the analog input, with a 1M resistor between the wires. The op amp is just amplifying the single.

Step 2: Microcontroller Code

Normally Arduino would be used for a project like this, but I’m going against the grain here and using a firmware called Espruino, which lets you run javascript on microcontrollers. If you’re curious, I did a whole video on flashing the Node MCU ESP8266 dev board with Espruino. You should check it out.

See the code on GitHub

At the top I setup some constants, like: what pins are, used and an array of timings in milliseconds for the secret knock. This is the time between each knock. I also setup functions for unlocking and locking the door, as well as checking for the correct knock. When the board starts up it connects to wifi and creates a web server that can receive commands to control the door. A watch is set on the pin connected to the IR sensor, so the unlock function will be fired when the sensor is tripped. As far as the vibration sensor goes… an interval is started that reads the analog pin that the vibration sensor is connected to every millisecond, and if the signal is above a set threshold the timing is captured. If there are enough vibrations captured, it will run the function that checks if the captured timings match the secret timings close enough. If they do, It will open the door.

Step 3: Web App Controls

Web app code

The web app is just a web page with some javascript that sends commands to the web server we created on the microcontroller. I made it a static website on AWS S3, and saved it to the home screen of my phone. Now I can unlock the door, lock the door, or leave it unlocked. It would also be possible to secure the app, and setup my network so I can operate the door form anywhere with an internet connection.

You will need to change the IP address that is used in the code to the one for your microcontroller. I made my router reserve the IP, so it would never change.

Step 4: Electromagnet Mounting Bracket

I went to Fusion 360, and created a bracket to fit the 49mm electromagnet's its dimensions. Here is a link to the model. I then sent it over to the 3D printer. Once that amazingly long process was done, I gave it a coat of primer, sanded the crap out of it, and hit it with some white paint.

Step 5: Mounting Magnet and Plate

To make sure the magnet was going to line up on the steel plate correctly; I covered the plate in blue tape, traced the bracket on to it, then market where the mount holes should go.

When drilling through hard metal it's a good idea to start with a small bit and work your way up. Also, use oil to lubricate the drill bit.

I have a hollow door, so I ran log bolts all the way through it, and put large washers on the other side to make sure it wouldn't pull through.

I used wood screws to mount the bracket with the magnet to the the frame. I then soldered a long wire to the wires on the magnet, and ran the wires through a long piece of white sheathing. On the inside, I ran the wire around the door facing, and down to were the control box would be.

Step 6: The Controls Box

The controls box is just a super simple box with a lid that I modeled and printed. There are holes on the two short ends to let the wires run through. The circuitboard just sits inside it, and the IR sensor’s LEDs stick out through holes I drilled in the side.

Here is the model.

Step 7: Vibration Sensor & Project Completion

To hook up the vibration sensor, I attached another long piece of wire that I ran through white sheathing. To mount it to the door, I used hot glue. I covered the sensor with a 3D printed cap to keep things looking pretty.

After that was done I soldered the wires for the magnet and the vibration sensor to their respective wires on the circuitboard.

After taping down the door latch, which I eventually removed all together, and doing some clean up the project was complete!

Please see the video so see how this project works.

First Time Author

Runner Up in the
First Time Author