Arduino Real Time Clock with Alarm and Temperature Monitor

Last time I’ve built a simple real time clock and calendar using Arduino UNO board and DS3231 and now I’m going to add two alarm functions and temperature monitor to the previous project. Related topic URL is the one below:
Arduino and DS3231 real time clock

The DS3231 RTC has a built-in two alarm functions and a temperature sensor with a resolution of 0.25 and an accuracy of ±3°C which make this project more easier.

Hardware Required:

  • Arduino UNO or compatible board   —> ATmega328P datasheet
  • DS3231 RTC board   —> DS3231 datasheet
  • 2004 LCD screen
  • 3 x push button
  • LED
  • 10K ohm variable resistor (or potentiometer)
  • 2 x 330 ohm resistor
  • 3V coin cell battery
  • Breadboard
  • Jumper wires

The circuit:
Project circuit schematic diagram is shown below.

Arduino DS3231 real time clock set buttons alarm temperature

In this project I used the DS3231 board, this board basically contains the main chip which is the DS3231, two pull-up resistors (4.7K) of SCL, SDA and INT/SQW lines and coin cell battery holder. There is also 24C32 EEPROM and some other resistors (not used in this project).
The DS3231 board is supplied with 5V as the 2004 LCD, this 5V comes from the Arduino board, there are 3 data lined connected between this board and the Arduino, SCL line is connected to analog pin 5, SDA is connected to analog pin 4 and INT line is connected to digital pin 2 which is the external interrupt pin of the Arduino. The DS3231 interrupts the microcontroller when there is an alarm (alarm1 or alarm2).
In the circuit there are 3 push buttons: B1, B2 and B3. These buttons are used to set time, calendar and alarms. Time and calendar can be adjusted with B1 and B2, button B1 selects time or date parameter (time parameters: hours and minutes; calendar parameters: day of the week, date, month and year) and B2 increments the selected parameter. Buttons B3 and B2 adjust alarm1 and alarm2 parameters (hours, minutes and ON/OFF), button B3 selects the parameter and B2 increments the selected parameter.

Also, there is an LED connected to Arduino pin 12, this LED is used as an alarm indicator (alarm1 or alarm2), so if there is an alarm the DS3231 pulls down the INT pin which interrupts the microcontroller and the microcontroller turns the LED ON, here button B2 turns both the LED and the occurred alarm OFF.

Arduino Code:
The Arduino code below doesn’t use any library for the DS3231.
By reading the datasheet of the DS3231 RTC the code will be more easier!

Programming hints:
The DS3231 works with BCD format only (except the temperature) and to convert the BCD to decimal and vise versa I used the following commands (example for minute variable):
minute = (minute >> 4) * 10 + (minute & 0x0F);                                  // Convert BCD to decimal
minute = ((minute / 10) << 4) + (minute % 10);                                     // Convert decimal to BCD

Code functions:
void DS3231_read() : this function reads time and calendar data from the DS3231 (seconds, minutes, hours, day, date, month and year).

void DS3231_display() : displays time and calendar data, before displaying time and calendar data are converted from BCD format to decimal format. This function displays the calendar by calling a function named void calendar_display() .

void alarms_read_display() : basically this functions reads alarm1 and alarm2 minutes and hours. It also reads the DS3231 control register, status register and temperature registers (2 registers).
The other job of this function is to display alarms data (hours, minutes and status) and the temperature value. The alarm status are extracted from the control register.

byte edit(byte x, byte y, byte parameter) : I used this function to edit time, calendar and alarm parameters except the day. I used a variable named i to distinguish between the parameters:
i = 0, 1 : time hours and minutes respectively
i = 2, 3, 4: calendar date, month and year respectively
i = 5, 6: alarms hours and minutes respectively
i = 7: alarm status (ON or OFF)
After the edit of time/calendar/alarms the data have to be converted back to BCD format and written to the DS3231.

