Part 2 of the SmartMon v2.7ex series.UPDATE !! SmartMon v2.7Ex is available also on Tindie Store !

This time we are exploring the Real Time Clock  implementation and we will see how easy or complicated is to set & program the onboard RTC Clock.

And the story behind:

For a complete monitoring/ data logging experience, a nice to have feature is a proper RTC clock. Having this in mind, for the new version of the SmartMon I choose to add it on-board for a very simple and easy integration in your related Voltage, Current, Power projects.

The choosen one is the PCF8563 from NXP, a very nice and easy to program CMOS Real-Time Clock (RTC) and Calendar optimized for low power consumption. A programmable clock output, interrupt output and voltage-low detector are also provided. All addresses and data are transferred serially via a two-line bidirectional I2C-bus with a maximum bus speed of 400 kbit/s.


•  Provides year, month, day, weekday, hours, minutes, and seconds based on a

        32.768 kHz quartz crystal
•  Century flag
     •  Clock operating voltage: 1.0 V to 5.5 V at room temperature
     •  Low backup current; typical 0.25uA at Vdd =3.0V and Tamb=25C
     •   400 kHz two-wire I2C-bus interface (at VDD= 1.8 V to 5.5 V)
     •   Programmable clock output for peripheral devices (32.768 kHz, 1.024 kHz, 32 Hz, and 1Hz)
     •   Alarm and timer functions – separate Alarm triggered MOSFET output with separate voltage
                     input/supply included onboard, can direct drive upto 2A external devices, 3/5/12V
                     Relays, interrupts, etc
     •   Integrated oscillator capacitor
     •   Internal Power-On Reset (POR)
     •   I2C-bus slave address: read A3h and write A2h
     •   Open-drain interrupt pin


                                    SmartMon v2.7Ex – RTC module schematic

For more details about the RTC, please see PCF8563 Datasheet and the related PCF8563 RTC clock driver Article

What we will need:

Connection with the ESP8266 nEXT EVO Board is very easy, as Analog Extension Board – AN1 connector is fully compatible with the nEXTBus connector:

                                             SmartMon v2.7Ex Board

This time we are talking here about ESP8266/LUA driver but also Arduino implementation will follow.

If you use another ESP8266, or Arduino, ARM, PIC, whatever MCU you use there days for your projects, then just be sure that you are connecting the I2C lines (SDA/SCL) on the allocated pins for your setup.

Driver implementation

As PCF8563 has a I2C compatible compatible interface, driver building it following more or less the same  process  as before for I2C devices.

For more details about the RTC, please take a deeper look at the  PCF8563 Datasheet and the related PCF8563 RTC clock driver Article.

1. Data conversion functions:

1.1 Decimal to BCD:

       function decToBcd(val)
             local d = string.format("%d",tonumber(val / 10))
             local d1 = tonumber(d*10)
             local d2 = val - d1
            return tonumber(d*16+d2)

1.2  BCD to Decimal:
      function bcdToDec(val)
           local hl=bit.rshift(val, 4)
          local hr = string.format("%d%d", hl, hh)
          return string.format("%d%d", hl, hh)
2. Init I2C bus/interface:
        address = 0x51, -- A2, A1, A0 = 0
        id = 0
        init = function (self, sda, scl)
      = 0
              i2c.setup(, sda, scl, i2c.SLOW)
3. ReadTime function:
   readTime = function (self)
       wkd = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
       i2c.address(, self.address, i2c.TRANSMITTER)
       i2c.write(, 0x02)
       i2c.address(, self.address, i2c.RECEIVER), 7)
       return  bcdToDec(,1),0x7f)),
4. SetTime function:
   setTime = function (self, second, minute, hour, day, date, month, year)
       i2c.address(, self.address, i2c.TRANSMITTER)
       i2c.write(, 0x02)
       i2c.write(, decToBcd(second))
       i2c.write(, decToBcd(minute))
       i2c.write(, decToBcd(hour))
       i2c.write(, decToBcd(day))
       i2c.write(, decToBcd(date))
       i2c.write(, decToBcd(month))
       i2c.write(, decToBcd(year))

For testing,  pack it together and save the code on ESP as ‘pcf8563.lua‘, restart ESP and run:

-- Set Initial Time and Date
 require('pcf8563')                                -- call for new created PCF8563 Module Driver
 sda, scl = 2, 1                                      --  declare your I2C interface PIN's 
pcf8563:init(sda, scl)                           -- initialize I2C Bus
  pcf8563:setTime(0,34,13,12,4,3,15)   -- setTime(s,min,hour,day,weekday,month, year)
 -- get Time and Date
 sda, scl = 2, 1
 pcf8563:init(sda, scl)
s, m, h, d, dt, mn, y = pcf8563:readTime()        --ReadTime function call
=string.format("%s - %s/%s/20%s",dt, d, mn, y)
=string.format(" %s:%s:%s", h, m, s)


Next Article in series can be found here Part 3: INA219 Driver example and software implementation

Creative Commons License SmartMon series boards, software and articles by are licensed under a Creative Commons Attribution-NonCommercial 4.0 International License

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

battery monitor system

Thingspeak – SmartMon Ext Board P4

This is Part 4 of the SmartMon Extension Board series. SmartMon v2.7ex Board  is a extension board fully compatible with Arduino, ESP8266, ARM, PIC & other available MCu’s out there.  As long as your MCU/Dev Read more…

battery monitor system

Smart Mon Ext Board – Software example – P3

This is Part 3 of the SmartMon Extension Board series. SmartMon v2.7ex Board  is a extension board fully compatible with Arduino, ESP8266, ARM, PIC & other available MCu’s out there.  As long as your MCU/Dev Read more…


MCP4728 – 12 Bit I2C DAC Driver – AN1

From today we will move on the Analog interface part of the nEXT EVO Board AN-1 and we will start talking about the Digital to Analog conversion, Analog Autoscaling input and Analog to Digital conversion Read more…