ESPHome - rgbw lights flickering

I recently purchased couple of Tuya RGBW LED controllers and flashed one of them with ESPHome as a test. That worked great. I put together config as per this page -

So my config looks as follows:

  name: rgb-light-office

  board: esp01_1m

# Enable logging

# Enable Home Assistant API
    key: !secret api_key

  password: !secret ota_pwd

  - ssid: !secret ssid
    password: !secret pwd
    static_ip: my ip address
    gateway: my_gateway

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Rgb-Light-Office"
    password: !secret another_pw


# Example configuration entry
  - platform: esp8266_pwm
    id: output_green
    pin: GPIO14
  - platform: esp8266_pwm
    id: output_red
    pin: GPIO5
  - platform: esp8266_pwm
    id: output_blue
    pin: GPIO12
  - platform: esp8266_pwm
    id: output_white
    pin: GPIO15

  - id: action_state
    type: int
    restore_value: no
    initial_value: '0'

  - platform: gpio
    pin:   GPIO13
    name: "RGBW Controller Button"
      - invert:
      - delayed_on_off: 20ms
        - lambda: id(action_state) = (id(action_state) + 1) % 4;
        - if:
              lambda: 'return id(action_state) == 0;'
              - light.turn_off:  rgbw_lightstrip1
        - if:
              lambda: 'return id(action_state) == 1;'
              - light.turn_on:
                  id: rgbw_lightstrip1
                  brightness: 60%
        - if:
              lambda: 'return id(action_state) == 2;'
              - light.turn_on:
                  id: rgbw_lightstrip1
                  brightness: 40%
        - if:
              lambda: 'return id(action_state) == 3;'
              - light.turn_on:
                  id: rgbw_lightstrip1
                  brightness: 15%
  - platform: rgbw
    name: "rgbw_strip_01"
    id: rgbw_lightstrip1
    red: output_red
    green: output_green
    blue: output_blue
    white: output_white

    # Ensure the light turns on by default if the physical switch is actuated.
    restore_mode: ALWAYS_OFF

At first that seemed to work fine - however when I started playing with the colours I noticed that when the colour brightness is set to 100% and white brightness is 100% white lights start to flicker.
If I take the white brightness down to 70-80% the flickering stops.
With the colour lights turned off and only white is active on 100% there is no flickering. It also works the other way round so white is off and rgb are 100% there is no flickering.

Has anyone came across similar problem? Any suggestions how to fix it?

BTW - when the controller was running on Tuya there was problems - no flickering

I could imagen two things which are different from the stock tuya firmware:

  • the output frequency (defaults in esphome is 1kHz I think)
    → Higher should give less flicker

  • the maximum output max_power (defaults in esphome is the maximum = 1)
    → Lower could give less flicker

Can’t figure out were I read it but someone once claimed that the stock tuya fw of a device didn’t output the maximum but was limited in software.

Other general tweaks for the software pwm of a esp82xx could include limiting the logger from defaults DEBUG to INFO only.

Ok, I’ve made adjustment to the max_power, changed brightness to 80% and set debug to info and the reduction in the flickering is significant.
Before I start playing with output frequency I was wondering how high/low can I go with that value?
Where can I find this info?

Not sure if that specific answer you are looking for is found in the esphome docs:

frequency (Optional, frequency): The frequency to run the PWM with. Lower frequencies have more visual artifacts, but can represent much more colors. Defaults to 1000 Hz.

but as you want to go higher and not lower I would suggest to try 10kHz for a start and see if there is flickering left.

Actually the ledc (esp32 pwm) page lists some more details regarding frequencies:

Recommended frequencies

To get the highest available frequency while still getting the same bit depth it is recommended to pick one of the following frequencies.

Higher bit depth means that the light has more steps available to change from one value to another. This is especially noticeable when the light is below 10% and takes a long transition, e.g. turning slowly off.

Frequency Bit depth Available steps for transitions
1220Hz 16 65536
2441Hz 15 32768
4882Hz 14 16384
9765Hz 13 8192
19531Hz 12 4096

The ESP8266 for instance has usually a frequency of 1000Hz with a resolution of 10 bits. This means that there are only 4 steps between each value.