Family Encyclopedia >> Home & Garden

Build a DIY Smart Self-Locking Door with Smartphone Bluetooth Proximity Detection

Secure your home office or workshop effortlessly with this professional-grade DIY smart lock. It automatically unlocks when your smartphone is nearby via Bluetooth and locks securely when you leave—keeping children or unwanted visitors out without keys or codes.

How it works: Bluetooth proximity sensing. Your phone broadcasts its presence, detected by a Raspberry Pi, which controls a relay to engage the lock.

The Concept

Your smartphone constantly emits Bluetooth signals. Even in non-discoverable mode, a known device address allows pinging for presence. We'll use a Raspberry Pi with a Bluetooth adapter to monitor range (about 4m). When out of range, it triggers a relay to lock the door.

What You'll Need

  • Raspberry Pi (any model works; I'm using an older Model B). GPIO pinouts vary—check your model. Requires SD card, micro USB power, and network (Ethernet or Wi-Fi: Setting up wireless networks on your Raspberry Pi).
  • Bluetooth USB adapter (e.g., Adafruit Bluetooth 4.0 BLE or any basic dongle; speed irrelevant as we just ping for presence: How Bluetooth 4.0 is shaping the future).
  • GPIO breakout board ("T-cobbler") and jumper wires for easy pin access (~$6).
  • Relay board (4-channel, 5V/12V compatible, ~$5; microcontroller-safe, 12V/5A min).
  • 12/24V electromagnetic lock (180kg holding force, ~$35, with mounting plates).
  • Independent 12/24V power supply (never draw from Pi).
  • Custom lock.py script (built step-by-step below).

Setting Up Bluetooth

Bluetooth is core—install support and test your adapter via SSH (How to configure your Raspberry Pi for headless SSH).

sudo apt-get install bluez python-bluez

Check adapter:

hcitool dev

Scan for devices (one-time):

wget https://raw.githubusercontent.com/karulis/pybluez/master/examples/simple/inquiry.py
python inquiry.py

If no devices, ensure phone is discoverable (e.g., open Bluetooth settings on iPhone). Note your phone's MAC address from output.

Build a DIY Smart Self-Locking Door with Smartphone Bluetooth Proximity Detection

Create detect.py:

nano detect.py

Paste (replace MAC with yours):

#!/usr/bin/python
import time
import bluetooth
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 != None):
        print "User present"
    else:
        print "User out of range"
    time.sleep(10)

Save (Ctrl+X, Y), run: python detect.py. Toggle phone Bluetooth to test. Scans every 10s balance responsiveness and power use.

Build a DIY Smart Self-Locking Door with Smartphone Bluetooth Proximity Detection

As a seasoned Pi builder, note: Frequent scans may impact phone battery; add a manual toggle switch for inside use.

GPIO Pin Modes

Use BCM mode (GPIO numbers, e.g., on breakout boards) or BOARD (physical pins). This guide uses BCM (matches my Adafruit cobbler).

Build a DIY Smart Self-Locking Door with Smartphone Bluetooth Proximity Detection

Check your model: Rev1/2, B+, Pi2 vary (StackExchange pin diagrams).

Wiring the Relay

Connect cobbler: Pi pins 1/2 to 3v3/5V0. Test voltages.

Build a DIY Smart Self-Locking Door with Smartphone Bluetooth Proximity Detection

Wire relay: 5V0 to VCC, GND to GND, GPIO23 (BCM) to IN1. Test relay compatibility (some need 5V; mine works at 3.3V: Getting Started with GPIO on Raspberry Pi).

Install wiringPi:

git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

Set GPIO23 output:

gpio -g mode 23 out
gpio -g readall

Build a DIY Smart Self-Locking Door with Smartphone Bluetooth Proximity Detection

Test: gpio -g write 23 1 / 0. Hear click? Good.

Install Python GPIO: sudo apt-get install python-dev python-rpi.gpio

Copy to lock.py, add:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
RELAY = 23
GPIO.setup(RELAY, GPIO.OUT)

In if/else:

GPIO.output(RELAY, 1)  # Adjust for your relay's active state

Run: sudo python lock.py. Full code: Gist.

Installing the Electromagnet Lock

Relay: 12V+ to COM, lock+ to NO, GND common.

Build a DIY Smart Self-Locking Door with Smartphone Bluetooth Proximity Detection

Mount per instructions; use brackets for thin/inward doors.

Build a DIY Smart Self-Locking Door with Smartphone Bluetooth Proximity Detection

Enhancements and Notes

This reliable proof-of-concept secures casual access. For robust security: add UPS battery, deadbolt, and physical barriers.

Bluetooth proximity extends to garage doors or lights. Share your builds or issues in comments—happy to assist from my years of Pi projects!