The Arduino turns the LED ON when it interrupted by the DS3231, the DS3231 sends the interrupt signal (pulls down the INT line) when there has been an alarm. Button B2 resets and turns OFF the alarm. If both alarms are active, button B2 will resets and turns OFF the occurred alarm only and keeps the other as it is. To do that we’ve to detect which alarm has been occurred which can be easily done by reading the status register of the DS3231 (A1IF and A2IF flag bits). Turning ON or OFF an alarm is done by writing to the control register (bits: INTCN, A1IE and A2IE). Always INTCN bit should be 1. I used the following line to write 1 to the INTCN bit and to turn OFF the occurred alarm:
Wire.write(4 | (!bit_test(status_reg, 0) & alarm1_status) | ((!bit_test(status_reg, 1) & alarm2_status) << 1));
alarm1_status and alarm2_status are boolean variables (can be true or false), for example if alarm1_status is 1 ==> alarm1 is ON and if alarm1_status is 0 ==> alarm1 is OFF. The same thing for alarm2.
The complete Arduino code is below.

Small Video:
The video below shows a simple hardware circuit of the project.

28 thoughts on “Arduino Real Time Clock with Alarm and Temperature Monitor”

  1. Hi SP, is there a need to use an interrupt? Can I connect the SQW from the RTC to a random digital pin (and adjust the code accordingly to set a flag)?

  2. please help me i wnat to replace the led with a buzzer but i need a photograph and the code again so please send it to me because i m new

  3. Is there a Proteus simulation file for this project, like the one shared with the: Arduino and DS3231 real time clock ?

  4. I’m working on smth similar but i cant find all of the parts for proteus. It is possible for you guys to share project document so i can work on the simulation? Also which software do you guys use ?

  5. Hi, I have one task. Can you help me?
    The task is as follows:
    I need to include at a specific hour and minute PWM that unfolds from 0-100% in 20 minutes (smooth). Stay 100% of the time spent and then return to 0% again in 20 minutes (smooth). This should all be in line with the actual date and time. Can Alarm 2 turn off Alarm 1. I hope you help me. Now I’m learning to work with Adruino. It’s a little complicated for me, but I hope it won’t be a problem for you.
    thanks

  6. Hi, can you help me? what mistake I’ve made, the led alarm is on, the button and the LCD are working properly.
    I use pin 8 for the led alarm, I have adjusted the coding.

    1. Simple Projects

      No you can’t because the DS1307 doesn’t have hardware alarm functions and also the built-in temperature monitor.

  7. I do not understand directly, because there are 2 temperatures (temperature_lsb; temperature_msb) do they do some type of media? Could you explain more?

    If you can help me with some modifications the program would be very grateful!

  8. can I change the first alarm to be the start time and the second one to be the stop time if I want to use it to oparit fan that work with relay

  9. hi. what about if i wanna make an alarm once a day in a week? example like i just want to set on the alarm on monday, 6 p.m. could you tell me how to make it?

    1. Yes you can add it. Buzzer works with PWM signal which means you’ve to replace digitalWrite(alarm_pin, HIGH); (located inside void Alarm) with your PWM code. Do the same thing to switch the buzzer off.
      Generally buzzers work with a specified frequency (can be found in buzzer datasheet), configure your Arduino to generate a PWM signal appropriate to your buzzer.

  10. Bonsoir, par hasard j’ai trouvé ce code formidable, je l’ai un peu modifier pour mon usage, supprimer une alarme et séparer la date du jour, avec un lcd 16×4, voici le code.

    /* Arduino real time clock and calendar with 2 alarm functions and temperature monitor using DS3231
    Read DS3231 RTC datasheet to understand the code
    Time & date parameters can be set using two push buttons connected to pins 9 (B1) & 10 (B2).
    Alarm1 and alarm2 can be set using two push buttons connected to 11 (B3) & 10 (B2).
    Pin 12 becomes high when alarm occurred and button B2 returns it to low and
    turns the occurred alarm OFF.
    DS3231 interrupt pin is connected to Arduino external interrupt pin 2.
    http://simple-circuit.com/arduino-ds3231-real-time-clock-alarm-temperature/
    */

    // include LCD library code
    #include
    // include Wire library code (needed for I2C protocol devices)
    #include

    // LCD module connections (RS, E, D4, D5, D6, D7)
    LiquidCrystal lcd(8,9,10,11,12,13);//pins(rs, enable, d4, d5, d6, d7)

    const int button1 = 4; // bouton mise a l’heure,jour,date
    const int button2 = 5; // stop alarme
    const int button3 = 6; // bouton mise a l’heure alarme
    const int alarm_pin = 7; // Alarme pin numero

    void setup() {
    pinMode(4, INPUT_PULLUP);
    pinMode(5, INPUT_PULLUP);
    pinMode(6, INPUT_PULLUP);
    pinMode(7, OUTPUT);
    digitalWrite(alarm_pin, LOW);

    lcd.begin(16, 4);
    Wire.begin();
    attachInterrupt(digitalPinToInterrupt(2), Alarm, FALLING); //sortie SQW du DS 3231
    }

    // Variables declaration
    bool alarm1_status, alarm2_status;
    char Time[] = ” : : “,
    calendar[] = ” “,
    calend[] = ” / /20 “,
    alarm1[] = “Al: : :00”;

    byte i, second, minute, hour, day, date, month, year,
    alarm1_minute, alarm1_hour,
    status_reg;

    void Alarm(){
    digitalWrite(alarm_pin, HIGH);
    }
    void DS3231_read(){ // Function to read time & calendar data
    Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address
    Wire.write(0); // Send register address
    Wire.endTransmission(false); // I2C restart
    Wire.requestFrom(0x68, 7); // Request 7 bytes from DS3231 and release I2C bus at end of reading
    second = Wire.read(); // Read seconds from register 0
    minute = Wire.read(); // Read minuts from register 1
    hour = Wire.read(); // Read hour from register 2
    day = Wire.read(); // Read day from register 3
    date = Wire.read(); // Read date from register 4
    month = Wire.read(); // Read month from register 5
    year = Wire.read(); // Read year from register 6
    }
    void alarms_read_display(){ // Function to read and display alarm1, alarm2 and temperature data
    byte control_reg;

    Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address
    Wire.write(0x08); // Send register address
    Wire.endTransmission(false); // I2C restart
    Wire.requestFrom(0x68, 11); // Request 11 bytes from DS3231 and release I2C bus at end of reading
    alarm1_minute = Wire.read(); // Read alarm1 minutes
    alarm1_hour = Wire.read(); // Read alarm1 hours
    Wire.read(); // Skip alarm1 day/date register
    // Read alarm2 hours
    Wire.read(); // Skip alarm2 day/date register
    control_reg = Wire.read(); // Read the DS3231 control register
    status_reg = Wire.read(); // Read the DS3231 status register
    Wire.read(); // Skip aging offset register

    // Convert BCD to decimal
    alarm1_minute = (alarm1_minute >> 4) * 10 + (alarm1_minute & 0x0F);
    alarm1_hour = (alarm1_hour >> 4) * 10 + (alarm1_hour & 0x0F);

    // End conversion
    alarm1[8] = alarm1_minute % 10 + 48;
    alarm1[7] = alarm1_minute / 10 + 48;
    alarm1[5] = alarm1_hour % 10 + 48;
    alarm1[4] = alarm1_hour / 10 + 48;

    alarm1_status = bitRead(control_reg, 0); // Read alarm1 interrupt enable bit (A1IE) from DS3231 control register
    // Read alarm2 interrupt enable bit (A2IE) from DS3231 control register

    lcd.setCursor(0, 3);
    lcd.print(alarm1); // Display alarm1
    lcd.setCursor(13, 3);
    if(alarm1_status) lcd.print(“ON “); // If A1IE = 1 print ‘ON’
    else lcd.print(“OFF”); // If A1IE = 0 print ‘OFF’

    }
    void calendar_display(){ // Function to display calendar
    switch(day){
    case 1: strcpy(calendar, “Dimanche “); break;
    case 2: strcpy(calendar, “Lundi “); break;
    case 3: strcpy(calendar, “Mardi “); break;
    case 4: strcpy(calendar, “Mercredi “); break;
    case 5: strcpy(calendar, “Jeudi “); break;
    case 6: strcpy(calendar, “Vendredi “); break;
    case 7: strcpy(calendar, “Samedi “); break;
    default: strcpy(calendar, “Samedi “);
    }

    lcd.setCursor(0, 1);
    lcd.print(calendar); // Display calendar
    }
    void calend_display(){
    calend[13] = year % 10 + 48;
    calend[12] = year / 10 + 48;
    calend[8] = month % 10 + 48;
    calend[7] = month / 10 + 48;
    calend[5] = date % 10 + 48;
    calend[4] = date / 10 + 48;
    lcd.setCursor(0, 2);
    lcd.print(calend); // Display calendar
    }

    void DS3231_display(){
    // Convert BCD to decimal
    second = (second >> 4) * 10 + (second & 0x0F);
    minute = (minute >> 4) * 10 + (minute & 0x0F);
    hour = (hour >> 4) * 10 + (hour & 0x0F);
    date = (date >> 4) * 10 + (date & 0x0F);
    month = (month >> 4) * 10 + (month & 0x0F);
    year = (year >> 4) * 10 + (year & 0x0F);
    // End conversion
    Time[7] = second % 10 + 48;
    Time[6] = second / 10 + 48;
    Time[4] = minute % 10 + 48;
    Time[3] = minute / 10 + 48;
    Time[1] = hour % 10 + 48;
    Time[0] = hour / 10 + 48;
    calendar_display(); // Call calendar display function
    calend_display();
    lcd.setCursor(4, 0);
    lcd.print(Time); // Display time
    }
    void Blink(){
    byte j = 0;
    while(j = 5) && digitalRead(button2) && (digitalRead(button3) || i 23) // If hours > 23 ==> hours = 0
    parameter = 0;
    if(((i == 1) || (i == 6)) && parameter > 59) // If minutes > 59 ==> minutes = 0
    parameter = 0;
    if(i == 2 && parameter > 31) // If date > 31 ==> date = 1
    parameter = 1;
    if(i == 3 && parameter > 12) // If month > 12 ==> month = 1
    parameter = 1;
    if(i == 4 && parameter > 99) // If year > 99 ==> year = 0
    parameter = 0;
    if(i == 7 && parameter > 1) // For alarms ON or OFF (1: alarm ON, 0: alarm OFF)
    parameter = 0;
    lcd.setCursor(x, y);
    if(i == 7){ // For alarms ON & OFF
    if(parameter == 1) lcd.print(“ON”);
    else lcd.print(“OFF”);
    }
    else{
    sprintf(text,”%02u”, parameter);
    lcd.print(text);
    }
    if(i >= 5){
    DS3231_read(); // Read data from DS3231
    DS3231_display(); // Display DS3231 time and calendar
    }
    delay(200); // Wait 200ms
    }
    lcd.setCursor(x, y);
    lcd.print(” “); // Print two spaces
    if(i == 7) lcd.print(” “); // Print space (for alarms ON & OFF)
    Blink(); // Call Blink function
    lcd.setCursor(x, y);
    if(i == 7){ // For alarms ON & OFF
    if(parameter == 1) lcd.print(“ON “);
    else lcd.print(“OFF”);
    }
    else{
    sprintf(text,”%02u”, parameter);
    lcd.print(text);
    }
    Blink();
    if(i >= 5){
    DS3231_read();
    DS3231_display();}
    if((!digitalRead(button1) && i = 5)){
    i++; // Increment ‘i’ for the next parameter
    return parameter; // Return parameter value and exit
    }
    }
    }

    void loop() {
    if(!digitalRead(button1)){ // If B1 button is pressed
    i = 0;
    hour = edit(4, 0, hour);
    minute = edit(7, 0, minute);
    while(!digitalRead(button1)); // Wait until button B1 released
    while(true){
    while(!digitalRead(button2)){ // If button B2 button is pressed
    day++; // Increment day
    if(day > 7) day = 1;
    calendar_display(); // Call display_calendar function
    calend_display();
    lcd.setCursor(0, 1);
    lcd.print(calendar); // Display calendar
    lcd.setCursor(0, 2);
    lcd.print(calend);

    delay(200);
    }
    lcd.setCursor(0, 1);
    lcd.print(” “); // Print 3 spaces
    lcd.setCursor(0, 2);
    lcd.print(” “);

    Blink();
    lcd.setCursor(0, 1);
    lcd.print(calendar); // Print calendar
    lcd.setCursor(0, 2);
    lcd.print(calend);

    Blink(); // Call Blink function
    if(!digitalRead(button1)) // If button B1 is pressed
    break;
    }
    date = edit(4, 2, date); // Edit date
    month = edit(7, 2, month); // Edit month
    year = edit(12, 2, year); // Edit year
    // Convert decimal to BCD
    minute = ((minute / 10) << 4) + (minute % 10);
    hour = ((hour / 10) << 4) + (hour % 10);
    date = ((date / 10) << 4) + (date % 10);
    month = ((month / 10) << 4) + (month % 10);
    year = ((year / 10) << 4) + (year % 10);
    // End conversion
    // Write time & calendar data to DS3231 RTC
    Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address
    Wire.write(0); // Send register address
    Wire.write(0); // Reset sesonds and start oscillator
    Wire.write(minute); // Write minute
    Wire.write(hour); // Write hour
    Wire.write(day); // Write day
    Wire.write(date); // Write date
    Wire.write(month); // Write month
    Wire.write(year); // Write year
    Wire.endTransmission(); // Stop transmission and release the I2C bus
    delay(200);
    }
    if(!digitalRead(button3)){ // If B3 button is pressed
    while(!digitalRead(button3)); // Wait until button B3 released
    i = 5;
    alarm1_hour = edit(4, 3, alarm1_hour);
    alarm1_minute = edit(7, 3, alarm1_minute);
    alarm1_status = edit(13, 3, alarm1_status);
    i = 5;

    alarm1_minute = ((alarm1_minute / 10) << 4) + (alarm1_minute % 10);
    alarm1_hour = ((alarm1_hour / 10) << 4) + (alarm1_hour % 10);

    // Write alarms data to DS3231
    Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address
    Wire.write(7); // Send register address (alarm1 seconds)
    Wire.write(0); // Write 0 to alarm1 seconds
    Wire.write(alarm1_minute); // Write alarm1 minutes value to DS3231
    Wire.write(alarm1_hour); // Write alarm1 hours value to DS3231
    Wire.write(0x80); // Alarm1 when hours, minutes, and seconds match
    // Write alarm2 hours value to DS3231
    Wire.write(0x80); // Alarm2 when hours and minutes match
    Wire.write(4 | alarm1_status | (alarm2_status << 1)); // Write data to DS3231 control register (enable interrupt when alarm)
    Wire.write(0); // Clear alarm flag bits
    Wire.endTransmission(); // Stop transmission and release the I2C bus
    delay(200); // Wait 200ms
    }
    if(!digitalRead(button2) && digitalRead(alarm_pin)){ // When button B2 pressed with alarm (Reset and turn OFF the alarm)
    digitalWrite(alarm_pin, LOW); // Turn OFF the alarm indicator
    Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address
    Wire.write(0x0E); // Send register address (control register)
    // Write data to control register (Turn OFF the occurred alarm and keep the other as it is)
    Wire.write(4 | (!bitRead(status_reg, 0) & alarm1_status) | ((!bitRead(status_reg, 1) ) << 1));
    Wire.write(0); // Clear alarm flag bits
    Wire.endTransmission(); // Stop transmission and release the I2C bus
    }
    DS3231_read(); // Read time and calendar parameters from DS3231 RTC
    alarms_read_display(); // Read and display alarms parameters
    DS3231_display(); // Display time & calendar
    delay(50); // Wait 50ms
    }

  11. hello greetings, nice to see your project, I want to ask you if it is possible to configure your skech to have as alarm days instead of hours

    1. Yes, you can add day or date to the alarm functions, so the alarm will be when date, hours, and minutes match or when day, hours, and minutes match (hours can not be removed). To do that you’ve to do some modifications to the code. More details are on the DS3231 datasheet.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top