ESPHome GPIO voltage + analogWrite?

Hi, I am trying to get a candlelight flickering effect on an LED connected to GPIO12 on my ESP8266 board but I am stuck.

My current code is this:

esphome:
  name: lilygo1
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "redacted"
  password: "redacted"
  
  manual_ip:
    static_ip: 192.168.3.150
    gateway: 192.168.3.1
    subnet: 255.255.255.0        

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "redacted"
    password: "redacted"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

switch:
  - platform: gpio
    name: "Pin GPIO12"
    pin: GPIO12    
    
binary_sensor:
  - platform: gpio
    name: "Pin GPIO14"
    pin: GPIO14

Currently if I switch on GPIO12 in home assistant, the LED connected between GPIO14 and Ground pin will light up. So I am hoping I am in the right path.

Now to achieve the candlelight effect, from googling, it would appear if I set this GPIO to have a PWM output of something like this:

analogWrite(14, random(120)+135);
delay(random(100));

How would I integrate the above code into the yaml file?

Thanks in advance

hi,

I think you should use Binary light instead https://esphome.io/components/light/binary.html
You have to specify an output component, which I have no experience with, but maybe you can find some more info somewhere.

light:
  - platform: binary
    name: "LED"
    output: output_component1
    pin: GPIO14

Possibly you can create an effect for the light with a lambda.
Hope this helps

Thanks DidierM. You gave me some ideas. The binary platform does not allow me to add the flicker component so I modified the code to reflect this:

output:
  - platform: esp8266_pwm
    pin: GPIO0
    frequency: 10 Hz
    id: pwm_output

switch:
  - platform: gpio
    name: "Pin GPIO12"
    pin: GPIO12    
    
light:
  - platform: monochromatic
    name: "LED"
    output: pwm_output
    effects:
      - flicker:

So when i switch on LED, I can see that it flicker 3 times before turning to full brightness, but sadly it does not flicker further.

Any ideas?

Hi @fliptoback,

well, now you can change the pwm fequency part to for example:

light:
- platform: ...
  on_turn_on:
     - output.esp8266_pwm.set_frequency:
         id: pwm_output
         frequency: !lambda |-
             return(rand() % 100 + 1);

this will return a random number between 1 and 100. It automatically does this on turn on of the light.
If you put it under your effects part you can create your own effect (you will see it in the pulldown menu in HA).

hope this works!

Thanks DidierM again. This time it won’t compile as it says on_turn_on is not a valid option for the platform monocromatic.

I am so close!!

Finally successful!

Here is the esphome yaml code that works for me. I have to change the board platform to nodemcuv2.

Back in home assistant all I need to do is to add the entities, select the flicker from the drop down menu and voila. Very pleased with the candlelight effect.

esphome:
name: candle
platform: ESP8266
board: nodemcuv2

wifi:
ssid: “redacted”
password: “redacted”

manual_ip:
static_ip: 192.168.3.150
gateway: 192.168.3.1
subnet: 255.255.255.0

#Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: “redacted”
password: “redacted”

captive_portal:

#Enable logging
logger:

#Enable Home Assistant API
api:

ota:

light:

  • platform: monochromatic
    name: “Candle_lily”
    id: candle_lily
    output: candle_pwm
    default_transition_length: 0s
    effects:
    • flicker:
      name: Flicker
      alpha: 90%
      intensity: 5%

output:

  • platform: esp8266_pwm
    id: candle_pwm
    pin: GPIO2
    frequency: 1000 Hz

great work!

Thanks! I am also very pleased! :slight_smile: