Introduction: Clock-Calender Using DS3231+GLCD+Atmega32

About: I'm a senior telcom engineer by profession.My hobby is designing circuits and successfully implement them.Microcontroller coding is the main area of interest.Analog electronics is another hot favourite. I thin…

Any DIYer feels an unknown attraction for making a clock.Either you are a noob or a professional or a diyer like me,at a certain point of our working span,the urge for making a clock is felt.So here I am with my brand new instructables about how to make an extremely accurate+Good looking clock-calender.

This clock is based on DS3231 chip which is basically the upgraded version(with many additional features) of DS1307.The DS1307 uses an external crystal(32.768KHz) for it's counting.But the counting gets slow/fast depending on the external temperature.As the temp rises/falls,the crystal gets drifted and as a result,the DS1307 gets slow/fast.

In DS3231,the chip uses an internal temp compensated crystal i.e the chip reads the environmental temp and adjust itself accordingly to maintain accurate time.

I have used Atmega32 as the CPU which acts as the heart of the circuit.It is clocked at 16Mhz.

A 128x64 GLCD is used to display the time,date,month,year,day of the week,am/pm.Believe me,the total display is much more beautiful as compared to the picture i uploaded.

Additionally it serves another purpose....If you turn it on at night,there is no need to lit a night lamp....LOLZZZ....

Ok...apart from jokes,let's get started about how to build it.....

Step 1: Materias Needed....

The following materials will be needed to complete our project.....

1)ATmega32A microcontroller.

2)DS3231 breakout board/ChronoDot breakout board from Adafruit.You can get it from here.

It is strongly recommended that you use either ChronoDot breakout or breakout board from a highly reputed seller.

And if you use chinese breakout with the labelling "DS3231 For PI"/any other similar (which i am using right now and the picture is attached),then read the upcoming step very carefully.......

3)128 x 64 GLCD(ks01078 controller)(I am using JHD12864)

4)16Mhz crystal.

5)Resistors:

10k resistor x 2

4k7 resistor x 3

10k POT(pcb mount)

6)Capacitors:

100nF x 2

22pF x 2

470uF/25v x 1

Step 2: Wait......The DS3231 Is Getting Slow!!!!!!!!!!!!!!!!!

Yes....you read it right.

At my very first version of firmware,i programmed it such a way that the Atmega32 was reading the DS3231 register every second and updating data to the GLCD.According to the datasheet,the DS3231 has dual buffer register that means while you are reading the data from the 2nd buffer,the DS3231 will automatically count up in the background and write the data to first buffer.Later it will transfer the data to the second one.That means your reading process has no effect on counting.

But surely it affects the counting for the cheap chinese modules.These suckers stops the counting while you access the registers of the chip in order to read.

My DS3231 clock got approx 17 sec slow(With reference with my Casio G-Shock) within 12hr runtime.Then asking at avrfreaks,i came to know that this could happen because of cheap chinese crap chips.

So then to be sure,i programmed the second version of firmware so that the Atmega32 will read the time every 5min interval.Then i again tested the clock for 24hr and found a delay of 2s.

Now i finally modified the SQW output of the DS3231 breakout board(You will see in the next step) and programmed the chip so that it gives a square wave output of 1Hz(every second you get a falling edge/rising edge signal).Now i configured the interrupt of my ATmega32a on falling edge so that it remains in sync on every second with the DS3231 without even reading it.

Now the ATmega32a reads the DS3231 on power-up and then counts up by getting interrupted every second by the DS3231 output pulse.It only read the DS3231 again on every am/pm change or twice a day.

This way I saw that the Clock is running as compared(without even delayed by a second....still now) to my reference clock from last 8 days.May be this process is pretty accurate,but the chinese chip will get slow sooner or later as compared to a original DS3231.

That's why I emphasize you guys to use a good original clock chip breakout board

like Chronodot.(It is assumed that Adafruit will use original maxim chips)

Simply you can say...."If you use chinese,You will run out of time"......

Step 3: Modifying the "DS3231 for PI" Board to Access the SQW Pin

I am using a crappy chinese DS3231 breakout board which is labelled as "DS3231 for PI".It has no access to the SQW pin.On the other hand,Chronodot breakout board from Adafruit has access to the SQW pin.I also saw many other breakout boards on Internet which has access to the SQW pin.Please check your board that if you have access to the SQW pin.If not,then this modifying will give you an idea abut how to modify your board type.

The first attached pic is the pinout of the DS3231.Pin #3 is our main area of interest.The "DS3231 for PI" board has 5 pins.One of them is "NC" i.e not connected to anything.The main idea is to solder a wire and connect the pin#3 to the NC pin.Also,in order to function properly,the SQW pin must be pulled up i.e you must connect a 4k7 register between SQW pin and Vcc.

I first soldered a 4k7 register between "NC" pin of the board and "Vcc".Then i soldered a 7/36 wire between pin #3 of DS3231 and "NC" pin.Just check my snaps and you will understand.

Now i am able to get the 1Hz square wave output at the "NC" pin of the breakout board.....ready to rock!!!!!!!!!!!!!!!

Word of caution:As the pin spacing of the DS3231 is very small,use proper smd soldering iron to avoid solder blob(unintentional solder that connects two side by side pins).Be very precise while soldering.

DO NOT CONFUSE THE BREAKOUT BOARD "NC" PIN WITH THE "NC" PINS OF DS3231.

Step 4: The Total Circuit Diagram and Functioning.....

The circuit is attached as Diagram.pdf

At a first glance,the circuit may look complex but believe me,it is pretty simple.The Atmega32 is the main brain of the circuit.It is a 8 bit microcontroller with 32k byte of flash.Here it is clocked at 16Mhz using an external crystal.The 22pF caps are for decoupling the clock signal.The 10k resistor and the 100nF cap on the reset line is used to create a power-on-reset circuit for the Atmega32.Note one thing that the power pins(Pin#10 & 11)are not shown in the diagram They are the power pins of the Atmega32 and must be connected to +5v and gnd correspondingly to run the Atmega.

As i previously told,the "DS3231 for PI" breakout board has 5 pins.The NC pin is modified as i stated in the last step.The 4k7 resistor is soldered directly on the board.If your breakout board has the SQW pin available,then check that if it has the pull up(4k7 or any other value) resistor on-board or not.If already any resistor is available on-board,then there is no need for this external 4k7 resistor.But be sure about that.Otherwise the circuit will not work.The Chronodot breakout board doesn't have any pull up resistor on the SQW line.So you need to attach one for yourself.Either you can solder it on the breakout board or attach it in the main pcb/proto-board.The SQW is then connected to Pin#17(INT1) of Atmega to trigger Interrupt every second.

The SCL and SDA pins are connected to the Atmega32's SCL and SDA pins.Note that the "DS3231 for PI" board is mentioned "D" for SDA and "C" for SCL on the board.Connect it accordingly.In order to function properly,these lines must be pulled high.In my breakout board,the resistors are pre soldered on the board.But in Chronodot,you need to solder them.They are marked as "R1" and "R2".Check your own board type for pull up resistors on I2C lines and solder them as per your need.4k7 value resistors are pretty good for pull-up.The Vcc & Gnd pins are the external power pins for the DS3231 and must be connected to 5v.You can leave the RST pin of the Chronodot unconnected.

The 128 x 64 Display which i am using is named JHD12864.You can use any 128 x 64 with ks0108 controller chip.The display has 20 pins.The display is powered from 5v.The Backlight of the LCD is 5v tolerant(generally).I have powered the backlight(Pin#19 & 20) directly from 5v.But be sure if your display supports that.The data pins(D0 to D7) are connected to Atmega's PortA and used for bi-directional data.The control pins(RS,RW,EN,CS1,CS2) are connected to PortB of Atmega which are used to control the display.The 10k pot is used to adjust the brightness of the LCD.

The whole circuit is powered from 5v.You can use any good quality mobile charger but be sure of two things

1)It has less ripple on it's output.Otherwise the devices may malfunction.

2)It should give atleast 500mA as the display will consume high current(Total circuit requires 100~120mA).

