0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Build a Smart Door Lock with Raspberry Pi

To build a smart door lock with Raspberry Pi, connect a servo motor to control the lock mechanism and use Python to write a program that opens or closes the lock based on input like a password or sensor. You can add a keypad or Bluetooth for user authentication and control the servo to lock or unlock the door.
📐

Syntax

The basic syntax involves importing the RPi.GPIO library to control the servo motor pins, setting up the GPIO mode, and using PWM (Pulse Width Modulation) to rotate the servo to lock or unlock positions.

  • GPIO.setmode(GPIO.BCM): Sets pin numbering mode.
  • GPIO.setup(pin, GPIO.OUT): Sets the servo pin as output.
  • pwm = GPIO.PWM(pin, 50): Initializes PWM on the pin at 50Hz.
  • pwm.start(duty_cycle): Starts PWM with a duty cycle to position the servo.
  • pwm.ChangeDutyCycle(duty_cycle): Changes servo position.
  • GPIO.cleanup(): Resets GPIO pins after use.
python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
servo_pin = 18
GPIO.setup(servo_pin, GPIO.OUT)

pwm = GPIO.PWM(servo_pin, 50)  # 50Hz frequency
pwm.start(0)  # Initialization

def set_servo_angle(angle):
    duty = angle / 18 + 2
    GPIO.output(servo_pin, True)
    pwm.ChangeDutyCycle(duty)
    time.sleep(0.5)
    GPIO.output(servo_pin, False)
    pwm.ChangeDutyCycle(0)

# Example usage:
set_servo_angle(90)  # Move servo to 90 degrees

pwm.stop()
GPIO.cleanup()
💻

Example

This example shows a simple smart lock program that asks for a password and moves the servo to unlock or lock the door accordingly.

python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
servo_pin = 18
GPIO.setup(servo_pin, GPIO.OUT)

pwm = GPIO.PWM(servo_pin, 50)
pwm.start(0)

PASSWORD = "1234"


def set_servo_angle(angle):
    duty = angle / 18 + 2
    GPIO.output(servo_pin, True)
    pwm.ChangeDutyCycle(duty)
    time.sleep(0.5)
    GPIO.output(servo_pin, False)
    pwm.ChangeDutyCycle(0)

try:
    user_input = input("Enter password to unlock the door: ")
    if user_input == PASSWORD:
        print("Password correct. Unlocking door...")
        set_servo_angle(90)  # Unlock position
        time.sleep(5)  # Keep door unlocked for 5 seconds
        print("Locking door...")
        set_servo_angle(0)   # Lock position
    else:
        print("Incorrect password. Access denied.")
finally:
    pwm.stop()
    GPIO.cleanup()
Output
Enter password to unlock the door: 1234 Password correct. Unlocking door... Locking door...
⚠️

Common Pitfalls

  • Not setting the correct GPIO pin numbering mode (GPIO.BCM vs GPIO.BOARD) can cause pins to not work.
  • Incorrect duty cycle values can make the servo jitter or not move to the right position.
  • Forgetting to clean up GPIO pins with GPIO.cleanup() can cause errors on next run.
  • Not adding delays after changing servo position can cause incomplete movements.
  • Using input without validation can cause security risks; always sanitize inputs in real projects.
python
import RPi.GPIO as GPIO
import time

# Wrong way: Missing GPIO.setmode and cleanup
servo_pin = 18
GPIO.setup(servo_pin, GPIO.OUT)  # This will error because mode not set

pwm = GPIO.PWM(servo_pin, 50)
pwm.start(0)

# Correct way:
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo_pin, GPIO.OUT)

pwm = GPIO.PWM(servo_pin, 50)
pwm.start(0)

# Remember to call GPIO.cleanup() at the end
📊

Quick Reference

Summary tips for building a smart door lock with Raspberry Pi:

  • Use RPi.GPIO library to control servo motors.
  • Set GPIO mode to BCM for pin numbering consistency.
  • Use PWM to control servo angle for locking/unlocking.
  • Secure user input for authentication (password, keypad, Bluetooth).
  • Always clean up GPIO pins after use to avoid conflicts.

Key Takeaways

Use PWM on Raspberry Pi GPIO pins to control servo motor angles for locking and unlocking.
Always set GPIO mode and clean up pins to avoid hardware conflicts.
Secure user input methods like passwords or Bluetooth for safe access control.
Add delays after servo movements to ensure the lock mechanism completes its action.
Test servo angles carefully to match your door lock hardware requirements.