ESPHome ESP32 turn on output before I2C bus setup

I have an ESP32 in ESPHome with a TCS34725 RGB colour sensor attached to the I2C bus. Readings are sent to Home Assistant via MQTT. This works perfectly fine.
Deep sleep is configured with a sleep_duration of 15s and is activated on_boot after a delay of 15s. This also works perfectly fine. 15s is just a test value at this stage.

The TCS34725 3v3 is connected to the ESP32 3v3 pin, therefore stays on during deep sleep. I want to move it to an output pin so I can control power to it. This is where my problem lies.
I have declared an output on GPIO23, turned it on in on_boot and turned it off after the 15s delay and before the deep sleep.
This works well, except that the I2C bus is setup before GPIO23 is turned on, therefore the TCS34725 is not available yet and is marked as failed.

How can I turn on GPIO23 before the I2C bus is setup?

Config and logs below.

Config:

esphome:
  name: TCS34725
  on_boot:
    then:
      - output.turn_on: RGB_SENSOR
      - delay: 15s
      - output.turn_off: RGB_SENSOR
      - deep_sleep.enter: DEEP_SLEEP

esp32:
  board: esp32dev
  framework:
    type: arduino
i2c:
  sda: GPIO21
  scl: GPIO22
  scan: true
  id: bus_a

output:
  - platform: gpio
    pin: GPIO23
    id: RGB_SENSOR

sensor:
  - platform: tcs34725
    red_channel:
      name: "TCS34725 Red Channel"
    gain: 4x
    integration_time: 360ms
    glass_attenuation_factor: 1.0
    address: 0x29
    update_interval: 2s

mqtt:
  broker: <REDACTED>
  username: <REDACTED>
  password: <REDACTED>
  birth_message:
  will_message:

deep_sleep:
  id: deep_sleep1
  sleep_duration: 15s

# Enable logging
logger:

# Enable Home Assistant API
#api:

ota:
  password: <REDACTED>

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

