In this illustration we will going to wire the MAXIM DS3231 Real Time IC Clock. This module can be used in almost any project that requires a real time clock and date. The pins are already installed so that you can just easily connect the jumper wires or solder your own connection onto the board itself. It very easy to use and with the help of MCU.
This DS3231 Module has the CR2032 battery to power the module as standalone even if there is now MCU still running at real time. The board has also small ATMEL AT24C32 32K EEPROM which is handy for storing date, settings and other configuration.
This module work both 3.3v and 5v tolerant so you can use it with almost any other development platform board that uses I2C BUS. It uses the same command as the DS1307. The benefits over the DS1307 is the increased of accuracy, down to +/- 3.5 ppm whereas the DS1307 can vary wildly depending on the temperature, accuracy and the quality of external crystal and capacitors.
Required Components
- Arduino UNO/MEGA/MINI/PRO
- DS323 Real Time Clock Module
- Solder Less Bread Board
- Jumper Wires / DuPont Wires
Wiring Diagram
As you can see the illustration above you need to ignore the 32K and SQW pins, you will not going to used them. Place the jumper wire to SCL into your board A5 port, and the SDA pin goes to pin A4 port, if you’re using other version of Arduino that has SCL and SDA ports and AREF if the board has this port you can also use them. The power VCC pin you going to plug into the 5V pin and the ground GND place it into the Ground pin.
You need to double check the connections before plugging your board into your computer improper wiring can damage your modules and sensors.
If done everything correctly, when you going to plug the Arduino to the computer there will be a RED LED light on the clock module board showing that the board is ready to drive. If you don’t get any light from the board you need to double check the wiring connections.
Once you’re done, you need to import the DS3231 code library which can be downloaded here. This code library contains the code as well as the libraries required for this illustration, copy the file DS3231.cpp, DS3231.h, keyword.txt and readme.txt to your Arduino Libraries, folder name DS3231.
As you can see the sketch below this code is already on the library except that there will be a comments.
Arduino Sketch Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
#include <DS3231.h> //Import DS3231 Code Library #include <Wire.h> //Import Wire Code Library DS3231 Clock; bool Century=false; bool h12; bool PM; byte XDAY, XHOUR, XMIN, XSEC, XBITS; bool XDY, A12h, Apm; byte year, month, date, DoW, hour, minute, second; void setup() // Start the I2C interface { Wire.begin(); // THIS IS VERY IMPORTANT // If we going to set the time, change these numbers to the date and time you want to set to, and then upload it to the arduino. // once you have finished setting the time, comment out the following clock.set functions and then re-upload it to the board. Otherwise your clock will reset every time you open the serial monitor. Clock.setSecond(50);//To set the second Clock.setMinute(59);//To set the minute Clock.setHour(11); //To set the hour Clock.setDoW(5); //To set the day of the week Clock.setDate(31); //To set the date of the month Clock.setMonth(5); //To set the month of the year Clock.setYear(13); //To set the year (Last two digits of the year) Serial.begin(9600); // Start the serial interface at 9600 boud rate } void ReadDS3231() { int second,minute,hour,date,month,year,temperature; second=Clock.getSecond(); minute=Clock.getMinute(); hour=Clock.getHour(h12, PM); date=Clock.getDate(); month=Clock.getMonth(Century); year=Clock.getYear(); temperature=Clock.getTemperature(); Serial.print("20"); Serial.print(year,DEC); Serial.print('-'); Serial.print(month,DEC); Serial.print('-'); Serial.print(date,DEC); Serial.print(' '); Serial.print(hour,DEC); Serial.print(':'); Serial.print(minute,DEC); Serial.print(':'); Serial.print(second,DEC); Serial.print('\n'); Serial.print("Temperature ="); Serial.print(temperature); Serial.print('\n'); } void loop() { ReadDS3231(); delay(1000); /* Serial.print("2"); if (Century) { // Won't need this for 89 years. Serial.print("1"); } else { Serial.print("0"); } Serial.print(Clock.getYear(), DEC); Serial.print('-'); // then the month Serial.print(Clock.getMonth(Century), DEC); Serial.print('-'); // then the date Serial.print(Clock.getDate(), DEC); Serial.print(' '); */ // and the day of the week /* Serial.print(Clock.getDoW(), DEC); Serial.print(' '); */ // Finally the hour, minute, and second /* Serial.print(Clock.getHour(h12, PM), DEC); Serial.print(':'); Serial.print(Clock.getMinute(), DEC); Serial.print(':'); Serial.print(Clock.getSecond(), DEC); // Add AM/PM indicator if (h12) { if (PM) { Serial.print(" PM "); } else { Serial.print(" AM "); } } else { Serial.print(" 24h "); } // Display the temperature Serial.print("T="); Serial.print(Clock.getTemperature(), 2); // Tell whether the time is (likely to be) valid if (Clock.oscillatorCheck()) { Serial.prin t(" O+"); } else { Serial.print(" O-"); } */ // Indicate whether an alarm went off /* if (Clock.checkIfAlarm(1)) { Serial.print(" A1!"); } if (Clock.checkIfAlarm(2)) { Serial.print(" A2!"); } */ // New line on display //Serial.print('\n'); // delay(1000); // Display Alarm 1 information /* Serial.print("Alarm 1: "); Clock.getA1Time(XDAY, XHOUR, XMIN, XSEC, XBITS, XDY, A12h, Apm); Serial.print(XDAY, DEC); if (XDY) { Serial.print(" DoW"); } else { Serial.print(" Date"); } Serial.print(' '); Serial.print(XHOUR, DEC); Serial.print(' '); Serial.print(XMIN, DEC); Serial.print(' '); Serial.print(XSEC, DEC); Serial.print(' '); if (A12h) { if (Apm) { Serial.print('pm '); } else { Serial.print('am '); } } if (Clock.checkAlarmEnabled(1)) { Serial.print("enabled"); } Serial.print('\n'); // Display Alarm 2 information Serial.print("Alarm 2: "); Clock.getA2Time(XDAY, XHOUR, XMIN, XBITS, XDY, A12h, Apm); Serial.print(XDAY, DEC); if (XDY) { Serial.print(" DoW"); } else { Serial.print(" Date"); Serial.print(' '); Serial.print(XHOUR, DEC); Serial.print(' '); Serial.print(XMIN, DEC); Serial.print(' '); if (A12h) { if (Apm) { Serial.print('pm'); } else { Serial.print('am'); } } if (Clock.checkAlarmEnabled(2)) { Serial.print("enabled"); } */ /* //To display alarm bits Serial.print('\n'); Serial.print('Alarm bits: '); Serial.print(XBITS, DEC); */ /* Serial.print('\n'); Serial.print('\n'); delay(1000); // Display the time once more as a test of the getTime() function Clock.getTime(year, month, date, DoW, hour, minute, second); Serial.print(year, DEC); Serial.print("/"); Serial.print(month, DEC); Serial.print("/"); Serial.print(date, DEC); Serial.print("day of the week :"); Serial.println(DoW, DEC); Serial.print(hour, DEC); Serial.print(":"); Serial.print(minute, DEC); Serial.print(":"); Serial.println(second, DEC); */ } |
Datasheets & Code Libraries
Download the code DS3231.h library here | Rar
Download 24C32 Serial EEPROM Datasheet here | Pdf
Download DS3231 I2C Integrated RTC/TCX0/CRYSTAL Datasheet here | Pdf
Download Schematics Diagram here | Pdf