↩ All projects and sketches Deze pagina in het Nederlands
ESP8266 · ESP32 · SH1106

SH1106 display: NTP clock with ESP8266 or ESP32

SH1106 OLED display (1.3 inch) showing an NTP clock
SH1106 (blue text) on AliExpress — 1.3 inch OLED display, driven over I2C

A slightly larger OLED display than the SSD1306, also over I2C, with the same code usable on both ESP8266 and ESP32.

What do you need for this?

SH1106 display + ESP8266 | ESP32

This is a slightly larger display (1.3 inches), also easy to connect via the same pins as I2C. Compared to the SSD1306 code above, the library was changed and the time synchronization interval omitted. This code can also be used on both the ESP8266 and ESP32 without changes. Of course, SCL and SDA must go to D1 (GPIO5) and D2 (GPIO4) [ESP8266] or to 22 and 21 [ESP32] respectively.

Connections:

ESP32 ---> SDD1306 / SH1106
===========================
GND  --------> GND
3V3  --------> VCC
G21  --------> SDA
G22  --------> SCL
===========================
ESP8266 -> SDD1306 / SH1106
===========================
GND  --------> GND
3V3  --------> VCC
D2   --------> SDA
D1   --------> SCL

Sketch:

#include <WiFiManager.h>      // https://github.com/tzapu/WiFiManager/
#include <Adafruit_SH110X.h>  // https://github.com/adafruit/Adafruit_SH110X
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h>

WiFiManager myWiFi;  
Adafruit_SH1106G display = Adafruit_SH1106G(128, 64, &Wire, -1);  // GND -> GND, 3V3 -> VCC, esp32: G21 -> SDA, G22 -> SCL
struct tm tInfo;  // https://cplusplus.com/reference/ctime/tm/          esp8266: D1 -> SCL,  D2 -> SDA

void setup() {
  Serial.begin(115200);       // watch serial monitor in case of new WiFi-connection
  display.begin(0x3C, true);  // Address 0x3C default
  display.setTextColor(1);    // white = 1, black = 0
  display.setFont(&FreeSansBold12pt7b);
  myWiFi.autoConnect("SSD1306_ESP");  // connect ESP to new wifi via GSM or PC if new WiFi-connection
  configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "be.pool.ntp.org");
}

void loop() {
  getLocalTime(&tInfo);  // sntp sync at startup & every hour from then on (esp8266) - every 3 hours (esp32)
  display.clearDisplay();
  display.drawRect(0, 0, 128, 40, 1);
  display.setFont(&FreeSansBold18pt7b);
  display.setCursor(4, 31);
  display.printf("%02d:%02d", tInfo.tm_hour, tInfo.tm_min);
  display.setFont(&FreeSansBold12pt7b);
  display.printf(":%02d", tInfo.tm_sec);
  display.setCursor(4, 63);
  display.printf("%02d-%02d-%04d", tInfo.tm_mday, 1 + tInfo.tm_mon, 1900 + tInfo.tm_year);
  display.display();
}
No sync interval set Unlike the SSD1306 sketch, no explicit SNTP interval is set here. On the ESP8266 that stays at the default of 1 hour, on the ESP32 at 3 hours.

More displays with the ESP8266 / ESP32

Curious about other displays? Check out the full overview, including TM1637, SSD1306, ST7735, ST7789, e-paper and integrated displays like the Lilygo T-Display and the Waveshare ESP32-S3.