ESPhome switch not working with momentary switch

Hi,

I have ESPhome installed in D1mini board connected to simple 4 channel relay board and binary sensor switches to control relays, all connected to D1mini GPIO pins.

No Issues with relay board, but one binary sensor switch acting weird. I’m using momentary switches with 2 pins, one connected to GPIO pin allocated to binary sensor pin and one to ground. When I press the button, it’s working for only once, later not working no matter how many times I press, but working when I touch the GPIO pin wire with my finger/screw driver/any object, but not ground pin.

I’m searching for this phenomenon for long time, without any answer. Is it because any polarity? Do I need to use any kind of resistor or anything? Don’t know much about electronics.

Most likely you use a gpio pin with a limitation.

See here and choose wisely :wink:

Thanks for this info. Next time, I’ll refer to this while assigning GPIO pins. I mostly use MCP23017 to extend GPIOs for more than 4 switches which has no issues. Previously I used almost all pins on D1mini (except D8 as it needs pull down resistor which I don’t have) without any issue. Now I have 3 prototype boards only have issues with D0 pin (for input). I better leave that pin alone.

I don’t understand one thing. Any idea why it responds with other objects when not responding with ground? I’m just curious.

It really depends on the pin. Most have (only) weak pull up’s, at least one a weak pull down.

Other good read for this gpio mess is this link:

Under " Best Pins to Use – ESP8266" you see a nice summary and listed which pins a good for input/output or both (beside other restrictions).

1 Like

Putting aside the issue about which pin to use, the “activated by finger” evidence suggests you don’t have a pull-up resistor on that gpio pin. You can configure esphome to use the internal pull-up or wire in an external pull-up.

I’d suggest doing this for all of your inputs - even if they aren’t currently showing problems. If those pins are floating when button not pressed, you could see errors pop up as environment changes.

1 Like

exact answer I was looking for. Tested with 2 other prototype boards, it’s always pin D0. I’ll better try a resistor.

D0 on Wemos boards is gpio16. On Esp8266, that pin only has a pull-down resistor so you will need to use an external. Or rewire to a different pin.

can’t assign a different pin as all are occupied (except D8 as it causing boot failures and need to use a pull down resistor).
Also tried input_pulldown_16, not worked
Just to make sure, I’m using below diagram to use a 5K resistor between 5V and pin.
image
May I know is it the right resistance value?

Wiring looks good for a pull-up. I’d bump up to 10k on resistor value - that’s pretty normal for a pull-up / pull-down.

Sure, I have spare 10K resistors to use right now.
Can I use D8 pin with a pull down resistor (may be between pin and ground) with same resistance value without causing boot failure?

d8 is actually gpio15. This pin needs to be in a certain state during boot, but after that, should work. IE, wire correctly so pin is in correct state when button not pressed - and then be sure to not press button during boot.

1 Like

Yep, like it says for GPIO15/D8 on the random nerd tutorials page

Boot fails if pulled HIGH

@sathish can you share your confíguration for using the MCP23017 with switches? Im currently trying to use it with esphome and some momentary switches but it just doesnt register any input.

Works fine for me. Here’s my Livingroom node config,

esphome:
  name: living_room
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "XXXX"
  password: "XXXX"
  fast_connect: true
  # Optional manual IP
  manual_ip:
    static_ip: 192.168.X.XX
    gateway: XXXX
    subnet: XXXX
  ap:
    ssid: "Living Room Fallback Hotspot"
    password: "XXXX"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

web_server:

# I2C INTERFACE
i2c:
  sda: D2
  scl: D1
  scan: False
  frequency: 400kHz

# MCP23017 Component
mcp23017:
  - id: 'mcp23017_hub'
    address: 0x20

# Switches
binary_sensor:
  - platform: gpio
    name: "Switch 1"
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 0
      number: 0
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      - switch.toggle: relay1
    internal: True

  - platform: gpio
    name: "Switch 2"  
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 1
      number: 1
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - switch.toggle: relay2
    internal: True

  - platform: gpio
    name: "Switch 3"
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 2
      number: 2
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - switch.toggle: relay3
    internal: True

  - platform: gpio
    name: "Switch 4"
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 3
      number: 3
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - switch.toggle: relay4
    internal: True

  - platform: gpio
    name: "Switch 5"
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 4
      number: 4
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - switch.toggle: relay5
    internal: True

  - platform: gpio
    name: "Switch 6"
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 5
      number: 5
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - switch.toggle: relay6
    internal: True

  - platform: gpio
    name: "Switch 7"
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 6
      number: 6
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - switch.toggle: relay7
    internal: True

  - platform: gpio
    name: "Switch 8"
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 7
      number: 7
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - switch.toggle: relay8
    internal: True

  - platform: gpio
    pin:
      number: D5
      mode: INPUT_PULLUP
      inverted: True
    id: lb_01_b1
    on_release:
      - homeassistant.service:
          service: light.toggle
          data: {entity_id: light.livingroom_bulb}
    on_click:
      - min_length: 500ms
        max_length: 2000ms
        then:
          - homeassistant.service:
              service: scene.turn_on
              data:
                entity_id: scene.living_room_bulb_brightness_full
    internal: True



