ESP32 NTP Local AM-PM Time Display

ESP32 NTP Local AM-PM Time Display…

Just the other day I was asked for a way to display local time (am/pm) on an OLED…

It’s quite easy to achieve using a couple of include code libraries to format DateTime from the NTP server.

So lets have a go at displaying am/pm time using the esp32… it may not be elegant, but it does show the time!


We have a ESP32 development board; I am using a LOLIN32 D32 purchased for Phipps Electronics. Nice board every thing you need LiIon/LiPo battery charging circuit and fits a normal breadboard with a row of holes on both sides; for around $20 AUD, not bad!

Hopefully you can see by the pic’s, if you’re careful about soldering the headers you get the pin definitions showing on top of the board. Always disable in my opinion.

Note: I have no affiliation with Phipps Electronics, dealt with them a few times, like the stuff they handle and the service is second to none.

A standard 0.95″ SSD1306 OLED, in this case from my bits box, a breadboard with jumper wires and a 2500mAh Power Bank for portable power completes the hardware.


SCL pin from the OLED goes to GPIO 22 on the ESP32
SDA pin from the OLED goes to GPIO21 on the ESP32
VCC in this case is 3.3v
GND goes to GND


Copy this source code listing to a new project in your Arduino-IDE after setting the type of ESP32 board you are using and which COM port it is connected to. Save as whatever file name you like. I used the very imaginative name of esp32-local-time.ino.

Remember to Edit the SSID and Password.

Set the DateTime.setTimeZone(+10) value ie the +10 to your time zone offset

Here is a link to the time zones timezones

Here is the code.

 * Based on code example in ESPDateTime library
 * Simple Mods by Al McDivitt
 * ESPDateTime Library
 * SSD1306  Library
#include <WiFi.h>

#include "ESPDateTime.h" //

// don't forget to change this to real ssid/password
// or set these in config.h and include it


#include <Wire.h>
#include "SSD1306.h" //
SSD1306  display(0x3c, 21, 22);

unsigned long lastMs = 0;
unsigned long ms = millis();

void setupWiFi() {
  Serial.print("WiFi Connecting");
  while (WiFi.status() != WL_CONNECTED) {

void setupDateTime() {
  // setup this after wifi connected
  // you can use custom timeZone,server and timeout
  // DateTime.setTimeZone(-4);
  //   DateTime.setServer("");
  //   DateTime.begin(15 * 1000);

  DateTime.setTimeZone(+10); // note +10 is UTC +10 for Brisbane Australia.

  // set above for your time zone as per list
  if (!DateTime.isTimeValid()) {
    Serial.println("Failed to get time from server.");

void setup() {

  display.drawString(23, 15, "iotXplain");
  display.drawString(18, 30, "Connecting.");

  time_t t =;
  Serial.println(DateFormatter::format("Now it's %F %I:%M%p.", t));

void loop() {
if (millis() - ms >5000) { //display 5000 ms  note 1 minute = 60000 ms
    ms = millis();
    if (!DateTime.isTimeValid()) {
      Serial.println("Failed to get time from server, retry.");
    } else {
      time_t t =;
    Serial.println(DateFormatter::format("%I:%M%p.", t));
  display.drawString(21, 15, DateFormatter::format("%I:%M%p.", t));

Now, with the hardware attached to your computer all you need to do is compile and upload to the device. Give it a second or two and you should see the connecting message, then the local time displayed on the OLED Display.

I’ll go over a quick step by step on adding a library to the Arduino-IDE from a downloaded zip file, just in case some may have not done it before. You will need to add two library’s to make this project work.

Installing a Arduino-IDE library from .zip file

Oh no an error, what will I do?! (See mom those drama classes did come in handy).

We check and find we need the ESPDateTime Library, get it’s address on and go there.

We download the zip file to a folder for later installation.

Select Sketch -> Include Library -> Add .ZIP Library..

Naturally we select the .zip file downloaded earlier.

We should get the Library added message.

Then recompile our code.

If we get the -> Done Compiling message. we are good to go!

We can upload our working code to the device, easy.

And that’s all there is to it…

… hope you had some fun building a IOT time piece for the desk.

Cheer’s Al