Build a Smart Auto‑Locking Door Using Your Smartphone’s Bluetooth Proximity
Looking for a reliable, tech‑savvy way to secure your home office or private workshop? This guide walks you through creating a DIY smart lock that automatically detects your presence via Bluetooth and locks the door when you step away.
The Concept
Your smartphone constantly broadcasts a unique Bluetooth identifier when in discovery mode. By monitoring this signal with a Raspberry Pi, we can determine whether the device is within range and trigger a relay to lock or unlock the door accordingly.
What You’ll Need
- Raspberry Pi – any model will suffice; I used an older Model B for its simplicity. Ensure it has network connectivity (Ethernet or Wi‑Fi), an SD card, and a micro‑USB power supply.
- Bluetooth USB adapter – Adafruit’s 4.0 BLE module works reliably, but any compatible dongle will do. The adapter only needs to send short pings, so transfer speed isn’t critical.
- GPIO breakout board (“cobbler”) – Makes pin connections clear; inexpensive and handy.
- Relay board – Choose a 4‑channel board rated for 12 V/5 A. My choice was a generic model costing around $5.
- 12/24 V electromagnet lock – I purchased a 180 kg holding‑force lock with mounting hardware for roughly $35.
- 12/24 V power supply – The lock must draw power from an external supply; never power it from the Pi.
- Python script (Lock.py) – We’ll develop this step by step.
Getting Started with Bluetooth
First, install the necessary Bluetooth packages on your Pi. You can do this directly on the device or via SSH.
sudo apt-get install bluez python-bluez
Insert the dongle and verify it’s recognized:
hcitool dev
Next, grab a sample Python script to discover nearby devices:
wget https://pybluez.googlecode.com/svn/trunk/examples/simple/inquiry.py python inquiry.py
If the script reports “0 devices found,” ensure your phone is in discovery mode. On an iPhone, open the Bluetooth settings page to enable it.
Creating the Detection Script
Create a file named detect.py and add the following code. Replace the MAC address with your phone’s identifier.
#!/usr/bin/python
import bluetooth
import time
while True:
print("Checking "+time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
result = bluetooth.lookup_name('78:7F:70:38:51:1B', timeout=5)
if result:
print("User present")
else:
print("User out of range")
time.sleep(10)
Run the script to see real‑time status updates. The 10‑second interval balances responsiveness with power consumption. If battery life is a concern, consider adding a switch that disables the loop while you’re inside.
GPIO Board Modes
Choose a pin numbering scheme that matches your hardware. The “board” mode uses the physical pin numbers, while “BCM” (Broadcom) refers to the GPIO channel numbers. I’ll use BCM in the examples below, which aligns with most breakout boards.
Wiring the Relay
Connect the breakout board to the Pi: 3.3 V and 5 V pins to the relay’s VCC and GND, respectively. Wire GPIO 23 to the relay’s input. Verify the relay’s logic level requirements; many accept 3.3 V signals directly.
To test the relay manually, install wiringPi and configure GPIO 23 as an output:
git clone git://git.drogon.net/wiringPi cd wiringPi ./build
gpio -g mode 23 out
Confirm the pin status:
gpio -g readall
When the relay clicks, you’re ready to integrate the lock. Keep the lock’s power supply separate from the Pi’s power rails.
Next Steps
Combine the detection script with GPIO control logic to trigger the lock when the phone is out of range. Test thoroughly in a controlled environment before deploying.
Manufacturing process
- Inside Bank Vaults: History, Design, and Future of Security
- Inductive Proximity Sensor: Circuit Design, Functionality, and Practical Applications
- Control an LED via Bluetooth with Arduino – Simple DIY Guide
- Smartphone‑Controlled Bluetooth Mouse: Build Your Own Virtual Joystick
- Arduino Keypad Door Lock with User-Defined Code
- Contact‑Free Faucet with Door‑Activated Control – Safe & Smart COVID‑19 Solution
- Build an IR Sensor Project with Arduino UNO – Simple Guide
- Secure Arduino‑Based Keyless Door Lock with LCD Display and 4×4 Keypad
- Build an Arduino RFID Door Lock – Master RFID Technology with a Step‑by‑Step Guide
- Build an Arduino Proximity Sensor: Step‑by‑Step Guide with Components