Simple SH1106 display: how to?

Hi all,

I can’t get my SH1106 (https://www.amazon.de/gp/product/B078J78R45/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&th=1) to work at all.

I have my ESP32 set up and it arrived in Home Assistant “well”.

I then attached the four cables to the ESP32:

  • VDD to 3V3
  • GND to GND
  • SDA to GPIO14
  • SCK to GPIO26 (don’t know why it says SCK instead of SCL on the display pcb)

I googled for the code to use and there I started to get confused:

esphome:
  name: dst-schreibtisch
  friendly_name: DST_Schreibtisch

esp32:
  board: esp32dev
  framework:
    type: arduino

text_sensor:
  - platform: homeassistant
    name: "EspText"
    id: esp_text
    entity_id: input_text.esptext

i2c:
  sda: 14
  scl: 26

font:
  - file: "gfonts://Roboto"
    id: roboto
    size: 10

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    update_interval: 0.5s
    address: 0x3C
    lambda: |-
      it.printf(0, 10,id(roboto), "%s", id(esp_text).state.c_str());

So I couldn’t find anything on the platform and the model to use correctly.

I found some info here: Need help with SH1106 Oled Display jivgym uses the same model, so I copied his code.

He isn’t showing the i2c section, but I got that from here: Output data from one ESP32 to two SH1106 oled displays I’m using a different notation as ESPhome says that this was correct for my board, and I have been using this notation on other ESP devices of the same model/type.

So I think the code is kind of ok?

So I uploaded the code, but then everything seems to fall apart.

The ESP reports this when I plug it in and load the log window:

[11:06:18][W][component:237]: Component display took a long time for an operation (281 ms).
[11:06:18][W][component:238]: Components should block for at most 30 ms.
[11:06:18][W][component:157]: Component wifi set Warning flag: scanning for networks
[11:06:18][W][component:237]: Component display took a long time for an operation (280 ms).
[11:06:18][W][component:238]: Components should block for at most 30 ms.
[11:06:19][W][component:237]: Component display took a long time for an operation (280 ms).
[11:06:19][W][component:238]: Components should block for at most 30 ms.
[11:06:19][W][component:237]: Component display took a long time for an operation (280 ms).
[11:06:19][W][component:238]: Components should block for at most 30 ms.
[11:06:20][W][component:237]: Component display took a long time for an operation (280 ms).
[11:06:20][W][component:238]: Components should block for at most 30 ms.
[11:06:20][W][component:237]: Component display took a long time for an operation (280 ms).
[11:06:20][W][component:238]: Components should block for at most 30 ms.
[11:06:21][W][component:237]: Component display took a long time for an operation (280 ms).
[11:06:21][W][component:238]: Components should block for at most 30 ms.
[11:06:21][W][component:237]: Component display took a long time for an operation (280 ms).
[11:06:21][W][component:238]: Components should block for at most 30 ms.

Also, I see no text sensor in Home Assistant, although the log says that it created one:

[11:06:57][C][homeassistant.text_sensor:023]: Homeassistant Text Sensor 'EspText'
[11:06:57][C][homeassistant.text_sensor:024]:   Entity ID: 'input_text.esptext'

Any idea what I have done wrong this time? :frowning:

Hi

this is how I declare my display(s):


i2c:
  sda: GPIO21
  scl: GPIO22
  scan: false

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    address: 0x3C
    contrast: 70%
    id: blue_display

  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    address: 0x3D
    id: white_display

I think I’m using the same type as you, maybe that helps. Cheers!

Try with basic setup with default I2C pins 21/22:

esphome:
  name: dst-schreibtisch
  friendly_name: DST_Schreibtisch

esp32:
  board: esp32dev
  framework:
    type: arduino


i2c:
  sda: 21
  scl: 22
  scan: true

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    address: 0x3C
    lambda: |-
     it.print(0, 0, "Hello World!");

ps. you don’t have wifi and home assistant Api configured, so HA sensor doesn’t work (yet).

I tried your solution, swapped the GPIO pins, the ESP boots and shows this message:

[15:06:06][C][wifi:600]: WiFi:
[15:06:06][C][wifi:428]:   Local MAC: 40:91:51:22:A9:E0
[15:06:06][C][wifi:433]:   SSID: [redacted]
[15:06:06][C][wifi:436]:   IP Address: 192.168.1.110
[15:06:06][C][wifi:440]:   BSSID: [redacted]
[15:06:06][C][wifi:441]:   Hostname: 'dst-schreibtisch'
[15:06:06][C][wifi:443]:   Signal strength: -52 dB ▂▄▆█
[15:06:06][C][wifi:447]:   Channel: 1
[15:06:06][C][wifi:448]:   Subnet: 255.255.248.0
[15:06:06][C][wifi:449]:   Gateway: 192.168.0.1
[15:06:06][C][wifi:450]:   DNS1: 192.168.0.1
[15:06:06][C][wifi:451]:   DNS2: 0.0.0.0
[15:06:07][W][component:237]: Component display took a long time for an operation (282 ms).
[15:06:07][W][component:238]: Components should block for at most 30 ms.
[15:06:07][C][logger:185]: Logger:
[15:06:07][C][logger:186]:   Level: DEBUG
[15:06:07][C][logger:188]:   Log Baud Rate: 115200
[15:06:07][C][logger:189]:   Hardware UART: UART0
[15:06:07][C][i2c.arduino:071]: I2C Bus:
[15:06:07][C][i2c.arduino:072]:   SDA Pin: GPIO21
[15:06:07][C][i2c.arduino:073]:   SCL Pin: GPIO22
[15:06:07][C][i2c.arduino:074]:   Frequency: 50000 Hz
[15:06:07][C][i2c.arduino:086]:   Recovery: bus successfully recovered
[15:06:07][C][ssd1306_i2c:023]: I2C SSD1306
[15:06:07][C][ssd1306_i2c:023]:   Rotations: 0 °
[15:06:07][C][ssd1306_i2c:023]:   Dimensions: 128px x 64px
[15:06:07][C][ssd1306_i2c:024]:   Address: 0x3C
[15:06:07][C][ssd1306_i2c:025]:   Model: SH1106 128x64
[15:06:07][C][ssd1306_i2c:027]:   External VCC: NO
[15:06:07][C][ssd1306_i2c:028]:   Flip X: YES
[15:06:07][C][ssd1306_i2c:029]:   Flip Y: YES
[15:06:07][C][ssd1306_i2c:030]:   Offset X: 0
[15:06:07][C][ssd1306_i2c:031]:   Offset Y: 0
[15:06:07][C][ssd1306_i2c:032]:   Inverted Color: NO
[15:06:07][C][ssd1306_i2c:033]:   Update Interval: 0.5s
[15:06:07][C][captive_portal:089]: Captive Portal:
[15:06:07][C][mdns:116]: mDNS:
[15:06:07][C][mdns:117]:   Hostname: dst-schreibtisch
[15:06:07][C][esphome.ota:073]: Over-The-Air updates:
[15:06:07][C][esphome.ota:074]:   Address: dst-schreibtisch.local:3232
[15:06:07][C][esphome.ota:075]:   Version: 2
[15:06:07][C][esphome.ota:078]:   Password configured
[15:06:07][C][safe_mode:018]: Safe Mode:
[15:06:07][C][safe_mode:020]:   Boot considered successful after 60 seconds
[15:06:07][C][safe_mode:021]:   Invoke after 10 boot attempts
[15:06:07][C][safe_mode:023]:   Remain in safe mode for 300 seconds
[15:06:07][C][api:140]: API Server:
[15:06:07][C][api:141]:   Address: dst-schreibtisch.local:6053
[15:06:07][C][api:143]:   Using noise encryption: YES
[15:06:07][W][component:237]: Component display took a long time for an operation (281 ms).
[15:06:07][W][component:238]: Components should block for at most 30 ms.
[15:06:07][D][api:103]: Accepted 192.168.1.100
[15:06:07][W][component:237]: Component api took a long time for an operation (51 ms).
[15:06:07][W][component:238]: Components should block for at most 30 ms.
[15:06:07][D][api.connection:1446]: Home Assistant 2025.1.3 (192.168.1.100): Connected successfully
[15:06:08][W][component:237]: Component display took a long time for an operation (281 ms).
[15:06:08][W][component:238]: Components should block for at most 30 ms.
[15:06:08][W][component:237]: Component display took a long time for an operation (281 ms).
[15:06:08][W][component:238]: Components should block for at most 30 ms.
[15:06:09][W][component:237]: Component display took a long time for an operation (281 ms).
[and so on]

So still nothing :-/

Call me stupid, it says “Hello world” on the display :smiley: I’m too dumb to read a display!

Ok, so how do I get custom text on the display?

What you exactly want to display?

My idea of this “project” was do measure the distance via a different ESP and an ultrasonic sensor, and display the distance on this OLED.

Other ideas include “time for a break” every day at 10am … something like that :slight_smile:

For that you don’t even need home assistant, Esphome can do it…
Anyway, start adding components for your setup:

logger:
  
api:

ota:
  - platform: esphome
    password: ""

# Example configuration entry
wifi:
  ssid: MyHomeNetwork
  password: VerySafePassword

Then you can add your font, text sensor and print from your first post.

1 Like

Awesome, ok, so it works with the following code:

esphome:
  name: dst-schreibtisch
  friendly_name: DST_Schreibtisch

esp32:
  board: esp32dev
  framework:
    type: arduino

text_sensor:
  - platform: homeassistant
    name: "EspText"
    id: esp_text
    entity_id: input_text.esptext

i2c:
  sda: 21
  scl: 22
  scan: false

font:
  - file: "gfonts://Roboto"
    id: roboto
    size: 10

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    update_interval: 0.5s
    address: 0x3C
    id: normal_display
    contrast: 70%
    lambda: |-
     it.print(0, 0, id(roboto), id(esp_text).state.c_str());

I then had to create a new helper/text sensor in HA, either in configuration.yaml or … well … as a helper:

input_text:
  esptext:
    name: "ESP Text"
    initial: "Hello World! :-)"

Now I can edit this helper and it will show the content immediately on the display :smiley:

Thank you guys so much!