PIC18F46K22 with DS3231 RTC and SSD1306 OLED | mikroC Projects

This post shows how to build real time clock & calendar with 2 alarm functions and chip temperature monitor using PIC18F46K22 microcontroller and DS3231 RTC chip.
In this project time, date and alarms are displayed on SSD1306 OLED screen (128×64 pixel) and they could be set from three push buttons connected to the PIC18F46K22.
MikroC PRO for PIC compiler is used in this project.

The DS3231 RTC has built-in two alarm functions and temperature sensor with resolution of 0.25 °C and accuracy of ±3 °C. Chip temperature also will be displayed on the SSD1306 OLED.

In this project the SSD1306 OLED is configured to work in I2C mode, make sure that your display is configured to work in I2C mode, some displays need jumper placing or some soldering.

To see how to interface PIC18F46K22 MCU with SSD1306 OLED display (I2C mode), take a look at the following project:
Interfacing PIC18F46K22 with SSD1306 OLED display | mikroC Projects

Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • SSD1306 OLED display
  • DS3231 board   —->  DS3231 datasheet
  • 3 x push button
  • 330 ohm resistor
  • LED
  • 3V coin cell battery
  • 5V power source
  • Breadboard
  • Jumper wires

PIC18F46K22 with DS3231 RTC and SSD1306 OLED circuit:
The following image shows project circuit schematic diagram.

SSD1306 OLED DS3231 PIC18F46K22 with alarm and set buttons circuit

All the grounded terminals are connected together.

The PIC18F46K22 microcontroller has 2 hardware I2C modules (MSSP1 and MSSP2 modules).
In this project I2C1 module is used with SDA1 on pin RC4 (#23) and SCL1 on pin RC3 (#18). The SDA1 pin of the MCU is connected to the SDA pin of the display and the SCL1 pin of the MCU is connected to the SCL pin of the display.
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 I2C slave address of the device is 0x7A.

The SSD1306 OLED and the DS3231 RTC share the same I2C bus, SCL and SDA pins of the DS3231 board are also connected to SCL1 and SDA1 pins of the PIC18F46K22.

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 are set with B1 and B2 where 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 set 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 PIC18F46K22 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 line (RB0) which interrupts the microcontroller and the microcontroller turns the LED ON, here button B2 turns both the LED and the occurred alarm OFF.

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

PIC18F46K22 with DS3231 RTC and SSD1306 OLED 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 for the SSD1306 OLED display is required, its full name (with extension) is SSD1306OLED.C, download link is the one below:
SSD1306 OLED mikroC library

for more information about this driver, visit the following post:
SSD1306 OLED display library for mikroC compiler | mikroC Projects

Also, another library for the DS3231 RTC is required, its full name (with extension) is: DS3231.C, download link is the one below:
DS3231 mikroC library

and for more details about it, visit this topic:
DS3231 RTC Library for mikroC Compiler | mikroC Projects

after the download of the 2 library files, add both of them to mikroC project folder.

The connection of the 3 push buttons, the LED (alarm indicator) and the SSD1306 OED reset pin are defined in the C code as shown below:

The drivers of the SSD1306 OLED and the DS3231 RTC are included in the code using the 2 lines below:

Functions used in the code:
char debounce (int button): this function is for deboucing button, it used in the code for debouncing B1  and B3, returns 1 if button is debounced.

void dow_print(): displays day of the week (Monday, Tuesday …) on the LCD.

void rtc_print(): displays time, date, day of the week, alarm 1, alarm 2 and chip temperature on the display. This function calls the previous one for displaying the day of the week.

void wait(): this function is just a delay of 500 ms except that it can be interrupted by the two push buttons (B1 and B2). In this function Timer0 module is used to count the 500 milliseconds, it is used as 16-bit timer with prescaler = 16 (==> 1 tick every 8 microseconds ==> 62500 x 8 = 500000 us = 500 ms).

char edit(char x_pos, char y_pos, char parameter): this function is for setting the real time clock, returns the edited parameter.

void alarms_edit(char _alarm): this function is for setting the status of alarm 1 and alarm 2 (ON or OFF). If the variable _alarm = 1 the alarm will be set is alarm 1, and if _alarm = 2 the alarm will be set is alarm 2.

Rest of code is described through comments.

Full mikroC code:

The following picture shows a protoboard circuit of the project:

SSD1306 OLED DS3231 PIC18F46K22 real time clock with alarm

Proteus simulation of this project should give a result near to the one shown in the video below where 20×4 LCD is used instead of the SSD1306 OLED display:


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

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