Interfacing PIC12F1822 with DS3231/DS1307 and I2C LCD

This post shows how to make a real time clock using PIC12F1822 microcontroller and DS3231 RTC chip (or DS1307 RTC) where time and date are displayed on 16×2 LCD. The DS3231 as well as the 16×2 LCD are connected to the same I2C bus with the PIC12F1822 microcontroller. The LCD is provided with PCF8574 I/O expander which makes it an I2C LCD. This project works also with DFRobot I2C LCD displays.

Last time I made an interfacing of PIC12F1822 and I2C LCD, project link is below:
Interfacing PIC12F1822 microcontroller with I2C LCD

In this project the DS3231 (DS1307) and the I2C LCD share the same I2C bus which means the SDA lines of the two devices are connected together and the SCL lines also, that minimizes number of pins used.
Time and date are displayed on the 16×2 LCD and they can be set with two push buttons connected to the PIC12F1822 microcontroller. With two pins for the I2C bus and two other pins for the push buttons, the microcontroller will use 4 pins (if internal oscillator is used).

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

  • PIC12F1822 microcontroller
  • DS3231 (or DS1307) board
  • 16×2 LCD screen
  • PCF8574 I/O expander (or PCF8574A)   —   PCF8574 datasheet
  • 2 x pushbutton
  • 5 x 10k ohm resistor
  • 330 ohm resistor
  • 10k ohm variable resistor or potentiometer
  • 3V coin cell battery
  • 5V source
  • Breadboard
  • Jumper wires

PIC12F1822 with DS3231 RTC board and I2C LCD

The Circuit:
Project circuit diagram is shown below.

PIC12F1822 DS3231 I2C LCD circuit

(All grounded terminals are connected together)

The PIC12F1822 microcontroller has one hardware I2C module with SDA on pin RA2 (#5) and SCL on pin RA1 (#6).

The I2C LCD and the DS3231 (or DS1307) board share the same I2C bus which means the SDA line of the I2C LCD (presented by PCF8574) and the DS3231 RTC chip are connected together with RA2 pin of the PIC12F1822 MCU, the SCL line of the I2C LCD and the SCL line of the DS3231 are connected together with RA1 pin of the PIC12F1822 MCU.

The two push buttons in the circuit are used to set time and date of the real time clock, button 1 (B1) is connected to RA5 pin (#2) and button 2 (B2) is connected to RA4 pin (#3) of the PIC12F1822 MCU.

PIC12F1822 internal oscillator is used (@8MHz) and MCLR pin (RA3) is configured as an input pin.

The Code:
The code below works with DS3231 and DS1307 without any modification. It was tested with CCS C compiler version 5.051.

To be able to compile the C code below, a driver for the I2C LCD is required, download link is in the following topic:
I2C LCD driver for CCS PIC C compiler

Or it can be downloaded from this link:
I2C LCD driver download

I2C LCD and DS3231 (DS1307) share the same I2C bus and the PIC12F1822 MCU communicates only with 1 device at a time depending on the address, the I2C LCD address is 0x4E and the DS3231 (the DS1307 also) address is 0xD0.

CCS C code:

The following video shows a breadboard circuit of the project:

and the one below shows Proteus simulation where DS1307 RTC is used. Using DS3232 chip will give the same result:

Proteus simulation file download:
PIC12F1822 + DS1307 + I2C LCD

2 thoughts on “Interfacing PIC12F1822 with DS3231/DS1307 and I2C LCD”

  1. Hey,

    Where can I find the I2C functions library? I can’t find it.
    So I can’t use the i2c_start (and other) functions.

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