Main Menu

search

You are here

RTC Program Code

[last updated: 2024-08-06]
PCF8523 programming protocols
RTC - PCF8523
-----


          Generic code for 8523 with 128x32 OLED:
      ```cpp

      // EDIT THIS LINE ********************************
      String programName = "RTC_01";

      long baudRateSerial = 57600;
      // -------------------------------------
      //
      // Purpose:
      // this program shows the minimum code needed
      // to write text to an OLED display
      //
      // Edit History:
      // rev. 01: from OLED_minimum_02.ino
      // adding hardware for PCF8523 RTC
      // libraries added, RTC declaration and rtc.adjust working
      // big bold font installed and working
      // hrs & mins extracted and displayed on OLED
      //
      // Program operation/flow:
      // ...
      //
      //
      // Hardware configuration:
      // 13 onboard LED
      // A4 to OLED-SDA
      // A5 to OLED-SCL
      // gnd and 3.3v connected from Uno to OLED
      //
      // RTC: PCF8523
      // RTC gnd connected to MCU gnd
      // RTC Vcc connected to +5v from Nano
      // RTC SDA connected to A4 of Nano
      // RTC SCL connected to A5 of Nano
      // ---------------------------------

      // ************** INCLUDES *******************

      //#include // this is not needed for text, but IS needed for any graphics
      #include // this is the minimum required library
      #include

      #include

      // ---------- END includes ------------

      // ************** GLOBALS *******************

      #define SCREEN_WIDTH 128 // OLED display width, in pixels
      #define SCREEN_HEIGHT 32 // OLED display height, in pixels

      // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
      #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
      Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

      RTC_PCF8523 rtc;

      String textToPrint = "init";
      String strHrNow;
      String strMinNow;

      // ---------------- END globals -----------------

      // ************** SETUP *******************

      void setup() {

      Serial.begin (baudRateSerial);
      printPreamble();

      // for RTC
      if (! rtc.begin()) {
      Serial.println("Couldn't find RTC");
      Serial.flush();
      while (1) delay(10);
      }

      delay(2000); // wait for RTC to initialize
      if (! rtc.initialized() || rtc.lostPower()) {
      Serial.println("RTC is NOT initialized, let's set the time!");
      rtc.adjust(DateTime(2024, 8, 5, 7, 5, 0)); // rtc.adjust(DateTime(yyyy, mo, day, hr, min, sec))
      }

      //rtc.adjust(DateTime(2024, 8, 5, 21, 6, 0));

      rtc.start();
      // ------------------------------------------

      // for OLED
      if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
      Serial.println(F("SSD1306 allocation failed"));
      for(;;); // Don't proceed, loop forever
      }

      // Clear the buffer
      display.clearDisplay();
      // draw border
      display.drawRect(0, 0, 128, 32, SSD1306_WHITE);
      display.display();

      // set text parameters
      display.setFont(&FreeSansBold18pt7b);
      display.setTextColor(WHITE);
      display.setCursor(3, 25); // y=6 centers text-size-3 vertically in screen
      display.setTextSize(1);

      display.print("init...");
      display.display();

      delay(1500);

      } // -------------- END setup --------------

      // ************** LOOP *******************

      void loop() {

      clrCtr();

      delay(1500);

      DateTime now = rtc.now();

      uint16_t yrNow = now.year();
      uint16_t hrNow = now.hour();
      uint16_t minNow = now.minute();

      if (hrNow < 10) {
      strHrNow = "0" + String(hrNow);
      } else {
      strHrNow = String(hrNow);
      }
      if (minNow < 10) {
      strMinNow = "0" + String(minNow);
      } else {
      strMinNow = String(minNow);
      }

      textToPrint = strHrNow + ":" + strMinNow;

      display.setCursor(20, 27);
      display.setTextSize(1);
      clrCtr();
      // print to OLED
      display.print(textToPrint);
      display.display();
      delay(2000);

      } // --------------- END loop ---------------

      // **************** FUNCTION DEFS *****************

      // list of function defs:

      // printPreamble
      // clrCtr
      // --------------------------

      void printPreamble() {

      delay(100);
      Serial.println(" "); Serial.println(" ---------- ");
      Serial.print("Program name: "); Serial.println(programName);
      Serial.println(" ---------- ");

      } // ---------------- END printPreamble ----------------

      void clrCtr() {
      display.fillRect(1, 1, 126, 30, BLACK); // clear center of screen
      } // ----------------- END clrCtr ----------------------

      // --------------- END function defs ------------

      // --------------- END program ------------------

      ```

    -------------------------------------------------------------


.

eof