Driving an LED directly on a GPIO?

I have a button with an LED in it.
Like this:

I want to use the LED for communication about the state.
So in normal mode it is on, and then when a time out is closing, it starts flashing.
I’ve gotten the code built.
But the question is, can I drive the LED in the button directly from the GPIO? Do I need to put a resistor on it, or can I use a built-in pull-down or something?
I’ve previously had a relay on it, but I would like to save some space from that.

If you have a button with a 5V LED, it is possible to connect it directly to the GPIO. The only problem I see is that if the LED is powered by 5V, it already has a limiting resistor inside and the GPIO output is only 3.3V. The LED will glow dimly or not at all. I would probably use a 3.3V/5V level converter.
Can you share the LED flashing code?

I don’t really understand the description of button 3 - 9 (5V)

1 Like

My code so far is

substitutions:
  device_name: raclette-w-temp
  countdown_time: 116min
  slow_flashing_time: 3min
  fast_flashing_time: 3min
  slow_flashing_delay: 3s
  fast_flashing_delay: 1s
  device_description: "10.11.13.100 Smart Tefal Raclette with temperature sensor"
  
esphome:
  name: ${device_name}
  comment: '${device_description}'
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  reboot_timeout: 60min
  manual_ip:
    static_ip: 10.11.13.100
    gateway: 10.11.13.1
    subnet: 255.255.255.0
  ap:
    ssid: "${device_name} Hotspot"
    password: !secret appw

# Enable Home Assistant API
api:
  password: !secret apipw

ota:
  password: !secret otapw

time:
  - platform: homeassistant
    id: homeassistant_time

logger:
  baud_rate: 0 #need this to free up UART pins

web_server:
  port: 80
  auth:
    username: !secret webuser
    password: !secret webpw

# Timer for auto turnoff
script:
  - id: countdown
    then:
    - delay: ${countdown_time}  # timer length
    - script.execute: slow_flashing_timer
    - while:
        condition:
          script.is_running: slow_flashing_timer
        then:
          - light.turn_off: buttonlight
          - delay: ${slow_flashing_delay}
          - light.turn_on: buttonlight
          - delay: ${slow_flashing_delay}
    - script.execute: fast_flashing_timer
    - while:
        condition:
          script.is_running: fast_flashing_timer
        then:
          - light.turn_off: buttonlight
          - delay: ${fast_flashing_delay}
          - light.turn_on: buttonlight
          - delay: ${fast_flashing_delay}
    - switch.turn_off: relay
    - light.turn_off: buttonlight
  - id: slow_flashing_timer
    then:
    - delay: "${slow_flashing_time}"
  - id: fast_flashing_timer
    then:
    - delay: "${slow_flashing_time}"

spi:
  miso_pin: D6
  clk_pin: D5

# Text Sensor with general information
text_sensor:
  - platform: version
    name: '${device_name} ESPHome Version'
  - platform: wifi_info
    ip_address:
      name: '${device_name} ip'
    ssid:
      name: '${device_name} ssid'

sensor:
  - platform: wifi_signal
    name: '${device_name} wifi signal'
    update_interval: 60s
    accuracy_decimals: 0
  - platform: uptime
    name: '${device_name} uptime'
    unit_of_measurement: days
    update_interval: 300s
    accuracy_decimals: 1
    filters:
      - multiply: 0.000011574
  - platform: max6675
    name: '${device_name} Temperature'
    cs_pin: D8

binary_sensor:
  - platform: gpio
    id: button
    internal: true
    pin:
      number: D4
      mode: INPUT_PULLUP
      inverted: true
    on_press:
      - if:
          condition:
            switch.is_on: relay
          then:
            - if:
                condition:
                  or:
                    - script.is_running: slow_flashing_timer
                    - script.is_running: fast_flashing_timer
                then:
                  - logger.log: "Countdoun resat"
                  - script.stop: countdown
                  - script.stop: slow_flashing_timer
                  - script.stop: fast_flashing_timer
                  - light.turn_on: buttonlight
                  - script.execute: countdown
                else:
                  - switch.turn_off: relay
          else:
            - switch.turn_on: relay
    filters:
      - delayed_off: 30ms

output:
  - platform: gpio
    pin: D1
    id: light_relay

switch:
  - platform: gpio
    pin: D2
    id: relay
    name: '${device_name} relay'
    on_turn_on:
      - light.turn_on: buttonlight
      - script.execute: countdown
    on_turn_off:
      - script.stop: countdown
      - light.turn_off: buttonlight

light:
  - platform: binary
    id: buttonlight
    name: '${device_name} powerindicator'
    output: light_relay

It is only semitested, in the sence that it was tested a long time ago, but a lot of changes has been made since

@pepe59 could I use a MOSFET? I’ve gotten some lying around P16NF06L, but I’ve no idea how to connect them (I’m not that experience with components, except for soldering them together).

Yes you can. This MOSFET has a threshold voltage (VGS th) of 2.5V, so it will be turned on by the 3.3V output of the GPIO.

Connect the GPIO pin to its gate, the source to ground and the drain to the shorter pin of the LED. The longer pin of the LED should be connected to a 5V power with a resistor in between to limit the current flow (else the LED will burn itself). That should be all. Whenever the GPIO is high the MOSFET will short the drain and the source so the LED will turn on.

1 Like

I think the limiting resistor is already fitted in the button if the description of the button says 5V connection, but it would be good to check.

1 Like

@sachinss Great, and thankyou for spelling it out to me! It’s a big help.
So I’ve soldered it like this:

That should be correct, right?

@pepe59 Hmm, I can’t find my order for the buttons on banggood any more, they were ordered years ago, and if I try and measure resistance across the diode, my multimeter just says no connection, so I guess I’d better go with a resistor.
I have a couple lying around in a heap of mess, as far as I can it’s a 33 Ohm and 110 Ohm, so I guess they are too small, right?


I guess need something like 1 kOhm?

Yes that seems fine. Make sure there’s no short.

1 Like

Any input on the resistor size?

If there is a standard LED diode inside the button, the diode should have approx. 2V, current 20mA and input voltage 5V, the limiting resistance will be 150 Ω.
I still don’t believe that there is no limiting resistor inside the button.

Just verify by connecting the LED button to 3V(pay attention to the polarity of the LED +/-) and if the LED lights up weakly or at all, connect it to 5V and if it lights normally then the button contains a limiting resistor. It can also be checked with a multimeter, but measure the LED in the forward direction.
The link to the buttons is that the LED switches on only when the button is pressed

Ok, I’ll try that tomorrow, office is now a guestroom :slight_smile: