Real time clock with alarm and temperature monitor | MPLAB Projects

This post shows how to build a real time clock with 2 alarm functions and temperature monitor using PIC16F887 microcontroller and DS3231 RTC IC. Time, date, alarms and temperature are displayed on 20×4 LCD screen. Time, date and alarms are set with 3 push buttons connected to the PIC microcontroller.
The compiler used in this project is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler).

Related Projects:
Interfacing LCD with PIC microcontroller | MPLAB Projects
Interfacing DS1307/DS3231 RTC with PIC MCU | MPLAB Projects

PIC18F4550 DS3231 RTC hardware circuit

Hardware Required:
The components required for this project are listed below.

  • PIC16F887 microcontroller   —>  datasheet
  • DS3231 board                   —>  DS3231 datasheet
  • 20×4 LCD screen
  • 3 x pushbutton
  • 2 x 330 ohm resistor
  • LED
  • 10k ohm variable resistor or potentiometer
  • 3V coin cell battery
  • 5V source
  • Breadboard
  • Jumper wires

Real time clock with alarm and temperature monitor circuit:
Project circuit diagram is shown in the image below.

PIC16F887 with DS3231 RTC and 20x4 LCD - MPLAB XC8 - mikroC

(All grounded terminals are connected together)

The DS3231 board is supplied with 5V as the 2004 LCD and the PIC16F887 microcontroller, there are 3 data lines connected between this board and the microcontroller: SCL line is connected to pin RC3 (#18), SDA line is connected to pin RC4 (#23) and INT line is connected to external interrupt pin RB0 (#33). The DS3231 interrupts the microcontroller when there is an alarm (alarm 1 or alarm 2).

In the circuit there are 3 push buttons: B1, B2 and B3. These buttons are used to set time, date and alarms. Time and date can be adjusted with B1 and B2, button B1 selects time or date parameter (time parameters: hours and minutes; date parameters: day of the week, date, month and year) and B2 increments the selected parameter. Buttons B3 and B2 adjust alarm 1 and alarm 2 parameters (hours, minutes and ON/OFF), button B3 selects the parameter and B2 increments the selected parameter.

Also, there is an LED connected to PIC16F887 pin RB4 (#37), this LED is used as an alarm indicator (alarm 1 and alarm 2), so if there is an alarm, the DS3231 pulls down the INT pin (RB0) which interrupts the microcontroller and the microcontroller turns the LED ON, here button B2 turns both the LED and the occurred alarm OFF.

The 20×4 LCD screen is connected to the PIC16F887 microcontroller as follows:
RS —> RD0 pin
E  —> RD1 pin
D4 —> RD2 pin
D5 —> RD3 pin
D6 —> RD4 pin
D7 —> RD5 pin
VSS, RW, D0, D1, D2, D3 and K are connected to circuit GND (ground)
VEE to the variable resistor (or potentiometer) output pin
VDD to +5V and A to +5V through 330 ohm resistor

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

Real time clock with alarm and temperature monitor C code:
The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05.

To be able to compile the C code, a small LCD library for MPLAB XC8 compiler is required which can be downloaded from the following link:

after the download, add the library file (LCD_Lib.c) to project folder.

Some programming hints:
The microcontroller turns the LED ON (connected to pin RB4) when it is interrupted by the DS3231, the DS3231 sends the interrupt signal (pulls down the INT line) when there has been an alarm (alarm 1 or alarm 2). Button B2 resets and turns OFF the alarm. If both alarms are active, button B2 will reset and turn OFF the occurred alarm only and keep 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:
I2C_Write(4 | ((!(status_reg & 1)) & alarm1_status) | (((!((status_reg >> 1) & 1)) & alarm2_status) << 1));
alarm1_status and alarm2_status are 1-bit variables (can be 1 or 0), for example if alarm1_status is 1 ==> alarm 1 is ON and if alarm1_status is 0 ==> alarm 1 is OFF. The same thing for alarm 2.

The microcontroller used in this example is PIC16F887, configuration words are:


  • In-Circuit Debugger disabled
  • Low voltage programming disabled
  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • Brown-out Reset (BOR) disabled
  • Data memory code protection disabled
  • Program memory code protection disabled
  • RE3/MCLR pin function is digital input, MCLR internally tied to VDD
  • Power-up Timer (PWRT) disabled
  • Watchdog Timer (WDT) disabled
  • INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
  • Flash Program Memory Self Write disabled
  • Brown-out Reset set to 4.0V

Full MPLAB XC8 code:

The video below shows how the project should work in hardware circuit (in this video the microcontroller used is PIC16F877A):

Leave a Reply

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