Obviously linear power supplies are far more appreciable than smps.If you have any of them,you can power this circuit.

To build a variable linear power supply(With colour display),visit my this Instructable.

Step 5: Software....

We will compile the program two times i.e we will burn program to the microcontroller two times.

First time we will Set the day date etc.

Second time we will program the Atmega32 so that it will only display again & again (without setting) all the clock-calender datas to the GLCD.

Follow the steps very carefully.

The software is written in core avr-gcc using Winavr.It can be downloaded from here.Install Winavr.No need to run for now.

First download and save all the code files in a same location.For ex: in a Folder named "Clock".Move it to desktop.

Another thing,the Makefile can't be uploaded to it's original format.I have uploaded in text format.You have to select the Makefile and rename it without the .txt extension.A warning message will appear in windows,but just proceed by click Yes.The filename would be "Makefile" only(no extension required).

Now right click on the main.c.Now click on open with Programmer's notepad 2.

1)Now a text editor will open.Now go to the menus at top and just near the "Help" menu,you can see a drop down list.Click on it and change it to C/C++.If C/C++ is already selected,then you can skip this step.See the first pic for ref.

2)Now find " int main(void) " function and at the 10th line,you can see a line

//ds3231_set(0x03,0x45,0x00,0x01,0x16,0x07,0x05,0x03);

This is line via which you will set your day,date,time etc etc.Uncomment the line(omit those // at the startup)

The format is - ds3231_set(hour,minute,second,am/pm,Year,month,date,day)

0=am,1=pm.Sunday=1.

Now in my example, I set - 03:45:00 pm 16/07/05 Tue.Change the line accordingly to your needs.I am using 12hr format(I hate 24's).

Now for example,if you want to set 12:20 am of 20/07/2016, Wednesday,then you will write like below

ds3231_set(0x12,0x20,0x00,0x00,0x16,0x07,0x20,0x04);

Now save it by Ctrl+s.Refer to the Second pic.

3)Now Make the total program by Clicking on Tools->[WinAVR] Make All.Refer to pic 3.

4)Now there will be a main.hex file generated in the same folder.Burn it to the Atmega32.

5)After successfull burning,you can see that all the datas are being dispayed on the GLCD.

6)Now again disable the ds3231_set function by writing // in front of it.(The colour should be changed to green).

7)Now click on Tools-> [WinAVR] Make Clean.This will wipe out the old hex file and associated files.

8)Again Make the total program by Clicking on Tools->[WinAVR] Make All.

9)Now burn the newly generated main.hex file to the Atmega32.

The ks0108 library is based on Fabian maximilian Thiele's library.I made some tweaks and added more functions to the original library as per my need.All the other libraries are completely written by me.

Bingo!!!You are done......

Step 6: From Breadboarding to Completion.....

Here are some of my project pics regarding the whole process.All the pics are self explainatory.Please go through them to experience the whole journey from Breadbarding to completion.

At first,the total circuit was built on breadboard and tested for nearly 12-15 days.After the success,I made the PCB by PCB etch method.I powered the circuit using a 5v Mobile charger.A Sparkfun USB MicroB breakout board is used to feed the power through it. The cabinet is a two part cabinet.I cutted,sanded & shaped it as per necessity.Dremel,Drill and Files were my true friend regarding thi swhole installation work.I finally mounted it on the wall.

Step 7: Final Words....

There is nothing to worry about to make this project.I basically completed it as an off-time project.But believe me,it was a pretty interesting journey...especially to overcome the software bugs!!!!!!

The total project can be completed within a single day(If all the parts are next to your hands).So be ready to make it in the upcoming weekend.

Have a great time ahead and also i am available for any type of assistance.Feel free to contact me.......

Good bye.

Rgds//Sharanya