Inconsistant functionality on diy smart lock project with ESP32 board and servo

I recently started a project based on this blog write up: Making a Dumb Lock Smart with DIY Hardware and ESPHome

I’m using the same servo, lock, and 3d printed parts as a base. The blog post uses an ESP8266 board, I’ve opted for an ESP32 board for additional bluetooth functionality. I’ve modified the code for the upgraded board and implemented the newer ESPhome lock component.

Everything seems to work initially, occasionally the servo sticks and is non responsive for random periods of time. Waiting and toggling lock unlock fixes the issue sometimes. Here’s the code:

esphome:
  name: rear-lock

esp32:
  # default board type
  board: esp32dev
  framework:
    type: arduino

output:
  - platform: ledc
    id: lock_servo
    pin: GPIO27
    frequency: 50Hz
    
servo:
  - id: servo1
    min_level: "2%"
    max_level: "12.6%"
    output: lock_servo

lock:
  - platform: output
    # id is for firmware call reference
    id: rear_lock
    # name is for esphome reference
    name: "rear_lock"
    output: lock_servo
    # on_lock action to take when lock is locked component call: -lock.lock: rear_lock
    on_lock:
      - servo.write:
          id: servo1
          level: -0.9
      - logger.log: "Door Locked!"
    # on_unlock action to take on component call: -lock.unlock: rear_lock
    on_unlock:
      - servo.write:
          id: servo1
          level: 0.3
      - logger.log: "Door Unlocked!"
    

binary_sensor:
  - platform: gpio
    # id used as reference / alias
    id: lock_button
    # input pin
    pin: 
      number: GPIO5
      mode: INPUT_PULLUP
    on_press:
      # lock usage
      - lock.lock: rear_lock
    on_release:
    #lock usage
      - lock.unlock: rear_lock  
     
# Enable logging
logger:

# added in project
debug:

# Enable Home Assistant API
api:
  encryption:
    key: ""

ota:
  password: ""

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

captive_portal:

observations:
-Log doesn’t show loss of power due to servo load
-Log shows binary_sensor input signal and home assistant lock and unlock are being received by board
-Log shows PWM signal being sent to servo
-When servo sticks toggling binary_sensor pin on/off usually, but not always fixes problem
-Board reboot fixes problem consistently

any suggestions welcome

Adding a short .5 second delay before each servo write seems to have fixed the problem:

lock:
  - platform: output
    # id is for firmware call reference
    id: rear_lock
    # name is for esphome reference
    name: "rear_lock"
    output: lock_servo
    # on_lock action to take when lock is locked component call: -lock.lock: rear_lock
    on_lock:
      - delay: .5s
      - servo.write:
          id: servo1
          level: -0.9
      - logger.log: "Door Locked!"
    # on_unlock action to take on component call: -lock.unlock: rear_lock
    on_unlock:
      - delay: .5s
      - servo.write:
          id: servo1
          level: 0.3
      - logger.log: "Door Unlocked!"

This is a very neat project. If the locking mechanism jams, can the servo break any of the 3D printed bits?

It’s possible, that might happen. The couple of breaks I’ve seen have been due to over tightening. I’ve been using PETG which would be more durable and temp resistant than PLA. The original 3d models provided on thingiverse through the blog post needed some scaling modifications and repositioning of holes to line up correctly, at least with the jimmy locks I was using. That said, the original models do work with scaling, they may break prematurely due to alignment issues however.

If you’ve got interest in the project I could throw up a remix of the modified models. Code seems to be working reliably on 2 smart locks for the last month or so

Edit: Thought I’d throw up a picture for size reference
Esp32 board is in the 3d printed box with a voltage stepdown converter. Door looks weird cause its a custom build for an RV conversion