Jan 5, 2018

How to Add Real Time Clock
to Arduino

PCF8523 Real Time Clock

I bought two high accuracy RTC (real time clock) modules. They can run for years off of a coin cell battery, even when the Arduino loses power, or is reprogrammed. Both of these modules are special in they claim higher accuracy than your typical RTC. What is a RTC? Is a battery-powered clock that is included in a microchip in a computer motherboard. This microchip is usually separate from the microprocessor and other chips and is often referred to simply as "CMOS". A it runs on a battery and keeps time for you even when there is a power outage! Most microcontrollers, including the Arduino, have a built-in timekeeper, here are also timers built into the chip that can keep track of longer time periods like time. So why would you need to have a separate RTC chip?. Reason is that timekeeper called millis() only keeps track of time since the Arduino was last powered.

That means that when the power is turned on, the millisecond timer is set back to 0. The Arduino doesn't know that it's 'Sunday' or 'April 15th ', all it can tell is 'It's been 14,000 milliseconds since I was last turned on. What if you wanted to set the time on the Arduino? You'd have to program in the date and time and you could have it count from that point on. But if it lost power, you'd have to reset the time. Much like battery powered alarm clocks, every time they lose power.

Getting Started

Hook up your RTC module

  1. GND to GND on your board
  2. VCC to the logic level power of your board (on classic Arduinos & Metros use 5V, on 3.3V devices use 3.3V)
  3. SDA to the SDA i2c data pin
  4. SCL to the SCL i2c clock pin
  5. There are internal 10K pull-ups on the PCF8523 on SDA and SCL to the VCC voltage

arduino nanao pin out

arduino nano pins

Talking to the RTC

The RTC is an i2c device, which means it uses 2 wires to communicate. These two wires are used to set the time and retrieve it. For the RTC library, we'll be using a fork of JeeLab's excellent RTC library, which is available on GitHub. You can do that by visiting the github repo and manually downloading or, easier go to the

  • First open Arduino

  • Type in RTClib, and look for Adafruit and click Install
  • Once done, restart the IDE

  • Open up Examples > RTClib > pcf8523
  • Upload it to your board with the PCF8523

  • Now open up the Serial Console and make sure the baud rate is set at 57600 baud, and you should see the following:

    When there is a power outage! (including the backup battery) it will reset to an earlier date and report the time as 0:0:0. Whenever you set the time, this will kickstart the clock ticking. So, basically, the upshot here is that you should never remove the battery once you've set the time.

    • Setting the time
    • With the same sketch loaded, uncomment the line that starts with RTC.adjust

    This line take the Date and Time according the computer you're using (right when you compile the code) and uses to program the RTC. Press the Upload button to compile and then immediately upload. If you upload later, the clock will be off by that amount of time.

    • Open up the Serial monitor window to show the time has been set

    You won't have to ever set the time again and the battery will last 5 or more years

    • Reading the time
    • Let's look at the sketch again to see how this is done

    The actual code is not worth going into much detail with, just know that you need to run it once to perform the initial time synchronisation. There may be a lot of code, however it breaks down well into manageable parts. It first includes the Wire library, which is used for I2C bus communication, followed by defining the bus address. Upload the code, open up the serial console at 57600 baud, and watch and your Arduino outputs the current time every second. Marvellous!

    We can also get a 'timestamp' out of the DateTime object by calling unixtime which counts the number of seconds (not counting leapseconds) since midnight, January 1st 1970 Since there are 60*60*24 = 86400 seconds in a day, we can easily count days since then as well. This might be useful when you want to keep track of how much time has passed since the last query, making some math a lot easier (like checking if it's been 5 minutes later, just see if unixtime() has increased by 300, you don't have to worry about hour changes) There's one problem where if you happen to ask for the minute right at 12:14:59 just before the next minute rolls over, and then the second right after the minute rolls over at 12:15:00 you'll see the time as 12:14:00 which is a minute off. If you did it the other way around you could get 12:15:59 - so one minute off in the other direction.

    Please also check OLED I2c Display With Arduino .