RTC module DS3231 - Timekeeping for Arduino
FunduinoPublished on
Datum und Uhrzeit mit DS1307 und DS3231 – auf dem LCD und im seriellen Monitor
You need this
1602 I2C LCD Module - 2x16 Characters, HD44780, Blue
3,02 € *Item no. FUN-1010568About the product — 1602 I2C LCD Module - 2x16 Characters, HD44780, Blue
DS1307 Tiny RTC module - I2C real-time clock, 5 V, backup battery
1,84 € *Item no. F23106775About the product — DS1307 Tiny RTC module - I2C real-time clock, 5 V, backup battery
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino
2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard cables male-female - 40 pieces, 20 cm
1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm
All 5 parts together 20,62 € *Ready to ship immediately · Delivery time: 1-3 business days
* All prices include statutory VAT, plus shipping costs.
What an RTC module is for
1 / 2
Build the circuit
1 / 3
Install the libraries and set the clock
Display time and date
Display the time and date on an LCD (DS1307)
#include <Wire.h>
#include <LiquidCrystal.h>
#include <time.h>
#include <DS1307RTC.h>
// Here, we specify which pins on the microcontroller board are used for the LCD.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
// In setup, we specify how many characters and lines are used.
lcd.begin(16, 2);
// Opens the serial port and sets the baud rate (9600) for transmission.
Serial.begin(9600);
// This function retrieves the time and date from the RTC.
setSyncProvider(RTC.get);
}
void loop()
{
// Serial.print displays something in the serial monitor:
// hour, minute, second, space, day, space, and so on.
Serial.print(hour());
// For minutes and seconds, printDigits is used,
// which is defined at the end of the sketch.
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
delay(1000); // wait one second
// setCursor specifies where the text should begin:
// here, at the third character in the first line.
lcd.setCursor(2, 0);
lcd.print(hour());
lcd.print(":");
lcd.print(minute());
lcd.print(":");
lcd.print(second());
lcd.print(" ");
// The word "o'clock" appears after the time. This is followed by three spaces;
// otherwise, an extra "r" would remain after "o'clock" for numbers below 10: the
// LCD library has no command that adds a leading 0 to numbers below 10.
lcd.print("o'clock");
lcd.print(" ");
lcd.print(" ");
lcd.print(" ");
// The next text begins at the second character in the second line.
lcd.setCursor(1, 1);
lcd.print(day()); // The date is displayed as
lcd.print("."); // "day"."month"."year".
lcd.print(month());
lcd.print(".");
lcd.print(year());
}
// Here, we specify that numbers below 10 are automatically preceded by a 0
// in the serial monitor. This applies only to the serial monitor, not the LCD.
void printDigits(int digits)
{
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}Extension: Display weekday
Extension: Display weekday
// Hier wird für den vorher im Sketch verwendeten Befehl printDay festgelegt, was er tut.
void printDay()
{
int wochentag;
wochentag = weekday(); // Der Wochentag ergibt sich aus dem Datum.
if (wochentag == 1) { lcd.print("So, "); } // Tag 1 ist Sonntag,
if (wochentag == 2) { lcd.print("Mo, "); } // Tag 2 ist Montag und so weiter.
if (wochentag == 3) { lcd.print("Di, "); }
if (wochentag == 4) { lcd.print("Mi, "); }
if (wochentag == 5) { lcd.print("Do, "); }
if (wochentag == 6) { lcd.print("Fr, "); }
if (wochentag == 7) { lcd.print("Sa, "); }
}Two I²C components at once: Change the LCD address
1 / 2
Display the time on an I²C LCD
Displaying the time and date on an I²C LCD (DS1307)
// Load libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <time.h>
#include <DS1307RTC.h>
// Name the I²C display and enter its HEX address (here 0x3D).
LiquidCrystal_I2C lcd(0x3D, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
lcd.begin(16, 2); // Start the display: 16 characters in 2 lines
lcd.backlight(); // Turn on the display backlight
Serial.begin(9600); // Start the serial connection at a baud rate of 9600
setSyncProvider(RTC.get); // Retrieve data from the RTC
}
void loop()
{
// Output in the serial monitor:
// Hour, minute, second, space, day, space, and so on.
Serial.print(hour());
// For minutes and seconds, printDigits is used,
// which is defined at the end of the sketch.
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
delay(1000); // wait one second
// setCursor specifies where the text should begin:
// here, at the third character in the first line.
lcd.setCursor(2, 0);
lcd.print(hour()); // The time appears in the format
lcd.print(":"); // hours:minutes:seconds.
lcd.print(minute());
lcd.print(":");
lcd.print(second());
lcd.print(" ");
lcd.print("Clock"); // The word "Clock" appears after it.
lcd.print(" ");
lcd.print(" ");
lcd.print(" ");
// The date appears on the second line.
lcd.setCursor(1, 1);
lcd.print(day());
lcd.print(".");
lcd.print(month());
lcd.print(".");
lcd.print(year());
}
// The printDigits command for the serial monitor
void printDigits(int digits)
{
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}The DS3231: also measure the temperature
1 / 2
DS3231: Date, time, and temperature in the serial monitor
// Include the required library
#include <RTClib.h>
// Name of the RTC module (rtc)
RTC_DS3231 rtc;
void setup()
{
// Start the RTC module
rtc.begin();
Serial.begin(9600);
/*
If the date and time are incorrect: set the date and time.
Order: year, month, day, hour, minute, second
Example: August 31, 2022, 10 hours, 31 minutes, 30 seconds
rtc.adjust(DateTime(2022, 8, 31, 10, 31, 30));
*/
}
void loop()
{
// rtc.now() gets the current time
DateTime aktuell = rtc.now();
/*
Set the date format
DD -> day with leading 0
MM -> month with leading 0
YYYY -> full year
*/
char Datum[] = "DD.MM.YYYY ";
// Convert the date to a string and display it
Serial.print(aktuell.toString(Datum));
// Set the time format
char Zeit[] = "hh:mm:ss";
// Convert the time to a string and display it
Serial.print(aktuell.toString(Zeit));
// Get the temperature
String Temperatur = String(rtc.getTemperature());
// The value uses the Anglo-Saxon number format:
// replace the period with a comma
Temperatur.replace(".", ",");
Serial.println(" Temperature: " + Temperatur + "°C");
// Pause for 1 second
delay(1000);
}Rate the instructions