Turn off OLED display after boot / restart

Hello,

First off: Great forum! Helped me a lot during ESPHome development. But now I got a problem, I’m unable to solve myself.

I have an ESP8266 NodeMCU v2 with a SSD1306 128x32 OLED display. I’m looking for a reliable way to turn off the display when the device is booting, rebooting, or after boot.

Currently I’m using this piece of code:

esphome:
  name: test2
  platform: ESP8266
  board: nodemcuv2
  on_boot:
    priority: -100
    then:
      - lambda: id(oled).turn_off();

display:
  - platform: ssd1306_i2c
    id: oled
    model: "SSD1306 128x32"
    address: 0x3C
    brightness: 0.5
    lambda: ... (omitted for clarity)

But this only works if the WiFi connection was successful. If WiFi connection is not working, the display stays turned on.

Is there any other way to start with display off, or make the code more reliable?

After boot I use a button with a 5s delay to turn it on and off automatically, which works great:

binary_sensor:
  - platform: gpio
    name: "Button"
    internal: true
    pin:
      number: D6
      mode: INPUT_PULLUP
    on_press:
      - lambda: id(oled).turn_on();
      - delay: 5s
      - lambda: id(oled).turn_off();

Thanks for your help and kind regards,
Ben

2 Likes

If there is no wifi connection ESPHome goes into a boot loop.

Setting the boot priority between 250 and 600 will execute it before wifi is set up.

Thank you!

I tried 250, 500 and 600, everything seems to work fine.

Is there any value more preferable over the other? Or some other way to make sure the display is off?

Only this, from the link I posted above:

  • priority ( Optional , float): The priority to execute your custom initialization code. A higher value means a high priority and thus also your code being executed earlier. Please note this is an ESPhome-internal value and any change will not be marked as a breaking change. Defaults to -10 . Priorities (you can use any value between them too):
  • 800.0 : This is where all hardware initialization of vital components is executed. For example setting switches to their initial state.
  • 600.0 : This is where most sensors are set up.
  • 250.0 : At this priority, WiFi is initialized.
  • 200.0 : Network connections like MQTT/native API are set up at this priority.
  • -100.0 : At this priority, pretty much everything should already be initialized.

You could power it from a GPIO that is LOW on boot and only send the pin high when you need it. See the table lover down on this page: ESP32 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials

You may need a switching transistor if the pin is not able to supply enough current.

Thanks again. Then I’ll just keep it at 250.

Using a separate GPIO for power sounds nice, but my ESP8266 has no more pins left :smile: . Maybe I’ll consider that when I switch to an ESP32.

That’s happened to me a few times. I don’t bother buying ESP8266s now.

My favourite ESP32: https://www.aliexpress.com/item/32815530502.html

Same footprint as the Wemos D1 mini (pin for pin compatible too) at an excellent price.

More info: https://forum.mhetlive.com/topic/8/mh-et-live-minikit-for-esp32

You can find it cheaper on aliexpress but I prefer to support the original developer.

1 Like

Good afternoon. Does your D6 pin number on the binary sensor match which one on the oled? SDA or SCL?
Thank you very much.