Accéder au contenu principal
Publicités

How to simply use DS1302 RTC module with Arduino board and LCD screen

 

 

Hello, this is yassine,

In this tutorial we gonna use DS1302 Real Time Clock module with Arduino board, so this module can be set up via an Arduino code and then you can use it as you can to show time on 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.

For this you gonna need:

-RTC DS1302 module

-Arduino board, here I’m using Uno

-LCD i2c screen, optionnal if you want to visualize on your screen

-Some jump wires

-And 1k Resistor if you have the same problem as the video

 

Here is where you can download library and codes:

RTC Virtuabotix Library

LCD i2c Library

RTC codes I’ve used « .ino »

 

Wiring: 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 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 Surtrtech Youtube chhannel/Facebook page 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 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 Surtrtech Youtube chhannel/Facebook page 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);
}
Publicités

Yassine Tout afficher

Electronics, Automation engineering student... I try to share my little experience with other electronics amateur.

11 commentaires sur “How to simply use DS1302 RTC module with Arduino board and LCD screen Laisser un commentaire

  1. 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.

    J'aime

    • 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

      J'aime

Répondre

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google

Vous commentez à l'aide de votre compte Google. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s

%d blogueurs aiment cette page :