Switch defined in ESPHome disconnect when turning off

Hi,

I have some switches defined in ESPHome using AZDelivery ESP32 device that work fine when switching them on, when they switch off, ESPHome get disconnected.

These switches are used in an HA Dashboard

The switches are defined like this:

switch:

  - id: relay_01
    name: "Relay 01"
    platform: gpio
    pin:
      number: GPIO21
      inverted: true
      mode:
        input: false
        output: true
        pullup: true

Looking at the ESPHome log, the result is this:

[17:54:51][D][api:102]: Accepted 192.168.69.10
[17:54:51][W][component:204]: Component api took a long time for an operation (0.05 s).
[17:54:51][W][component:205]: Components should block for at most 20-30ms.
[17:54:52][D][api.connection:1031]: Home Assistant 2023.8.1 (192.168.69.10): Connected successfully
[17:55:24][D][switch:012]: ‘Relay 01’ Turning ON.
[17:55:24][D][switch:055]: ‘Relay 01’: Sending state ON
[17:55:27][D][switch:016]: ‘Relay 01’ Turning OFF.
[17:55:27][D][switch:055]: ‘Relay 01’: Sending state OFF
WARNING 192.168.69.30: Connection error occurred: Ping response not received after 90.0 seconds
INFO Processing unexpected disconnect from ESPHome API for 192.168.69.30
WARNING Disconnected from API
WARNING Can’t connect to ESPHome API for 192.168.69.30: Error connecting to (‘192.168.69.30’, 6053): [Errno 113] Connect call failed (‘192.168.69.30’, 6053) (SocketAPIError)
INFO Trying to connect to 192.168.69.30 in the background

Looking at the HA system log, I see these entries:

Logger: aioesphomeapi.reconnect_logic
Source: runner.py:179
First occurred: 17:57:25 (1 occurrences)
Last logged: 17:57:25

Can’t connect to ESPHome API for garden-controller @ 192.168.69.30: Error connecting to (‘192.168.69.30’, 6053): [Errno 113] Connect call failed (‘192.168.69.30’, 6053) (SocketAPIError)

Logger: aioesphomeapi.connection
Source: runner.py:179
First occurred: 16:52:05 (4 occurrences)
Last logged: 17:57:22

garden-controller @ 192.168.69.30: Connection error occurred: [Errno 104] Connection reset by peer
garden-controller @ 192.168.69.30: Connection error occurred: Ping response not received after 90.0 seconds

Logger: frontend.js.latest.202308020
Source: components/system_log/init.py:270
First occurred: 16:51:35 (1 occurrences)
Last logged: 16:51:35

:0:0 Script error.

I’m struggling finding what is the cause of this behavior, can somebody help me?

thank you
Antonio

Probably not related to your problem, but why do you have a pull-up resistor being used on an output pin?

Yeah, I removed the pullup resistor but the result is the same

So when you turn the relay off it pulls the pin high?

What voltage are the relays?

Could it be a brownout?

The ESP32 is connected to an 8 relays board, I did some tests and it seems could be related to a power suply issue.
I’m going to change the power suplly and how the ESP32 is powered up and how the circuit served by relays is connected.
I’ll keep updated.