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

16,080

225

18

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:https://github.com/calebbrewer/secret-knock-magne...

Web App Code: https://codepen.io/calebbrewer/pen/dJKBmw

3D Models

49mm Electromagnet Mounting Bracket: https://codepen.io/calebbrewer/pen/dJKBmw Sensor Cap/Cover: https://codepen.io/calebbrewer/pen/dJKBmw

Project Box: https://codepen.io/calebbrewer/pen/dJKBmw

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

This is an entry in the
First Time Author

Share

    Recommendations

    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • PCB Contest

      PCB Contest
    • First Time Author

      First Time Author

    18 Discussions

    0
    None
    TheRadMan

    23 days ago

    Caleb
    At 9 Volts D.C., how much current does that electromagnet continually draw? I see the ebay specification is 10W, and it may be true that the component draws 75% of full 12V power at that reduced voltage. I see you mounted the N-ch MOSFET to a heatsink as well; how much heat dissipation is observed during the MOSFET normal ON state? I ask because your DIY mag-lock may be useful (cost effective) in escape room design and construction.
    best regards
    JRad (OACETT)

    3 replies
    0
    None
    CalebB57TheRadMan

    Reply 22 days ago

    This would be really cool for an escape room! I hadn't thought about that.
    The mosfet really doesn't need the heatsink. I just had it, and thought I would put it on. It doesn't get hot at all. I'm not sure how many mA it draws. I will test it. It can't be that much though. This magnet works great, but it should never be powered with 12V. It gets way too hot with 12V.

    1
    None
    TheRadManCalebB57

    Reply 9 days ago

    400N 40kg 49mm force Electric Lifting Magnet just came in from China. Similar to the one in your post, same size. Half the landed price, twice the shipping delay.
    Ebay seller packaging indicates 24V @ 5 Watts continuous. Moderately warm: core surface ~35C over about 1 hour. When trying to manually pry a round flat washer away, I feel a very strong magnetic holding force. At 9V; the holding force is diminished. Definitely cheaper than commercial models, with your 3D housing.
    Starting that EscRoom build soon.

    0
    None
    CalebB57TheRadMan

    Reply 6 days ago

    Awesome! If you want to run that magnet at, or just under it's rated voltage you could use something like a 19v PSU from a laptop, then get buck converter to bring the input voltage down to around 5v for the microcontroller.

    0
    None
    kgiori

    22 days ago

    Well done project! I'd like to see your javascript augmented to include a webthing definition (https://iot.mozilla.org/things/), per the schema proposed by Mozilla (https://iot.mozilla.org/schemas). Then it can also be connected to the Mozilla IoT Gateway (https://iot.mozilla.org/gateway/), which has a cool rules system to enable interaction with other IoT devices connected to the gateway. Selfishly, then it would be worth my while to try to copy your awesome design. :)

    1 reply
    0
    None
    CalebB57kgiori

    Reply 22 days ago

    I'm glad you like it. I will look into WebThings. I had never heard of it before. Sounds cool!

    0
    None
    LlewellynD1

    23 days ago

    Wonderful idea!
    And grrreat for Security as well.
    TYVM

    1
    None
    MillerI

    24 days ago

    What a great 'ible! I think I'll make mine using an electric strike rather than an electromagnet. It's more energy efficient, controls the door just like the original latch and strike do and, I don't need to screw anything to the outside of the door frame. Also, it's easy to configure the strike with fail-safe or fail-secure on lost power.

    I install electronic access devices regularly and I save the magnets for glass doors and doors that don't normally latch (cooler doors, double swinging, etc.).

    2 replies
    0
    None
    CalebB57MillerI

    Reply 23 days ago

    I'm glad you liked it! An electric strike would be better in this case. I just got really fascinated with electromagnets, and wanted to do it all custom.

    0
    None
    MillerICalebB57

    Reply 23 days ago

    I get that. Sometimes I wonder what it's like to actually reinvent the wheel instead of just buying a cheap one off the shelf. I think you probably could have rigged up a little Servo motor with a locking tab that would simply block the door closed.

    Magnets are fascinating to work with though.

    0
    None
    AndrewB637

    25 days ago on Step 7

    Nice design. The only concern I have is when the power is lost, then the electromagnet is released.

    1 reply
    0
    None
    CalebB57AndrewB637

    Reply 23 days ago

    Thanks! You could do a battery backup easily, but this is just on an interior door. It's meant to stop people from wandering into my home office. It has actually impressed me how much I use it, and how well the knock code works.

    0
    None
    John Peter RobertS

    24 days ago on Step 7

    Nice done.
    But why use a supper box?
    Ha ha, i know what it meant.
    I would have made a bracket of aluminium instead of plastic.
    But yours look good.
    Best regards from Peter

    1 reply
    0
    None
    CalebB57John Peter RobertS

    Reply 23 days ago

    haha. I just fixed that. I'm glad you liked it. I didn't really have a way to make the bracket out of metal at the time, but that would be a lot stronger.