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

SSD1306 display: NTP clock with ESP8266 or ESP32

SSD1306 OLED display, white text version SSD1306 OLED display, blue text version
SSD1306 on Amazon · blue text version — 0.96 inch OLED display, driven over I2C

A small, inexpensive OLED display with room for more information than a 7-segment display — date, time and seconds, using the same code on both ESP8266 and ESP32.

What do you need for this?

SSD1306 display + ESP8266 | ESP32

This is a small and inexpensive display (0.96"), with easy connection via the same pins as I2C. This display can show much more information than an LED segment display. This example code can be put on both the ESP8266 and the ESP32 without changes. Of course, SCL and SDA must be connected to D1 (GPIO5) and D2 (GPIO4) [ESP8266] or to 22 and 21 [ESP32] respectively.

In the code below, we have set a 4-hour interval to synchronize with SNTP.

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/
#if defined(ESP32) 
#include <esp_sntp.h>  // ESP32 core
#endif
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h>

WiFiManager myWiFi;
Adafruit_SSD1306 display(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(2, 0x3C);
  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
#if defined(ESP32)
  sntp_set_sync_interval(4 * 60 * 60 * 1000UL);
#endif
  configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "be.pool.ntp.org");
}

void loop() {
  getLocalTime(&tInfo);  // sntp sync at startup & every 4 hours from then on
  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();
}
#if defined(ESP8266)
uint32_t sntp_update_delay_MS_rfc_not_less_than_15000() {  // declare only, unnecessary to call this function.
  return 4 * 60 * 60 * 1000UL;                             // NTP sync every 4 hr
}
#endif
Sync interval On the ESP32, sntp_set_sync_interval() sets the synchronization interval to 4 hours. On the ESP8266, that's done through the function sntp_update_delay_MS_rfc_not_less_than_15000(), which you only need to declare — the ESP core calls it itself.

More displays with the ESP8266 / ESP32

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