Help: GPIO outputs being toggled at boot on esp32

The problem

Setup: ESP32 NodeMCU, with ESPHome 2022.2.4 installed. GPIO 33 connected to Wemos D1 relay shield. Active High, attached to NO. This is to open a door on HA command.

The same setup on a ESP8266 worked perfectly. I had to expand this project to more sensors, including bluetooth, so opted for ESP32.

On ESP32, at boot or reset, the GPIO pin is going high, then low. This activates the relay, so opens the door (this becomes a security issue)

CODE:

# Relay pin
switch:
  - platform: output
    id: doorrelay
    #pin: GPIO32
    output: doorPIN
    name: "Door relay"
    restore_mode: ALWAYS_OFF
    icon: "mdi:gate"
    on_turn_on:
    #- output.turn_on: doorPIN
    - output.turn_on: doorrelayLED
    - delay: 500ms
    #- output.turn_off: doorPIN
    - switch.turn_off: doorrelay
    - output.turn_off: doorrelayLED
    - homeassistant.service:
        service: notify.mobile_app_stefe_iphone
        data:
          title: "Button was pressed"
          message: "opening door"

# Relay pin
output:
  - platform: gpio
    pin: GPIO02
    id: doorrelayLED
    inverted: true
    
  - platform: gpio
    pin: GPIO33
    id: doorPIN

I have tried the following, with no success

  1. Initial code used switch platform GPIO, set on pin 32. I tried changing this to an output switch.
  2. Tried GPIO 33 as well
  3. Placed a pulldown resistor 4k7 between relay pin and ground.
  4. changed ALWAYS_OFF to restore_DEFAULT_OFF
  5. tried inverting pins to get an inverse logic
  6. Tried a button script rather than switch

The only thing that worked was setting a different GPIO in the code, but keeping the physical connection. That is, soldering input to GPIO 33, but using GPIO 32 in code. At that point, nothing happens on boot, but of course, this is useless, as I cannot activate the relay any more.

It seems that at boot, GPIO outputs that are set up in ESPhome toggle on & off.

Help welcome, as now I have to take everything down again.

Something more so far.

It would seem that the SWITCH or BUTTON component are activated once at boot. Is there a way to stop this?

  • I have commented out the line: output.turn_off: doorrelay
    This causes the relay to activate at boot, and remain on. This would mean to me that rather than an issue with the GPIO startup, it is the button / switch component that are triggered for no reason at boot.

I have made a temporary solution. I have added a lambda to the button section, to check if the ESP32 has been on for more than a minute.

If not, it will not set the GPIO on.

sensor:
  - platform: uptime
    name: Uptime Sensor
    id: time_since_boot
        
# Relay pin
button:
  - platform: template
    id: doorbutton
    name: "Door button"
    icon: "mdi:gate"
    on_press:
      then:
        - logger.log: Button Pressed
        - lambda: |-
            if (id(time_since_boot).raw_state > 60) {
              id(doorPIN).turn_on();
            }
        #- output.turn_on: doorPIN
        - output.turn_on: doorrelayLED
        - delay: 500ms
        - output.turn_off: doorPIN
        - output.turn_off: doorrelayLED
        - homeassistant.service:
            service: notify.mobile_app_stefe_iphone
            data:
              title: "Button was pressed"
              message: "opening door..."
        - homeassistant.event:
            event: esphome.button_pressed
            data:
              message: Door was opened remotely


# Relay pin
output:
  - platform: gpio
    pin: GPIO02
    id: doorrelayLED
    inverted: true
    
  - platform: gpio
    pin: GPIO33 #33 #27
    id:  doorPIN

Still I hope this issue is fixed…

1 Like

this means whenever restart ESP, all IO pins are going to high after booting, add pull-down resistor
I.e add 10k resistor on IO pin to ground, I think it will be solved.

For me the solution was to add: “early_pin_init: false” to the board config.
The behavior is documented here: ESP8266 Platform — ESPHome

I have the same issue when using GPIO13 as a transistor base control.
I am studying it now. This is a good ref: ESP32 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials

How did you solve this for pin 13 because i am using the same pin and it is also pulled high on boot.