Knock Detector

Greg Abbas

In the age of smart homes, wouldn't it be cool if you had a secret knock that your house could recognize, so you could unlock a door without any key, phone, or keypad?

If you connect a microphone to a microcontroller, it can listen for a knock with a certain rhythm and react appropriately. Apparently projects like this were fashionable a while ago (for example this one), but I never made one so I thought I would try my hand at it. Especially because consumer-level smart locks are so prevalent now that you don't even need to build the lock actuator yourself, just the detector part.

Here are the materials I used to build it:

Here's what the ESP32 board looks like.

I chose the ESP32 because it has onboard wifi, analog input, and enough horsepower to understand MQTT and JSON, but it's much simpler than a Raspberry Pi so there are fewer ways for it to fail.

Initially I just connected the sound detector to the ESP32 and powered it via the micro-USB jack, but unfortunately the ESP32's wifi radio produces way to much noise on its power rail for the sound detector (which is understably sensitive) to function. I tried slapping a big electrolytic capacitor from voltage to ground, but that didn't help. So instead I used a pair of 7805 regulators so that each board could have its own separate voltage rail, and then they're willing to co-exist happily.

The resistors on the ESP32's regulator are there to increase its voltage slightly, because by default the regular appears to want to put out only about 4.85 volts, and the ESP32 seems happier with about 5.1 volts. I designed the board to support an adjusted voltage for the sound detector too, but I didn't need to use it.

Although I've etched printed circuit boards (PCBs) myself before, mine always end up looking decidedly amateur. This time I wanted to design it in a CAD package and have a service manufacture it for me. For CAD, I picked Eagle because it gets recommended a lot. Unfortunately it has been acquired by Autodesk and integrated into Fusion 360, which means that it now requires that you use their cloud-storage service and deal with a bunch of idiosyncracies that they introduced as a result of cramming it into Fusion. It still gets the job done though. The schematic shown above was produced in Eagle, and here's the PCB design (with some wholly gratuitous silk screen graphics):

For manufacturing, after googling around a bit I settled on OSH Park because they seemed easy to work with and geared towards hobbyists like me. I was right, they did a great job. I uploaded my design and ordered their "After Dark" product. It was hard to wait 3 weeks for it to arrive, but they did a flawless job.

Those little spikes are due to the way they produce several smaller boards in one big manufacturing run and then break them apart, so I dremeled them off before soldering my parts on. I used female headers for the ESP32. The sound detector is held on by a little curved piece of plastic, so that when the whole contraption is mounted on a wall, the microphone will be pressed up gently against it.

Here's the ESP32 stuck on:

And the power connected. It would've been more elegant to use a barrel jack that could be soldered directly to the PCB, but I didn't have one of those so I used the parts I did have.

I didn't really want to screw my PCB directly to the wall, so instead I 3D-printed a small standoff bracket with holes for machine screws to hold the PCB, and holes for wood screws to go into the wall.

Assembled:

This unit then goes on the back of a wall near the door to be unlocked.

Software

Unfortunately my employer doesn't want me to post source code to the web, but I can describe what I did. I used the "MQTT" messaging protocol because I've found it a really nice way to integrate IoT components for home automation. The libraries for that and JSON that I found were designed for the ESP32 IDF SDK, not Arduino. And lately I've found Arduino to feel a bit amateurish anyway, so I thought I'd give IDF a try. It's harder to get working and has more rough edges, but works fine once you get the hang of it.

The algorithm just polls the sound detectors "envelope" on an analog GPIO pin as fast as it can, and notices every time it peaks. If it's been long enough since the last peak (100 milliseconds) then it records how long it's been since the previous peak. When there are enough of those events, it compares the array of timing measurements to the timing for a known rhythm, normalizes for tempo, and computes the mean squared error. For instance, this "happy birthday" rhythm is 220, 150, 360, 380, 380. If that MSE is less than some threshold, then it's considered a "match" and the software sends an MQTT message to whomever is subscribed. In this case, a quick-and-dirty iOS app that's programmed to provide audible feedback:

Resources

Here are the files:

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Happy making!

Questions or comments? Email me at .

2020 Oct 21