I’ve been working some more on my 8×8 LED matrix clock.

I’ve added a DS1307 RTC chip, which has to run from the 5v pin on the NodeMCU and use 10k pullup resistors between 3.3v and the SCL/SDA pins on the NodeMCU and DS1307. I’m using the Adafruit fork of RTCLib as it has better ESP8266 support and uses unixtime. I’m using this chip as I’ve got a load of them and already have a veroboard setup with the coincell and crystal.

I’ve also added a button, which when pressed reads the time from the RTC and displays it, then it fetches the NTP time over the internet and updates the display (and RTC). Also a single pixel is lit when the display is showing NTP time, so I can differentiate between RTC and NTP. I’ve done it this way as sometimes it can take up to 10secs to fetch the NTP time, and I really wanted something to display instantly. The battery-backed RTC means it will still show accurate time if there is no internet access. The display sleeps after 5secs.

I’m not sure if I’m going to replace the NodeMCU with just a bare ESP-12E on some breadboard fed by a LiFePO4 battery, or stick with the whole NodeMCU powered from a USB mains adaptor. The 5v supply is quite handy, and means I don’t need to add a step-up just for the DS1307.

Update: _I’ve ordered some DS3231 RTC modules which run from 3.3v, although I must remember to cut the trace or remove the diode/resistor trickle charge circuit as I’ll be using them with non-rechargeable CR2032 batteries. So I can remove the pullup resistors and run the whole clock from 3.3v

I’ve also moved the button to the RST pin and implemented an infinite deepSleep(0); so that the MCU and display sleeps until you reset the MCU. I’ve yet to measure the current consumption.

For the 8×8 LED matrices I’m using my fork of the LedControl library. I’m considering soldering the pin headers a bit differently than they currently are – maybe facing inwards on the underside, rather than sticking out of the top and bottom.

The pinout for the NodeMCU is a bit weird when using Arduino instead of NodeLua, here is the pin mapping, my setup is:

DIN : D7 (GPIO-13)
CLK : D5 (GPIO-14)
CS  : D3 (GPIO-0)
VCC : 3V3

SCL : D1 (GPIO-5), pullup resistor goes to 3.3v
SDA : D2 (GPIO-4), pullup resistor goes to 3.3v
5v  : VIN

Button : RST, other side goes to GND

Here’s a video of it in action before I added the RTC – notice it takes a couple of seconds for the NTP request after the button is pressed:

The code is:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "LedControl.h"
#include <Wire.h>
#include <RTClib.h>

// local port to listen for udp packets
unsigned int localPort = 2390;

// ntp server pool
IPAddress timeServerIP;
const char* ntpServerName = "europe.pool.ntp.org";

// ntp time stamp is in the first 48 bytes of the message
const int NTP_PACKET_SIZE = 48;

// buffer to hold incoming and outgoing packets
byte packetBuffer[NTP_PACKET_SIZE];

// create a udp instance
WiFiUDP udp;

// data, clock, cs, numdevices
LedControl lc = LedControl(D7,D5,D3,4);

// ds1307 constuctor

