Esp32 gpio pins not working

Good afternoon, I have a Yaml file that uploads correctly into my ESP WROOM 32U and partially works. The Yaml file is below.

As can be seen, this controls 5*5v relays via GPIO pins. The issue is that GPIO pins 21 and 22 do not control the Ralays as the other GPIO pins do. I must be doing something wrong, if anyone cpould assist I would be grateful.

esphome:
  name: "ac-control"
  friendly_name: ESPHome Web 13ebdc
  min_version: 2025.9.0
  name_add_mac_suffix: false

esp32:
  variant: esp32
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
- platform: esphome

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

switch:
  - platform: gpio
    pin: GPIO17
    name: "Lounge"
    id: relay1
    #name: "Lounge A/C"
    icon: "mdi:air-conditioning"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay1

  - platform: gpio
    pin: GPIO18
    name: "Master Bedroom"
    id: relay2
    #name: "Master Bedroom A/c"
    icon: "mdi:house"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay2

  - platform: gpio
    pin: GPIO19
    name: "Dinning Room"
    id: relay3
    #name: "Dining Room A/C"
    icon: "mdi:air"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay3

  - platform: gpio
    pin: GPIO021
    name: "Breakfast Room"
    id: relay4
    #name: "Breakfast Room A/C"
    icon: "mdi:air"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay4

  - platform: gpio
    pin: GPIO22
    name: "Utility Room"
    id: relay5
    #name: "Utility Room A/C"
    icon: "mdi:air"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay5

What kind of relays and how they are wired? Post a link.

just to rule out defective relays, did you try the working relays on GPIO21/22 and the non-working relays on GPIO17/18/19?

Hi thanks for replying, yes the realys work if connected to the other GPIO pins

I notice with your current code, the relays will be on by default, if you want them to be off by default, you must add the inverted: true option.
maybe that’s the issue, the relays are already on?

  - platform: gpio
    pin: GPIO21
    inverted: true
    name: "Breakfast Room"
    id: relay4
    #name: "Breakfast Room A/C"
    icon: "mdi:air"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay4
 

If you use 5V relays incorrectly with 3.3V Esp32, soon you have other pins that don’t work.

And they are defined GPIO21, not GPIO021.

1 Like

Try this:

  - platform: gpio
    pin: GPIO021
    inverted: true
    name: "Breakfast Room"
    id: relay4
    #name: "Breakfast Room A/C"
    icon: "mdi:air"
    on_turn_on:
      then:
        - delay: 500ms
        - switch.turn_off: relay4

Good catch, but it will still work fine otherwise. GPIO021 will work too.
But better to use the correct syntax/indentations indeed.

Thank you :slight_smile:

Yizhet 5V 8 Channel-Relay, DC 5V 230V Relay Shield Module Control Board with Optocoupler for Raspberry Pi Arduino PIC AVR MCU DSP ARM TTL Logi

From Amazon.

With gnd from ESP32 to gnd pin next to the control pins and 5v from the ESPP32 to the VCC pin next to the control pins. A seperate 5v DC power supply conected to GND (-5v) and vcc (+5v) connected to JD-VCC.

Sounds pretty bad.

Connect your 5v psu to jd-vcc and gnd , jumper REMOVED.

Connect the trigger side VCC to 3.3V pin of esp, and IN pins to esp gpios.