How to turn on LEDs driven by ESP32 GPIOs but dimmed?

I have the following code defining the LED:

output:
  - platform: ledc
    pin: GPIO4
    id: led
    frequency: 1000 Hz
    inverted: false

light:
  - platform: monochromatic
    name: "Blue LED"
    output: led
    id: blue_led
    restore_mode: ALWAYS_OFF
    on_turn_on:
      - output.set_level:
          id: led
          level: 0.25 
    on_turn_off:
      - output.set_level:
          id: led
          level: 0.0 

and from elsewhere I use a lambda and try to turn it on with:

id(blue_led).turn_on();

but nothing happens. It does not light up. If I call id(led).turn_on(); directly, then it turns on, but its full brightness.

How can I turn it on so that its like 25% brightness. I dont care about being able to control it from home assistant. I just want it to never be brighter than 25% . This is a blue LED and its eye piercing! :eye::dark_sunglasses:

Post all of the ESPHome yaml configuration.

Is the blue LED on GPIO4 and you always want it at 25%? Then use this:

output:
  - platform: ledc
    pin: GPIO4
    id: led
    frequency: 1000 Hz
    inverted: false
    duty_cycle: 0.25

Or if you need to use light component, remove all the on_turn_… automations and call the light:
id(blue_led).turn_on().set_brightness(0.25).perform();

1 Like

Ive tried that but get this compilation error:

 platform: ledc
  pin: GPIO4
  id: led_blue
  frequency: 1000 Hz
  inverted: False
  
  [duty_cycle] is an invalid option for [output.ledc]. Please check the indentation.
  duty_cycle: 0.25

This worked! Thanks!

You’re welcome.

1 Like