const int num[10][8] = {
    {0x00,0x78,0xcc,0xec,0xfc,0xdc,0xcc,0x78}, // zero
    {0x00,0xfc,0x30,0x30,0x30,0x30,0xf0,0x30}, // one
    {0x00,0xfc,0xcc,0x60,0x38,0x0c,0xcc,0x78}, // two
    {0x00,0x78,0xcc,0x0c,0x38,0x0c,0xcc,0x78}, // three
    {0x00,0x0c,0x0c,0xfe,0xcc,0x6c,0x3c,0x1c}, // four
    {0x00,0x78,0xcc,0x0c,0x0c,0xf8,0xc0,0xfc}, // five
    {0x00,0x78,0xcc,0xcc,0xf8,0xc0,0x60,0x38}, // six
    {0x00,0x60,0x60,0x30,0x18,0x0c,0xcc,0xfc}, // seven
    {0x00,0x78,0xcc,0xcc,0x78,0xcc,0xcc,0x78}, // eight
    {0x00,0x70,0x18,0x0c,0x7c,0xcc,0xcc,0x78}  // nine

void drawNum(int number, int display)
    lc.setColumn(display, 0, num[number][0]);
    lc.setColumn(display, 1, num[number][1]);
    lc.setColumn(display, 2, num[number][2]);
    lc.setColumn(display, 3, num[number][3]);
    lc.setColumn(display, 4, num[number][4]);
    lc.setColumn(display, 5, num[number][5]);
    lc.setColumn(display, 6, num[number][6]);
    lc.setColumn(display, 7, num[number][7]);

// send an ntp request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
    Serial.println("Sending NTP packet...");

    // set all bytes in the buffer to 0
    memset(packetBuffer, 0, NTP_PACKET_SIZE);

    packetBuffer[0] = 0b11100011;   // li, version, mode
    packetBuffer[1] = 0;            // stratum, or type of clock
    packetBuffer[2] = 6;            // polling interval
    packetBuffer[3] = 0xEC;         // peer clock precision

    // 8 bytes of zero for root delay & root dispersion
    packetBuffer[12] = 49;
    packetBuffer[13] = 0x4E;
    packetBuffer[14] = 49;
    packetBuffer[15] = 52;

    // all ntp fields have been given values, send request
    udp.beginPacket(address, 123);
    udp.write(packetBuffer, NTP_PACKET_SIZE);

void displayDate(unsigned long unixtime)
    // power up led matrices
    for (int i=0; i<4; i++)
        lc.shutdown(i,false); // come out of powersaving
        lc.setIntensity(i,8); // set brightness 0-15
        lc.clearDisplay(i);   // clear display

    // print hour to led
    int myhour = (unixtime % 86400L) / 3600;

    // print minute to led
    int myminute = (unixtime % 3600) / 60;
    if (myminute < 10)

void gotoSleep()
    for (int i=0; i<4; i++)

    // latch cs pin and pause to work around display0 being stuck high on sleep problem
    digitalWrite(D3, LOW);

    // sleep mcu forever

void setup()
    // setup ds1307

    // display rtc time on led matrices
    DateTime now = RTC.now();

    // debug

    // connect to wifi network
    WiFi.begin("ssid", "password");

    // static ip, gateway, netmask
    WiFi.config(IPAddress(192, 168, 1, 2), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));

    // connect - could drain battery if never connects to wifi
    int wifitries = 0;
    while (WiFi.status() != WL_CONNECTED)
        // give it a moment

        // only try 5 times before sleeping
        Serial.print("Wifi connect tries: ");
        if (wifitries >5)
            Serial.println("Gave up on wifi");

    Serial.println("Starting UDP");
    Serial.print("Local port: ");

    // get a random server from the pool
    WiFi.hostByName(ntpServerName, timeServerIP);

    int cb = 0;
    int ntptries = 0;
    while (!cb)
        // send an ntp packet to a time server

        // wait to see if a reply is available
        cb = udp.parsePacket();

        // only try 5 times before sleeping
        Serial.print("NTP connect tries: ");
        if (ntptries >5)
            Serial.println("Gave up on NTP");

    // we&#039;ve received a packet, read the data from it
    Serial.print("Packet received, length=");

    // read the packet into the buffer
    udp.read(packetBuffer, NTP_PACKET_SIZE);

    // the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. first, esxtract the two words:
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);

    // combine the four bytes (two words) into a long integer
    // this is ntp time (seconds since jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    Serial.print("Seconds since Jan 1 1900 = ");

    // now convert ntp time into everyday time:
    Serial.print("Unix time = ");

    // unix time starts on jan 1 1970. in seconds, that&#039;s 2208988800:
    const unsigned long seventyYears = 2208988800UL;

    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears;

    // print unix time:

    // update ds1307 from ntp unixtime
    Serial.println("Updated RTC");

    // utc is the time at gmt
    Serial.print("The UTC time is ");

    // print the hour - 86400 equals secs per day
    Serial.print((epoch % 86400L) / 3600);

    // in the first 10 minutes of each hour, we&#039;ll want a leading &#039;0&#039;
    if (((epoch % 3600) / 60) < 10)

    // print the minute (3600 equals secs per minute)
    Serial.print((epoch % 3600) / 60);

    // in the first 10 seconds of each minute, we&#039;ll want a leading &#039;0&#039;
    if ((epoch % 60) < 10)

    // print the second
    Serial.println(epoch % 60);

    // display ntp time on led matrices

    // ntp has a dot rtc does not
    lc.setLed(3, 7, 0, true);

    // wait five seconds before shutting down

void loop()

Edit: finished video.