Logs:

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
[I][logger:214]: Log initialized
[C][ota:458]: There have been 0 suspected unsuccessful boot attempts.
[D][esp32.preferences:114]: Saving preferences to flash...
[I][app:029]: Running through setup()...
[I][i2c.arduino:156]: Performing I2C bus recovery
[E][i2c.arduino:172]: Recovery failed: SCL is held LOW on the I2C bus
[E][esp32-hal-i2c.c:1434] i2cCheckLineState(): Bus Invalid State, TwoWire() Can't init sda=0, scl=0
[C][tcs34725:022]: Setting up TCS34725...
[E][component:112]: Component tcs34725.sensor was marked as failed.
[C][wifi:037]: Setting up WiFi...
<REDACTED>
[I][wifi:502]: WiFi Connected!
<REDACTED>
[C][mqtt:029]: Setting up MQTT...
<REDACTED>
[I][mqtt:214]: MQTT Connected!
[C][deep_sleep:017]: Setting up Deep Sleep...
[I][app:062]: setup() finished successfully!
[I][app:102]: ESPHome version 2021.12.3 compiled on Mar 17 2022, 11:42:39
[C][wifi:488]: WiFi:
<REDACTED>
[C][logger:233]: Logger:
[C][logger:234]:   Level: DEBUG
[C][logger:235]:   Log Baud Rate: 115200
[C][logger:236]:   Hardware UART: UART0
[C][i2c.arduino:032]: I2C Bus:
[C][i2c.arduino:033]:   SDA Pin: GPIO21
[C][i2c.arduino:034]:   SCL Pin: GPIO22
[C][i2c.arduino:035]:   Frequency: 50000 Hz
[C][i2c.arduino:041]:   Recovery: failed, SCL is held low on the bus
[I][i2c.arduino:048]: Scanning i2c bus for active devices...
[I][i2c.arduino:056]: Unknown error at address 0x08
[I][i2c.arduino:056]: Unknown error at address 0x09
[I][i2c.arduino:056]: Unknown error at address 0x0A
[I][i2c.arduino:056]: Unknown error at address 0x0B
[I][i2c.arduino:056]: Unknown error at address 0x0C
[I][i2c.arduino:056]: Unknown error at address 0x0D
[I][i2c.arduino:056]: Unknown error at address 0x0E
[I][i2c.arduino:056]: Unknown error at address 0x0F
[I][i2c.arduino:056]: Unknown error at address 0x10
[I][i2c.arduino:056]: Unknown error at address 0x11
[I][i2c.arduino:056]: Unknown error at address 0x12
[I][i2c.arduino:056]: Unknown error at address 0x13
[I][i2c.arduino:056]: Unknown error at address 0x14
[I][i2c.arduino:056]: Unknown error at address 0x15
[I][i2c.arduino:056]: Unknown error at address 0x16
[I][i2c.arduino:056]: Unknown error at address 0x17
[I][i2c.arduino:056]: Unknown error at address 0x18
[I][i2c.arduino:056]: Unknown error at address 0x19
[I][i2c.arduino:056]: Unknown error at address 0x1A
[I][i2c.arduino:056]: Unknown error at address 0x1B
[I][i2c.arduino:056]: Unknown error at address 0x1C
[I][i2c.arduino:056]: Unknown error at address 0x1D
[I][i2c.arduino:056]: Unknown error at address 0x1E
[I][i2c.arduino:056]: Unknown error at address 0x1F
[I][i2c.arduino:056]: Unknown error at address 0x20
[I][i2c.arduino:056]: Unknown error at address 0x21
[I][i2c.arduino:056]: Unknown error at address 0x22
[I][i2c.arduino:056]: Unknown error at address 0x23
[I][i2c.arduino:056]: Unknown error at address 0x24
[I][i2c.arduino:056]: Unknown error at address 0x25
[I][i2c.arduino:056]: Unknown error at address 0x26
[I][i2c.arduino:056]: Unknown error at address 0x27
[I][i2c.arduino:056]: Unknown error at address 0x28
[I][i2c.arduino:056]: Unknown error at address 0x29
[I][i2c.arduino:056]: Unknown error at address 0x2A
[I][i2c.arduino:056]: Unknown error at address 0x2B
[I][i2c.arduino:056]: Unknown error at address 0x2C
[I][i2c.arduino:056]: Unknown error at address 0x2D
[I][i2c.arduino:056]: Unknown error at address 0x2E
[I][i2c.arduino:056]: Unknown error at address 0x2F
[I][i2c.arduino:056]: Unknown error at address 0x30
[I][i2c.arduino:056]: Unknown error at address 0x31
[I][i2c.arduino:056]: Unknown error at address 0x32
[I][i2c.arduino:056]: Unknown error at address 0x33
[I][i2c.arduino:056]: Unknown error at address 0x34
[I][i2c.arduino:056]: Unknown error at address 0x35
[I][i2c.arduino:056]: Unknown error at address 0x36
[I][i2c.arduino:056]: Unknown error at address 0x37
[I][i2c.arduino:056]: Unknown error at address 0x38
[I][i2c.arduino:056]: Unknown error at address 0x39
[I][i2c.arduino:056]: Unknown error at address 0x3A
[I][i2c.arduino:056]: Unknown error at address 0x3B
[I][i2c.arduino:056]: Unknown error at address 0x3C
[I][i2c.arduino:056]: Unknown error at address 0x3D
[I][i2c.arduino:056]: Unknown error at address 0x3E
[I][i2c.arduino:056]: Unknown error at address 0x3F
[I][i2c.arduino:056]: Unknown error at address 0x40
[I][i2c.arduino:056]: Unknown error at address 0x41
[I][i2c.arduino:056]: Unknown error at address 0x42
[I][i2c.arduino:056]: Unknown error at address 0x43
[I][i2c.arduino:056]: Unknown error at address 0x44
[I][i2c.arduino:056]: Unknown error at address 0x45
[I][i2c.arduino:056]: Unknown error at address 0x46
[I][i2c.arduino:056]: Unknown error at address 0x47
[I][i2c.arduino:056]: Unknown error at address 0x48
[I][i2c.arduino:056]: Unknown error at address 0x49
[I][i2c.arduino:056]: Unknown error at address 0x4A
[I][i2c.arduino:056]: Unknown error at address 0x4B
[I][i2c.arduino:056]: Unknown error at address 0x4C
[I][i2c.arduino:056]: Unknown error at address 0x4D
[I][i2c.arduino:056]: Unknown error at address 0x4E
[I][i2c.arduino:056]: Unknown error at address 0x4F
[I][i2c.arduino:056]: Unknown error at address 0x50
[I][i2c.arduino:056]: Unknown error at address 0x51
[I][i2c.arduino:056]: Unknown error at address 0x52
[I][i2c.arduino:056]: Unknown error at address 0x53
[I][i2c.arduino:056]: Unknown error at address 0x54
[I][i2c.arduino:056]: Unknown error at address 0x55
[I][i2c.arduino:056]: Unknown error at address 0x56
[I][i2c.arduino:056]: Unknown error at address 0x57
[I][i2c.arduino:056]: Unknown error at address 0x58
[I][i2c.arduino:056]: Unknown error at address 0x59
[I][i2c.arduino:056]: Unknown error at address 0x5A
[I][i2c.arduino:056]: Unknown error at address 0x5B
[I][i2c.arduino:056]: Unknown error at address 0x5C
[I][i2c.arduino:056]: Unknown error at address 0x5D
[I][i2c.arduino:056]: Unknown error at address 0x5E
[I][i2c.arduino:056]: Unknown error at address 0x5F
[I][i2c.arduino:056]: Unknown error at address 0x60
[I][i2c.arduino:056]: Unknown error at address 0x61
[I][i2c.arduino:056]: Unknown error at address 0x62
[I][i2c.arduino:056]: Unknown error at address 0x63
[I][i2c.arduino:056]: Unknown error at address 0x64
[I][i2c.arduino:056]: Unknown error at address 0x65
[I][i2c.arduino:056]: Unknown error at address 0x66
[I][i2c.arduino:056]: Unknown error at address 0x67
[I][i2c.arduino:056]: Unknown error at address 0x68
[I][i2c.arduino:056]: Unknown error at address 0x69
[I][i2c.arduino:056]: Unknown error at address 0x6A
[I][i2c.arduino:056]: Unknown error at address 0x6B
[I][i2c.arduino:056]: Unknown error at address 0x6C
[I][i2c.arduino:056]: Unknown error at address 0x6D
[I][i2c.arduino:056]: Unknown error at address 0x6E
[I][i2c.arduino:056]: Unknown error at address 0x6F
[I][i2c.arduino:056]: Unknown error at address 0x70
[I][i2c.arduino:056]: Unknown error at address 0x71
[I][i2c.arduino:056]: Unknown error at address 0x72
[I][i2c.arduino:056]: Unknown error at address 0x73
[I][i2c.arduino:056]: Unknown error at address 0x74
[I][i2c.arduino:056]: Unknown error at address 0x75
[I][i2c.arduino:056]: Unknown error at address 0x76
[I][i2c.arduino:056]: Unknown error at address 0x77
[I][i2c.arduino:060]: Found no i2c devices!
[C][gpio.output:010]: GPIO Binary Output:
[C][gpio.output:011]:   Pin: GPIO23
[C][tcs34725:047]: TCS34725:
[C][tcs34725:048]:   Address: 0x29
[E][tcs34725:050]: Communication with TCS34725 failed!
[C][tcs34725:052]:   Update Interval: 2.0s
[C][tcs34725:055]:   Red Channel 'TCS34725 Red Channel'
[C][tcs34725:055]:     State Class: 'measurement'
[C][tcs34725:055]:     Unit of Measurement: '%'
[C][tcs34725:055]:     Accuracy Decimals: 1
[C][tcs34725:055]:     Icon: 'mdi:lightbulb'
[C][captive_portal:144]: Captive Portal:
[C][ota:082]: Over-The-Air Updates:
<REDACTED>
[C][deep_sleep:024]: Setting up Deep Sleep...
[C][deep_sleep:027]:   Sleep Duration: 15000 ms
[I][deep_sleep:072]: Beginning Deep Sleep
[D][esp32.preferences:114]: Saving preferences to flash...
1 Like

I suspect the answer will lie in the on_boot priority.

Thanks for the pointer. I hadn’t seen the on_boot priority option.

If I have understood the documentation correctly, the output is currently turning on at the default priority (600), after hardware initialization (800) but before WiFi (250).

I have therefore set an on_boot priority of 900 in an attempt to get the output turned on before the I2C bus is initialized. However, this causes the output to never turn on. Same thing if I set the priority to 800. 700 works OK but has the same results as the initial problem, as expected.

esphome:
  name: TCS34725
  on_boot:
    priority: 900
    then:
      - output.turn_on: RGB_SENSOR
      - delay: 15s
      - output.turn_off: RGB_SENSOR
      - deep_sleep.enter: DEEP_SLEEP

Any further ideas?

As mentioned, the output component does not work with a priority of 800 or higher. However, I have had success with a lambda instead. I also had to increase the priority to 1000.

esphome:
  name: TCS34725
  on_boot:
    priority: 1000
    then:
      - lambda: |-
          pinMode(23, OUTPUT);
          digitalWrite(23, HIGH);
          delay(100);
      - delay: 15s
      - deep_sleep.enter: DEEP_SLEEP

@nickrout thanks for pointing me in the right direction.

3 Likes