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.

Parts in this circuit
| Part | Type |
|---|---|
| Lock servo | Servo |
| Unlock button | Push button |
| Locked | LED |
| Unlocked | LED |
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.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Coloured | Lock servo - signal | Pin 11 (GPIO17) |
| 2 | Red | Lock servo - 5 V | Pin 2 (5V) |
| 3 | Black | Lock servo - GND | Pin 6 (GND) |
| 4 | Coloured | Unlock button - Signal | Pin 13 (GPIO27) |
| 5 | Black | Unlock button - GND | Pin 9 (GND) |
| 6 | Coloured | Locked - anode + | Pin 16 (GPIO23) |
| 7 | Black | Locked - cathode - | Pin 14 (GND) |
| 8 | Coloured | Unlocked - anode + | Pin 15 (GPIO22) |
| 9 | Black | Unlocked - 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
- lock() and unlock() hold all the steps of one action, so the main loop stays short and easy to read.
- wait_for_press() pauses the program until the button is pressed. This does not waste power.
- After 5 seconds the code calls lock() again, so the door can never stay open by mistake.
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.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, red | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 5 mm LED, green | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | 330 ohm resistor, 1/4 W One per LED | 2 | Robu.in Amazon.in ThinkRobotics |
| 7 | Tactile push button (12 mm, with cap) | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | SG90 micro servo | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
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.