# Relays
switch:
  - platform: gpio
    name: "Down Lights"
    icon: 'mdi:ceiling-light'
    id: relay1
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 8
      number: 8
      # One of INPUT, INPUT_PULLUP or OUTPUT
      mode: OUTPUT
      inverted: True

  - platform: gpio
    name: "Fan"
    icon: 'mdi:fan'
    id: relay2
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 9
      number: 9
      # One of INPUT, INPUT_PULLUP or OUTPUT
      mode: OUTPUT
      inverted: True

  - platform: gpio
    name: "Plug"
    icon: 'mdi:power-plug'
    id: relay3
    restore_mode: RESTORE_DEFAULT_ON
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 10
      number: 10
      # One of INPUT, INPUT_PULLUP or OUTPUT
      mode: OUTPUT
      inverted: True

  - platform: gpio
    name: "Light"
    icon: 'mdi:wall-sconce-flat'
    id: relay4
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 11
      number: 11
      # One of INPUT, INPUT_PULLUP or OUTPUT
      mode: OUTPUT
      inverted: True

  - platform: gpio
    name: "RGB Light"
    icon: 'mdi:globe-light'
    id: relay5
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 12
      number: 12
      # One of INPUT, INPUT_PULLUP or OUTPUT
      mode: OUTPUT
      inverted: True

  - platform: gpio
    name: "Front Door Light"
    icon: 'mdi:outdoor-lamp'
    id: relay6
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 13
      number: 13
      # One of INPUT, INPUT_PULLUP or OUTPUT
      mode: OUTPUT
      inverted: True

  - platform: gpio
    name: "Roof Lights"
    icon: 'mdi:led-strip-variant'
    id: relay7
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 14
      number: 14
      # One of INPUT, INPUT_PULLUP or OUTPUT
      mode: OUTPUT
      inverted: True

  - platform: gpio
    name: "RGB Down Lights"
    icon: 'mdi:ceiling-light'
    id: relay8
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 15
      number: 15
      # One of INPUT, INPUT_PULLUP or OUTPUT
      mode: OUTPUT
      inverted: True

  - platform: restart
    name: "Livingroom Panel REBOOT"

# Status Sensors
sensor:
  - platform: wifi_signal
    name: "Livingroom Panel WiFi signal"
    update_interval: 60s

  - platform: uptime
    name: "Livingroom Panel uptime"

text_sensor:
  - platform: version
    name: "Livingroom Panel ESPHome version"

Thanks for this. Was driving me nuts why it wasn’t working. I used D1 with the INPUT_PULLUP mode. Finally works like I wanted.

thank for the code … while using I’m facing one problem .

i2c:
  sda: GPIO22
  scl: GPIO21
  scan: False
  frequency: 400kHz

mcp23017:
  - id: 'mcp23017_hub1'
    address: 0x20

binary_sensor:
  - platform: gpio
    name: "Pin 8"
    pin:
      mcp23017: mcp23017_hub
      number: 8
      mode: INPUT_PULLUP
      inverted: True

log

[18:00:29][C][api:095]: API Server:
[18:00:29][C][api:096]:   Address: 192.168.10.105:6053
[18:00:59][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:00:59][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:20][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:20][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:21][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:21][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:22][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:22][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:22][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:22][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:23][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:23][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:28][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:28][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:39][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:39][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:43][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:43][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:47][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:47][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:53][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:53][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:54][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:54][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:55][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:55][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:56][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:56][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:01:59][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:01:59][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:02:08][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:02:08][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:02:09][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:02:09][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON
[18:02:10][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state OFF
[18:02:10][D][binary_sensor:036]: 'MCP23008 Pin #1': Sending state ON

without pushing any button it is automatically on and off … what to do ?
for testing in the same device instead of mcp23017 pin 8, I used ESP gpio33 with the same wiring. it is not having any problem works perfectly.

mcp23017 pin is connected to GPIO21 and 22 VCC to 5v and GND to GND and A0 A1 A2 are connected to Ground (for 0x20)

what to do … did I make any mistake in mcp23017

can you pleas share me the wiring of mcp23017 to button and esp

When my esp is not configured in home assistant the physical switch works fine and I receive output from the log. But as soon as I connect the esp to home assistant I can turn the switch on and off from the dashboard but the physical switch no longer works. Any ideas what I need to fix?

I know you are new, but honestly you won’t get any help without your yaml, your wiring diagram and your logs.