I want to connect a touchbutton to esp01s GPIO2, do I need a pull up/down?

I’m rebuilding our Tefal Raclette to have it controlled with an ESP01s dual relay board.
All the logic is working, I can turn it on from inside HomeAssistant, but I can’t figure out if I need a pull up/down resistor on the GPIO2, or if I can do that as an argument in esphome?

I’m using this board:

So one relay is for 230v, the other is for the led in the pushbutton.
As the GPIO2 is not connected to the pin array on the board (as far as I can tell), I’m just going to solder directly to the pin on the board.
But how, just short GPIO2 to gnd, or do I need to add a resistor as a pull up/down for it? And what would the corresponding code look like?

It depends how you use it.

If you are going to short the pin to gnd then when it’s not shorted and just “floating” you want it pulled the opposite way - up, so it changes when you short it.

If instead you plan to short the pin to 3.3v, when the pin is not shorted and floating you want it pulled the opposite way - down.

No need for a physical resistor. Software enabling the internal pull up or down resistors is all that is required.

If you have an active device that outputs 0v or 3.3v the pin is never floating and you don’t need a pull resistor.

1 Like

Ok, so there is an internal I can use, so something like this:

binary_sensor:
  - platform: gpio
    id: button
    internal: true
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
    on_press:
      - switch.toggle: relay

And then short it to gnd?

Yep.

1 Like

Ok, so it partly works :-/
I just found out that I needed to add ‘optimistic’ to the first relay, not sure about the ‘light’?

The code looks like this (I’ve commented the timer out while debugging):

substitutions:
  device_name: raclette

esphome:
  name: ${device_name}
  platform: ESP8266
  board: esp01_1m
#  on_boot:
#    then:
#      - switch.turn_on: relay
#      - light.turn_on: buttonlight

wifi:
  ssid: !secret wifissid
  password: !secret wifipw
  reboot_timeout: 60min
  manual_ip:
    static_ip: 10.11.13.99
    gateway: 10.11.13.1
    subnet: 255.255.255.0
  fast_connect: true
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name} Hotspot"
    password: !secret appw

# Enable Home Assistant API
api:

ota:
  password: !secret otapw

time:
  - platform: homeassistant
    id: homeassistant_time

# Enable logging
logger:
  baud_rate: 0 #need this to free up UART pins

uart:
  baud_rate: 115200 # speed to STC15L101EW
  tx_pin: GPIO1
  rx_pin: GPIO3
  
web_server:
  port: 80
  auth:
    username: !secret webuser
    password: !secret webpw

# Text Sensor with general information
text_sensor:
  - platform: version
    name: '${device_name} ESPHome Version'
  - platform: wifi_info
    ip_address:
      name: '${device_name} ip'
    ssid:
      name: '${device_name} ssid'

binary_sensor:
  - platform: gpio
    id: button
    internal: true
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
#      inverted: true
    on_press:
      - switch.toggle: relay
    filters:
      - delayed_off: 10ms
sensor:
  - platform: wifi_signal
    name: '${device_name} wifi signal'
    update_interval: 60s
    accuracy_decimals: 0
  - platform: uptime
    name: '${device_name} uptime'
    unit_of_measurement: days
    update_interval: 300s
    accuracy_decimals: 1
    filters:
      - multiply: 0.000011574


switch:
  - platform: template
    name: 'Raclette'
    id: relay
    turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
      - light.turn_on: buttonlight
    turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
      - light.turn_off: buttonlight
    optimistic: true

output:
  - platform: template
    type: binary
    id: light_relay
    write_action:
      - if:
          condition:
            light.is_on: buttonlight
          then:
            uart.write: [0xA0, 0x02, 0x01, 0xA3]
          else:
            uart.write: [0xA0, 0x02, 0x00, 0xA2]

light:
  - platform: binary
    id: buttonlight
    name: Powerindicator
    output: light_relay

So the weird part is, if I toggle this throught HA it works, it turns on the LED when I turn on the relay and vice versa.
If I do it with the button, it turns the LED on and off, but not the switch as far as I can se?
I’m very confused, off to bed for now, I’m getting too tired for this :smiley:

What did I do wrong in the code?

Ahaa It’s a question of timing, I think I was ‘overloading’ the UART with commands, adding a delay fixed it.
I also reordered it a bit:

substitutions:
  device_name: raclette

esphome:
  name: ${device_name}
  platform: ESP8266
  board: esp01_1m
#  on_boot:
#    then:
#      - switch.turn_on: relay
#      - light.turn_on: buttonlight

wifi:
  ssid: !secret wifissid
  password: !secret wifipw
  reboot_timeout: 60min
  manual_ip:
    static_ip: 10.11.13.99
    gateway: 10.11.13.1
    subnet: 255.255.255.0
  fast_connect: true
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name} Hotspot"
    password: !secret appw

# Enable Home Assistant API
api:

ota:
  password: !secret otapw

time:
  - platform: homeassistant
    id: homeassistant_time

# Enable logging
logger:
  baud_rate: 0 #need this to free up UART pins

uart:
  baud_rate: 115200 # speed to STC15L101EW
  tx_pin: GPIO1
  rx_pin: GPIO3
  
web_server:
  port: 80
  auth:
    username: !secret webuser
    password: !secret webpw

# Timer for auto turnoff
script:
  id: countdown
  then:
  - delay: 120min  # timer length
  - switch.turn_off: relay

# Text Sensor with general information
text_sensor:
  - platform: version
    name: '${device_name} ESPHome Version'
  - platform: wifi_info
    ip_address:
      name: '${device_name} ip'
    ssid:
      name: '${device_name} ssid'

binary_sensor:
  - platform: gpio
    id: button
    internal: true
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
#      inverted: true
    on_press:
      - switch.toggle: relay
    filters:
      - delayed_off: 10ms
sensor:
  - platform: wifi_signal
    name: '${device_name} wifi signal'
    update_interval: 60s
    accuracy_decimals: 0
  - platform: uptime
    name: '${device_name} uptime'
    unit_of_measurement: days
    update_interval: 300s
    accuracy_decimals: 1
    filters:
      - multiply: 0.000011574


switch:
  - platform: template
    name: 'Raclette'
    id: relay
    turn_on_action:
      - light.turn_on: buttonlight
      - script.execute: countdown
      - delay: 1s
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
    turn_off_action:
      - script.stop: countdown
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
      - delay: 1s
      - light.turn_off: buttonlight
    optimistic: true

output:
  - platform: template
    type: binary
    id: light_relay
    write_action:
      - if:
          condition:
            light.is_on: buttonlight
          then:
            uart.write: [0xA0, 0x02, 0x01, 0xA3]
          else:
            uart.write: [0xA0, 0x02, 0x00, 0xA2]

light:
  - platform: binary
    id: buttonlight
    name: Powerindicator
    output: light_relay

So what it does. When the button is pushed, it will start the relay turn on procedure, the timer is set to 120mins, the relay will turn on the light in the button, and then the relay for power. When it is pressed again it stops the script, turns off the power and then the LED.

It of course integrates to HA, and the LED and relay can be controlled from that as well.

Now I’m just going to find out if I can make the delay shorter than 1s