hello ! guys Can you help me by providing code for ds3231 current date,time and wether. since i am tried but giving wrong date and time to i2c lcd module
Try the following sketch which shows output on Serial Monitor. You can add codes to show output on LCD.
#include "RTClib.h" //for DS3231 RTC contains Wire.h
#include<Wire.h>
byte prSec = 0;
RTC_DS3231 rtc; //user data type RTC_DS3231 variable rtc or object
DateTime nowDT;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup ()
{
Serial.begin(9600);
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//takes time of the day from PC
//rtc.adjust(DateTime(2021, 12, 31, 01, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
}
void loop ()
{
showTime();
waitOneSec();//1-sec interval of time display; it comes from DS3231 itself
}
void showTime()
{
nowDT = rtc.now(); //nowDT hold Date and Time
//---------------------------
byte myHour = nowDT.hour(); //myHour holds Hour of time of daya
if (myHour < 10)
{
Serial.print('0');
}
Serial.print(myHour); Serial.print(':'); //12:58:57 12:4:56
//-------------------------------------------------
byte myMin = nowDT.minute(); //to show leading zero of minute
if (myMin < 10)
{
Serial.print('0');
}
Serial.print(nowDT.minute()); Serial.print(':');
//--------------------------------------------------
byte mySec = nowDT.second();
if (mySec < 10)
{
Serial.print('0');
}
Serial.print(mySec);//(nowDT.second(), DEC);
//----------------------------------------- `
Serial.print(" ");
Serial.print(daysOfTheWeek[nowDT.dayOfTheWeek()]); Serial.print(' ');
byte myDay = nowDT.day();
if (myDay < 10)
{
Serial.print('0');
}
Serial.print(myDay); Serial.print(':');
//-----------------------------------------------------
byte myMonth = nowDT.month();
if (myMonth < 10)
{
Serial.print('0');
}
Serial.print(nowDT. month()); Serial.print(':');
Serial.println(nowDT.year());
}
//---------------------------------------------
void waitOneSec()
{
prSec = bcdSecond(); //current second of RTC
while (bcdSecond() == prSec)
{
;
}
}
byte bcdSecond()
{
nowDT = rtc.now();
return nowDT.second();
}
Output:
22:57:58 Wednesday 15:06:2022
22:57:59 Wednesday 15:06:2022
22:58:00 Wednesday 15:06:2022
22:58:01 Wednesday 15:06:2022
yeah it is correct .but i want to show current dete and time with that . can you help with these
along with weather in i2c lcd module. i can do this earlier.but problem with current data and time set
THIS IS MY CODE & I WANT TO TAKE DATE AND TIME FROM PC
#include <Wire.h>
#include "ds3231.h" // needs config.h, ds3231.cpp
#include <LiquidCrystal_I2C.h>
#define BUFF_MAX 128
//************************************************************************
// remove this block of commented lines - refers to non-I2C LCD
//************************************************************************
//---(Following are the PCF8574 pin assignments to LCD connections )----
// This are different than earlier/different I2C LCD displays
// #define Rs_pin 0
// #define Rw_pin 1
// #define En_pin 2
// #define BACKLIGHT_PIN 3
// #define D4_pin 4
// #define D5_pin 5
// #define D6_pin 6
// #define D7_pin 7
// #define LED_OFF 0
// #define LED_ON 1
// ---- -( Declare objects )---- - /
// this says I2C, but is using both I2C and ?other? pin protocol
// LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
//************************************************************************
//****Define I2C LCD Display *********************************
#define I2C_ADDR 0x27 // Define I2C Address for the PCF8574T
LiquidCrystal_I2C lcd(0x27, 16, 2); // USE THIS FOR I2C LCD
// END LCD DISPLAY *******************************************
uint8_t time[8]; // (1) redeclaration of "time" (2) never used
char recv[BUFF_MAX];
unsigned int recv_size = 0;
unsigned long prev, interval = 1000;
void setup()
{
Serial.begin(9600);
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(F(DATE), F(TIME)));//takes time of the day from PC
//rtc.adjust(DateTime(2021, 12, 31, 01, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
}
// DS3231_init(DS3231_INTCN); // archaic
DS3231_init(DS3231_CONTROL_INTCN);
memset(recv, 0, BUFF_MAX); // WHY?
Serial.println("GET time");
//LCD Setup******
lcd.begin (16, 2);
// initialize the lcd
lcd.backlight(); // this is the correct function call
// Switch on the backlight
// lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE); // not I2C LCD
// lcd.setBacklight(LED_ON); // not I2C LCD
//END LCD Setup*****
Serial.println("Setting time");
setTheTime("304022113122023"); // ssmmhhWDDMMYYYY set time once in the given format
}
void loop()
{
char tempF[6];
float temperature;
char buff[BUFF_MAX];
unsigned long now = millis();
struct ts t;
// show time once in a while
if (now - prev > interval) {
DS3231_get(&t); //Get time
temperature = DS3231_get_treg(); //Get temperature
dtostrf(temperature, 5, 1, tempF);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(t.mday);
printMonth(t.mon);
lcd.print(t.year);
lcd.setCursor(0, 1); //Go to second line of the LCD Screen
lcd.print(t.hour);
lcd.print(":");
if (t.min < 10)
{
lcd.print("0");
}
lcd.print(t.min);
lcd.print(":");
if (t.sec < 10)
{
lcd.print("0");
}
lcd.print(t.sec);
lcd.print(' ');
lcd.print(tempF);
lcd.print((char)223);
lcd.print("C ");
prev = now;
}
}
void setTheTime(char *cmd)
{
struct ts t;
// ssmmhhWDDMMYYYY set time
t.sec = inp2toi(cmd, 0);
t.min = inp2toi(cmd, 2);
t.hour = inp2toi(cmd, 4);
t.wday = inp2toi(cmd, 6);
t.mday = inp2toi(cmd, 7);
t.mon = inp2toi(cmd, 9);
t.year = inp2toi(cmd, 11) * 100 + inp2toi(cmd, 13);
DS3231_set(t);
Serial.println("OK");
}
void printMonth(int month)
{
switch (month)
{
case 1: lcd.print(" January "); break;
case 2: lcd.print(" February "); break;
case 3: lcd.print(" March "); break;
case 4: lcd.print(" April "); break;
case 5: lcd.print(" May "); break;
case 6: lcd.print(" June "); break;
case 7: lcd.print(" July "); break;
case 8: lcd.print(" August "); break;
case 9: lcd.print(" September "); break;
case 10: lcd.print(" October "); break;
case 11: lcd.print(" November "); break;
case 12: lcd.print(" December "); break;
default: lcd.print(" Error "); break;
}
}
You didn't like the help to the same question here?
Why not ask ChatGPT, like the responses you provided here?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.