Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 17 of 47 · Home automation · Intermediate

Smart door lock

A servo turns a bolt. Press the button to unlock the door for five seconds, then it locks itself again. Red and green LEDs show the state.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Smart door lock: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Lock servoServo
Unlock buttonPush button
LockedLED
UnlockedLED

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1ColouredLock servo - signalPin 11 (GPIO17)
2RedLock servo - 5 VPin 2 (5V)
3BlackLock servo - GNDPin 6 (GND)
4ColouredUnlock button - SignalPin 13 (GPIO27)
5BlackUnlock button - GNDPin 9 (GND)
6ColouredLocked - anode +Pin 16 (GPIO23)
7BlackLocked - cathode -Pin 14 (GND)
8ColouredUnlocked - anode +Pin 15 (GPIO22)
9BlackUnlocked - cathode -Pin 20 (GND)

Python code (gpiozero)

from gpiozero import AngularServo, Button, LED
from time import sleep

bolt = AngularServo(17)          # 0 degrees = bolt in, 90 degrees = bolt out
button = Button(27)
red = LED(23)
green = LED(22)

def lock():
    bolt.angle = 0
    red.on()
    green.off()
    print("Door LOCKED")

def unlock():
    bolt.angle = 90
    red.off()
    green.on()
    print("Door UNLOCKED")

lock()
while True:
    button.wait_for_press()
    unlock()
    sleep(5)                     # stay unlocked for 5 seconds
    lock()

How the code works

You will learn

  • Putting actions in functions (lock and unlock)
  • State shown with LEDs
  • Waiting for a press in a loop

Try this

  • Press the button and watch the servo, both LEDs and the console.
  • Change the open time to 10 seconds.
  • Make it need a double press within 2 seconds before it unlocks.

On a real Pi

  • A hobby servo is weak. Use a proper electric strike or a lock motor for a real door, driven through a relay or driver.
  • Power the servo from its own 5 V supply and share GND with the Pi.
  • A lock needs a manual key or a battery backup. Plan for power cuts.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
55 mm LED, green1Robu.in Amazon.in ThinkRobotics
6330 ohm resistor, 1/4 W
One per LED
2Robu.in Amazon.in ThinkRobotics
7Tactile push button (12 mm, with cap)1Robu.in Amazon.in ThinkRobotics
8SG90 micro servo1Robu.in Amazon.in ThinkRobotics
9Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
10Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 16. Automatic plant wateringAll projects18. Home alarm system →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.