PIC MCU with SSD1306 OLED and DS3231 RTC | mikroC Projects

This simple project shows how to build a real time clock with temperature monitor using PIC16F887 microcontroller, DS3231 RTCC (or DS3232) and SSD1306 OLED display. Time and date are set with two push buttons and they are displayed on the SSD1306 OLED screen (128×64 Pixel).
The DS3231 has a buit-in temperature sensor which means we can use it for measuring the chip temperature.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.

The DS3231 is more accurate than the DS1307 due to its built-in temperature sensor. It also (the DS3231) keeps time running if the main power source is down (with the help of a 3V coin cell battery). It also uses I2C interface to communicate with the master device which is in this project PIC16F887 microcontroller. This means the DS3231 and the SSD1306 OLED screen share the same I2C bus. Even they share the same bus but at any time the microcontroller ‘speaks’ with 1 device only depending on the address sent. The DS3231 RTC address is 0xD0 (the same as the DS1307 address) and the SSD1306 address is 0x7A.
In the circuit there are two push buttons for setting time and date of the real time clock.

Related Projects:
Interfacing PIC microcontroller with SSD1306 OLED | mikroC Projects
PIC16F887 Interfacing with SSD1306 and DS1307 | mikroC Projects

Hardware Required:

  • PIC16F887 microcontroller
  • SSD1306 OLED display (128×64 Pixel)
  • DS3231 board   —   DS3231 RTC datasheet
  • 2 x push button
  • 3V coin cell battery
  • 5V power source
  • Breadboard
  • Jumper wires

Interfacing PIC16F887 with DS3231 RTC and SSD1306 OLED circuit:
The following image shows project circuit diagram.

PIC16F887 with DS3231 RTC and SSD1306 OLED - SSD1306 DS3231

(All grounded terminals are connected together)

The PIC16F887 microcontroller has one hardware I2C module (MSSP module) with SDA on pin RC4 (#23) and SCL on pin RC3 (#18). The SDA pin of the MCU is connected to the SDA pin of the display (& DS3231 board) and the SCL pin of the MCU is connected to the SCL pin of the display (& DS3231 board).
The reset pin of the display is connected to pin RD4 (#27) of the microcontroller.

The SSD1306 OLED display DC pin is connected to VDD which means the I2C slave address of the display is 0x7A.

PIC16F887 microcontroller, DS3231 board and SSD1306 display board are supplied with 5V source between their VDD (or VCC) and GND pins.

The two push buttons B1 and B2 are for setting time and date. Button B1 is connected to pin RB0 (#33) and B2 is connected to pin RB1 (#34). Internal pull-ups for RB0 and RB1 pins are enabled in the code.

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

The C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.

To be able to compile the C code below with no error, a driver (& library) for the SSD1306 OLED display is required, it’s full name is SSD1306.C, download link is below:
SSD1306 OLED display driver for mikroC compiler

after the download, add the driver file (SSD1306.C) to project folder.

The DS3231 RTC and the SSD1306 OLED display share the same I2C bus, DS3231 I2C address is 0xD0 and SSD1306 display I2C address is 0x7A (default address).

Functions used in the code:
The RTC chip (DS1307 or DS3231) works with BCD format only, to convert BCD to decimal and vise versa I used the 2 functions below. Before displaying (after reading from RTC IC), the data have to be converted from BCD to decimal, and before writing to the RTC IC (after editing the parameters) the data have to be converted from decimal to BCD:
char bcd_to_decimal(char number);
char decimal_to_bcd(char number);
Each function returns the converted value of the variable number.

void RTC_display() : displays time, date and chip temperature, before printing time and date on the screen, they are converted from BCD format to decimal format using the function bcd_to_decimal(char number) .

void display_day(): this function is for displaying day of the week (Saturday, Sunday …).

char edit(char x, char y, char parameter) : I used this function to edit time and date parameters (minutes, hours, date, month and year). I used a variable named i to distinguish between the parameters:
i = 0, 1, 2 : date, month and year respectively
i = 3, 4: hours and minutes respectively

After the edit of time and date, the data have to be converted back to BCD format using the function decimal_to_bcd(char number) and written to the RTC chip.

void blink() : this small function works as a delay except that it can be interrupted by buttons B1 (connected to RB0) and B2 (connected to RB1). When called and without pressing any button the total time is 100 x 5ms = 500ms. With this function we can see the blinking of the selected parameter with a frequency of 1 Hz. So a delay of 500ms comes after the print of the selected parameter and after that delay, 2 spaces are printed which make the parameter disappears from the screen and another 500ms delay comes after the print of the 2 spaces.

Full mikroC code:
Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700

Hardware circuit result of this project should be similar to what’s shown in the following video where PIC16F877A microcontroller is used:

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