Help with deep sleep wake from keypad

I’ve rebuilt an old keypad from an old alarm system basically cutting the traces from the keypad to the old circuitboard and wired the button matrix to an ESP32 D1 mini. The keypad used to run off a 3V button batter but I have now wired a 18650 Lion cell instead. Hardwarewise this can be seen as just the esp with a 4x4 matrix without diods.

As this is running from battery the plan was to put the esp into deep sleep once the “unlock” key has been pressed (or after a time idling). The wake-up condition I’d like to be any buttonpress, but I could do with just one key too.

Where I’m a bit lost is how this should be done, if it can be in esphome. Additional wiring is okay but I don’t have access to any buttons or input besides the 4x4 matrix.
My attempts so far have been to set the input pins of the matrix as wake-up pins as well using the flag for multiple functions on one pin. I’ve also tried attaching a separate IO for the IRQ to one of the inputs.
I’ve played around with all the settings for IRQs and even tried to set the outputs of the matrix to LOW in the shutdown routine, but all results I’m getting are either board not waking up from sleep or waking up immediately. I cannot wake it by pressing any buttons.

I guess that if I’d had been trying other software I’d repurpose the pins before sleep to avoid collisions but this does not seem possible with esphome. What advice would you have for me? What can I try.

This code is just the base. All my attempts are not here :smiley:

esphome:
  name: keypad
  friendly_name: Keypad
  
  on_boot:
    then:
      - light.turn_on: led
  on_shutdown:
    priority: 700
    then:
      - output.turn_off:
          id: gpio_26

esp32:
  board: wemos_d1_mini32
  framework:
    type: arduino
    

# Enable logging
logger:

# Enable Home Assistant API
api: # Will move to mqtt OC!
  encryption:
    key: "___"

ota:
  password: "___"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Keypad Fallback Hotspot"
    password: "___"
  
deep_sleep:
  wakeup_pin_mode: KEEP_AWAKE
#   #Wakeup pins can be any of
#   # GPIO0, GPIO2, GPIO4, GPIO12, GPIO13, GPIO14, GPIO15, GPIO25, GPIO26, GPIO27, GPIO32, GPIO39
  id: deep_sleep_1
  sleep_duration: 30s
  # IOs that froze the keypad 04, 13
  wakeup_pin: # Bridged to IO 12
     number: 14
    #  allow_other_uses: true
     inverted: true

  # Earlier attempt 
  # esp32_ext1_wakeup: 
  #   pins:
  #       - number: 27
  #         allow_other_uses: true
  #       - number: 25
  #         allow_other_uses: true
  #       - number: 32
  #         allow_other_uses: true
  #       - number: 12
  #         allow_other_uses: true
  #   mode: ANY_HIGH #ALL_LOW #ANY_HIGH
  #   # ANY_HIGH goes out of sleep immediatly
  #   # All_LOW goes out of sleep immediatly

matrix_keypad:
  id: mykeypad
  rows:
    - pin:
        number: 26
        allow_other_uses: true
    - pin: 18 #19
    - pin: 19 #18
    - pin: 23 #5
  columns: # Inputs with PullUp enabled
    # Earlier used 22, 21, 17, 16
    - pin: 27
    - pin: 25
    - pin: 32
    - pin: 12
    # - pin: 
    #    number: 27
    #   #  allow_other_uses: true
    # - pin: 
    #    number: 25
    #   #  allow_other_uses: true
    # - pin: 
    #    number: 32
    #   #  allow_other_uses: true
    # - pin: 
    #    number: 12
    #   #  allow_other_uses: true
  keys: "123T456U789C*0#L"
  has_diodes: false

binary_sensor:
  - platform: matrix_keypad
    keypad_id: mykeypad
    id: keyLock
    row: 3
    col: 3
    on_press:
      then:
        - light.turn_off:
            id: led
        - deep_sleep.enter:
            sleep_duration: 30s
            id: deep_sleep_1

output:
  - platform: gpio
    pin: GPIO2
    id: led_output

  - platform: gpio
    pin:
      number: 26
      allow_other_uses: true
    id: gpio_26

light:
  - platform: binary
    name: LED
    output: led_output
    id: led

Edit: The IRQ seems to work on the same pins as the matrix. If I short the pin against ground the board wakes up. So if I could solve the output pin being low that may be it. Any ideas? :slight_smile: