Menu Close

Smart Pen Stand Kit for Arduino UNO

Buy Kit – Amazon

Buy Kit – Robo India

Install the required library in Arduino IDE software.

Before compiling the code Install the following libraries.

RTC library:
Go to – Sketch >Include Library > Manage Libraries
and install the library shown in the image below.

I2C LCD library:
Go to – Sketch >Include Library > Manage Libraries
and install the library shown in the image below.

Arduino Code:

After installing the library upload the below code to your Arduino board.
You can also download the code here. Download Arduino Code

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"
// LCD I2C Libraries
#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>

// declare LCD with the address.
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display

int lcd_show = -1;

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  Serial.begin(57600);
  // lcd function
  int error;
  Serial.println("Dose: check for LCD");

  // See http://playground.arduino.cc/Main/I2cScanner how to test for a I2C device.
  Wire.begin();
  Wire.beginTransmission(0x27);
  error = Wire.endTransmission();
  Serial.print("Error: ");
  Serial.print(error);

  if (error == 0) {
    Serial.println(": LCD found.");
    lcd_show = 1;
    lcd.begin(16, 2); // initialize the lcd

  } else {
    Serial.println(": LCD not found.");
  } // if
  if (lcd_show == 1) {
    lcd.setBacklight(255);
    lcd.home();
    lcd.clear();
    lcd.print("Designed by-");
    lcd.setCursor(0, 1);
    lcd.print("Your Name");
    lcd.noBlink();
    }
  delay(1000);

  // lcd function ends
  
// RTC setup function starts  
#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    rtc.adjust(DateTime(2020, 12, 02, 15, 25, 0));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

void loop () {

  if (lcd_show == 1) {
    lcd.setBacklight(255);
    lcd.home();
    lcd.clear();
    lcd.print("Hello LCD");
    lcd.noBlink();
    }
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    Serial.print("Temperature: ");
    Serial.print(rtc.getTemperature());
    Serial.println(" C");

    Serial.println();
    //LCD printing
    if (lcd_show == 1) {
      lcd.setBacklight(255);
      lcd.home();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Time - ");
      lcd.setCursor(7, 0);
      lcd.print(now.hour());
      lcd.print(":");
      if(now.minute()<10){
        lcd.print("0");
        }
      lcd.print(now.minute());
      lcd.print(":");
      lcd.print(now.second());

      lcd.setCursor(0, 1);
      lcd.print(now.day());
      lcd.print("-");
      lcd.print(now.month());
      lcd.print("-");
      lcd.print(now.year());
      lcd.setCursor(13, 1);
      lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
      
      lcd.noBlink();
      }
    delay(1000);
}

Troubleshooting:

1. RTC Library not Install

Solution:

On compiling/uploading the code you see the above error then install the library as explained at the beginning of the tutorial.

2. I2C LCD Library not install.

Solution:

On compiling/uploading the code you see the above error then install the library as explained at the beginning of the tutorial.

Video Tutorial: