How to simply use DS1302 RTC module with Arduino board and LCD screen
Hello there,
In this tutorial we gonna use DS1302 Real Time Clock module with Arduino board. This module can be set up via an Arduino code and then you can use it as you want, for example to show time/date a screen or to log some sensor values by time or setting sleep and wake modes, similar modules are used in computers, phones, TVs…. it always has a battery cell that can last long enough so even if the device is not powered the time keeps updating.
Here are some projects I did with this module:
- How to setup DS1302 RTC module with Keypad + Arduino + LCD
- Simple Arduino based Alarm Clock with DS1302 RTC
Or some similar modules:
- How to use DS1307 RTC with Arduino and LCD/OLED
- How to simply use DS3231 RTC module with Arduino + OLED display
For this we gonna need:
- RTC DS1302 module.
- Arduino board, here I’m using Uno.
- LCD i2c screen, optionnal if you want to visualize date/time on your screen. If you don’t know how to use one check here.
- Some jump wires.
- And 1k Resistor if you have the same problem as I had (check the video).
Libraries
Here are the libraries you’ll need for this, download them here:
Codes
Here I gathered all codes I used, you can download them here: RTC codes I’ve used “.ino”, or check below and copy them.
Wiring 1: Setup the module from the IDE and show the time on the Serial monitor

Code 1 : Setting the time and date of the module and showing it on the serial monitor
This code will permit you to set the DS1302 and then display date and time on the serial monitor.
Please read carefully the code below, the line
1 |
myRTC.setDS1302Time(15, 22, 21, 7, 14, 1, 2018); |
Permits you to set the module on the given values (numbers typed there), you have to set it up to your current date/time and then comment it
1 |
//myRTC.setDS1302Time(15, 22, 21, 7, 14, 1, 2018); |
And reupload the code again to the Arduino with removed or commented (//) setDS1302Time function, so when you restart your Arduino the program doesn’t execute the function again.
Code 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
//This code is to use with DS1302 RTC module, it permits you to setup the actual time and date //And you can visualize them on the serial monitor //This code is a modified version of the code provided in virtuabotixRTC library //Refer to https://Surtrtech.com for more information #include <virtuabotixRTC.h> //Library used //Wiring SCLK -> 6, I/O -> 7, CE -> 8 //Or CLK -> 6 , DAT -> 7, Reset -> 8 virtuabotixRTC myRTC(6, 7, 8); //If you change the wiring change the pins here also void setup() { Serial.begin(9600); // Set the current date, and time in the following format: // seconds, minutes, hours, day of the week, day of the month, month, year myRTC.setDS1302Time(15, 22, 21, 7, 14, 1, 2018); //Here you write your actual time/date as shown above //but remember to "comment/remove" this function once you're done //The setup is done only one time and the module will continue counting it automatically } void loop() { // This allows for the update of variables for time or accessing the individual elements. myRTC.updateTime(); // Start printing elements as individuals Serial.print("Current Date / Time: "); Serial.print(myRTC.dayofmonth); //You can switch between day and month if you're using American system Serial.print("/"); Serial.print(myRTC.month); Serial.print("/"); Serial.print(myRTC.year); Serial.print(" "); Serial.print(myRTC.hours); Serial.print(":"); Serial.print(myRTC.minutes); Serial.print(":"); Serial.println(myRTC.seconds); // Delay so the program doesn't print non-stop delay(1000); } |
Wiring 2: Add LCD i2c Screen and solving the grounding problem if you encounter it

Code 2: Showing the Date/Time on a LCD i2c screen
This code uses the LCD i²c screen, optional of course, be careful again with “SetDS1302Time” function again (read above).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
//This code is to use with DS1302 RTC module, it permits you to setup the actual time and date //And you can visualize them on the LCD i2c screen //This code is a modified version of the code provided in virtuabotixRTC library //Refer to https://Surtrtech.com for more information #include <virtuabotixRTC.h> //Libraries needed #include <LiquidCrystal_I2C.h> #define I2C_ADDR 0x27 //LCD i2c stuff #define BACKLIGHT_PIN 3 #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); //Wiring SCLK -> 6, I/O -> 7, CE -> 8 //Or CLK -> 6 , DAT -> 7, Reset -> 8 virtuabotixRTC myRTC(6, 7, 8); //If you change the wiring change the pins here also void setup() { Serial.begin(9600); lcd.begin (16,2); //Initialize the LCD lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); lcd.home (); myRTC.setDS1302Time(15, 31, 15, 5, 12, 1, 2018); //Here you write your actual time/date as shown above //but remember to "comment/remove" this function once you're done as I did //The setup is done only one time and the module will continue counting it automatically } void loop() { lcd.clear(); //Here after clearing the LCD we take the time from the module and print it on the screen with usual LCD functions myRTC.updateTime(); lcd.setCursor(0,0); lcd.print(myRTC.dayofmonth); lcd.print("/"); lcd.print(myRTC.month); lcd.print("/"); lcd.print(myRTC.year); lcd.setCursor(0,1); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes); lcd.print(":"); lcd.print(myRTC.seconds); delay(1000); } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
Amazing! Can I use that in my Arduino project?
Thanks!
Sure
Hello sir, Thankyou for the information.
Can you please explain the 7 in the brackets myRTC.setDS1302Time(15, 22, 21, 7, 14, 1, 2018);
It means “Day of the week” (Mon, Tue….) You can associate that number with its day name and display it too
Arduino:1.8.3 (Windows XP), Tarjeta:”Arduino/Genuino Uno”
REMOTOvv:19: error: no matching function for call to ‘LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int, int, int, int, int, int)’
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
^
C:\Documents and Settings\Administrador\Escritorio\REMOTOvv\REMOTOvv.ino:19:80: note: candidates are:
In file included from C:\Documents and Settings\Administrador\Escritorio\REMOTOvv\REMOTOvv.ino:7:0:
C:\Documents and Settings\Administrador\Mis documentos\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:57:3: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t)
LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows);
^
C:\Documents and Settings\Administrador\Mis documentos\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:57:3: note: candidate expects 3 arguments, 8 provided
C:\Documents and Settings\Administrador\Mis documentos\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:55:7: note: constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(const LiquidCrystal_I2C&)
class LiquidCrystal_I2C : public Print {
^
C:\Documents and Settings\Administrador\Mis documentos\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:55:7: note: candidate expects 1 argument, 8 provided
C:\Documents and Settings\Administrador\Mis documentos\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:55:7: note: constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(LiquidCrystal_I2C&&)
C:\Documents and Settings\Administrador\Mis documentos\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:55:7: note: candidate expects 1 argument, 8 provided
C:\Documents and Settings\Administrador\Escritorio\REMOTOvv\REMOTOvv.ino: In function ‘void setup()’:
REMOTOvv:31: error: ‘class LiquidCrystal_I2C’ has no member named ‘setBacklightPin’
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
^
REMOTOvv:31: error: ‘POSITIVE’ was not declared in this scope
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
^
exit status 1
no matching function for call to ‘LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int, int, int, int, int, int)’
Este reporte podría tener más información con
“Mostrar salida detallada durante la compilación”
opción habilitada en Archivo -> Preferencias.
Hello, this is a library issue, you have a library with the same name but when you call a function it can’t be found, so you either adapt the code to you existing library or remove it and use the library I provided
Hi. I followed your solution by adding a 1k resistor in series with DAT and Arduino’s input pin, but the problem persistrs. What can I do now? It’s Waveshare’s DS1302 RTC board. It prints crappy time and after 2-3 seconds it stops printing in Serial monitor.
Hello, try without the resistor, or a lower value one… make sure that the pins are soldred well, also check the other components, sometimes a badly soldred smd can cause those problems.
I tried all of these, I even bought another DS1302 RTC board to check if the previous one had a problem, but it has the same behaviour. I tried on another Arduino, I tried changing the jumper wires but nothing. It prints weird numbers, sometimes characters, and 2-4 seconds later it stops printing on my Serial Monitor. I even had almost the same numbers as you in your video, but the 1k (and other values) resistor didn’t solve the problem.
My wiring is like this:
Vcc -> 5v
GND -> GND
I/O (MISO) -> Pin 8
SCLK -> Pin 7
CE (CS) -> Pin 9
Initialization (on the code): virtuabotixRTC myRTC(7,8,9);
I really can’t determine the problem here, do you have other modules? things wired with the Arduino UNO?
I just realized that even a simple Serial.println(“test”) doesn’t work either! It turns out that my usb cable is crappy. Problem solved!
What is the grounding problem?
why did you include the lcd pins if it is using i2c already? im confused
Yes it’s an i²c, but it’s just an adapter soldered at the pins of a standard LCD, and the function used to declare the LCD needs those pins numbers.
Fantastic, Thank you.
what do i need to do if i only want to show the time of the day on a 4 digit 7segment display?
Hello, I am unable to download any archive from Google Drive?? Why is it impssible?? I can not realise the project without those archive. Please help.
Thx
How can i use in my scale weighing indicator?
23:18:27.509 -> Current Date / Time: 45/25/2165 45:85:85
23:18:28.499 -> Current Date / Time: 45/25/2165 45:85:85
23:18:29.497 -> Current Date / Time: 45/25/2165 45:85:85
23:18:30.505 -> Current Date / Time: 45/25/2165 45:85:85
23:18:31.507 -> Current Date / Time: 45/25/2165 45:85:85
23:18:32.493 -> Current Date / Time: 45/25/2165 45:85:85
23:18:33.489 -> Current Date / Time: 45/25/2165 45:85:85
23:18:34.519 -> Current Date / Time: 45/25/2165 45:85:85
23:18:35.511 -> Current Date / Time: 45/25/2165 45:85:85
23:18:36.511 -> Current Date / Time: 45/25/2165 45:85:85
23:18:37.505 -> Current Date / Time: 45/25/2165 45:85:85
23:18:38.534 -> Current Date / Time: 45/25/2165 45:85:85
23:18:39.525 -> Current Date / Time: 45/25/2165 45:85:85
23:18:40.535 -> Current Date / Time: 45/25/2165 45:85:85
I am getting the the time in this format in this form even after i set the time,I am new to programming pls help,I am using ds1302 and aurdino uno .
Arduino: 1.8.12 (Mac OS X), Board: “Arduino Uno”
test_2:19:80: error: no matching function for call to ‘LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int, int, int, int, int, int)’
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
^
In file included from /Users/raju/Documents/Arduino/test_2/test_2.ino:7:0:
/Users/raju/Documents/Arduino/libraries/LiquidCrystal_I2C-1.1.2/LiquidCrystal_I2C.h:57:3: note: candidate: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t)
LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows);
^~~~~~~~~~~~~~~~~
/Users/raju/Documents/Arduino/libraries/LiquidCrystal_I2C-1.1.2/LiquidCrystal_I2C.h:57:3: note: candidate expects 3 arguments, 8 provided
/Users/raju/Documents/Arduino/libraries/LiquidCrystal_I2C-1.1.2/LiquidCrystal_I2C.h:55:7: note: candidate: constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(const LiquidCrystal_I2C&)
class LiquidCrystal_I2C : public Print {
^~~~~~~~~~~~~~~~~
/Users/raju/Documents/Arduino/libraries/LiquidCrystal_I2C-1.1.2/LiquidCrystal_I2C.h:55:7: note: candidate expects 1 argument, 8 provided
/Users/raju/Documents/Arduino/libraries/LiquidCrystal_I2C-1.1.2/LiquidCrystal_I2C.h:55:7: note: candidate: constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(LiquidCrystal_I2C&&)
/Users/raju/Documents/Arduino/libraries/LiquidCrystal_I2C-1.1.2/LiquidCrystal_I2C.h:55:7: note: candidate expects 1 argument, 8 provided
/Users/raju/Documents/Arduino/test_2/test_2.ino: In function ‘void setup()’:
test_2:31:6: error: ‘class LiquidCrystal_I2C’ has no member named ‘setBacklightPin’; did you mean ‘setBacklight’?
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
^~~~~~~~~~~~~~~
setBacklight
test_2:31:36: error: ‘POSITIVE’ was not declared in this scope
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
^~~~~~~~
exit status 1
no matching function for call to ‘LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int, int, int, int, int, int)’
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
I am getting this error any suggestion please let me know
Hi, I even after using a 1kOhm I get 45/25/2165 as a date and an invalid time. Is there something else to help ?
Hi..
Thanks to this project and codes.
I make it and please let me know how to print day of week on lcd?
C:\Users\RAPID BOOTS\AppData\Local\Temp\Rar$DIa15064.15933\RTC_DS1302_LCDi2c\RTC_DS1302_LCDi2c.ino:19:80: error: no matching function for call to ‘LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int, int, int, int, int, int)’
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
^
In file included from C:\Users\RAPID BOOTS\AppData\Local\Temp\Rar$DIa15064.15933\RTC_DS1302_LCDi2c\RTC_DS1302_LCDi2c.ino:7:0:
C:\Users\RAPID BOOTS\Documents\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:57:3: note: candidate: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t)
LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows);
^~~~~~~~~~~~~~~~~
C:\Users\RAPID BOOTS\Documents\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:57:3: note: candidate expects 3 arguments, 8 provided
C:\Users\RAPID BOOTS\Documents\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:55:7: note: candidate: constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(const LiquidCrystal_I2C&)
class LiquidCrystal_I2C : public Print {
^~~~~~~~~~~~~~~~~
C:\Users\RAPID BOOTS\Documents\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:55:7: note: candidate expects 1 argument, 8 provided
C:\Users\RAPID BOOTS\Documents\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:55:7: note: candidate: constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(LiquidCrystal_I2C&&)
C:\Users\RAPID BOOTS\Documents\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:55:7: note: candidate expects 1 argument, 8 provided
C:\Users\RAPID BOOTS\AppData\Local\Temp\Rar$DIa15064.15933\RTC_DS1302_LCDi2c\RTC_DS1302_LCDi2c.ino: In function ‘void setup()’:
C:\Users\RAPID BOOTS\AppData\Local\Temp\Rar$DIa15064.15933\RTC_DS1302_LCDi2c\RTC_DS1302_LCDi2c.ino:29:7: error: ‘class LiquidCrystal_I2C’ has no member named ‘setBacklightPin’; did you mean ‘setBacklight’?
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
^~~~~~~~~~~~~~~
setBacklight
C:\Users\RAPID BOOTS\AppData\Local\Temp\Rar$DIa15064.15933\RTC_DS1302_LCDi2c\RTC_DS1302_LCDi2c.ino:29:37: error: ‘POSITIVE’ was not declared in this scope
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
^~~~~~~~
Multiple libraries were found for “LiquidCrystal_I2C.h”
Used: C:\Users\RAPID BOOTS\Documents\Arduino\libraries\LiquidCrystal_I2C
Not used: C:\Users\RAPID BOOTS\Documents\Arduino\libraries\Newliquidcrystal_1.3.5
exit status 1
Compilation error: no matching function for call to ‘LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int, int, int, int, int, int)’