Go Down

Topic: DS3234 Deadon RTC with NTP question (Read 1 time) previous topic - next topic


I have a problem with the Deadon RTC "(DS3234).

1) I have compiled and ran NTP sketches and got them to function just fine (without overloading our network btw).
2)I have successfully set the time on the RTC to compile time and it functions but gets less accurate over time.

My idea is to have the RTC updated with NTP and set THAT time each time I send an NTP request (approx once a day/week)

However.... I cannot seem to do the following:

a)set the RTC time to compile time;
b)send out an NTP request;
c)update RTC with the received NTP packet.

And I have done many searches for this exact thing and am confused with the results that I have found.

Does anyone have an example of how I  would accomplish this?

It would be greatly appreciated.


Why not using this http://arduino.cc/en/Tutorial/UdpNtpClient example and make the loop() (a bit modified to fulfill your needs) just a function you call when you wanna your RTC corrected?


Sep 21, 2012, 10:39 pm Last Edit: Sep 21, 2012, 11:01 pm by MSpijk Reason: 1
Maybe I can help here.....
I have a sketch wich uses NTP to sync my DS3234. The sketch uses my Arduino Duemilanove, an Ardino Ethernet R3 Shield and a DS3234.
The shield is stacked on the Duemilanove and the DS is connected like this; SS - pin 8, MOSI - pin 11, MISO - pin12, and SCK - Pin13.
I connected the shield to my local network and uploaded the sketch....

Please note that the sketch uses the mac-address of my ethernet shield, change it to yours. Also I use the RTC_DS3234.h library.

Code: [Select]


Udp NTP Client
Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them,
see http://en.wikipedia.org/wiki/Network_Time_Protocol
This code is in the public domain.


#include <SPI.h>        
#include <Wire.h>
#include <RTClib.h>
#include <RTC_DS3234.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Avoid spurious warnings
#undef PROGMEM
#define PROGMEM __attribute__(( section(".progmem.data") ))
#undef PSTR
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))

// Create an RTC instance, using the chip select pin it's connected to
const int  RTCchipSelect = 8; //chip select
RTC_DS3234 RTC(RTCchipSelect);

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
 0x90, 0xA2, 0xDA, 0x0D, 0x09, 0x24 }; // Change this to your Ethernet Shield's mac!

unsigned int localPort = 8888;      // local port to listen for UDP packets

IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server

const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup()
 // Open serial communications and wait for port to open:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

 // start Ethernet and UDP
 if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to configure Ethernet using DHCP");
   // no point in carrying on, so do nothing forevermore:

void loop()
 sendNTPpacket(timeServer); // send an NTP packet to a time server
 // wait to see if a reply is available
 if ( Udp.parsePacket() ) {  
   // We've received a packet, read the data from it
   Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer
   //the timestamp starts at byte 40 of the received packet and is four bytes,
   // or two words, long. First, esxtract the two words:
   unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
   unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
   // combine the four bytes (two words) into a long integer
   // this is NTP time (seconds since Jan 1 1900):
   unsigned long secsSince1900 = highWord << 16 | lowWord;  
   Serial.print("Seconds since Jan 1 1900 = " );
   // now convert NTP time into everyday time:
   Serial.print("Unix time = ");
   // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
   const unsigned long seventyYears = 2208988800UL;    
   // subtract seventy years:
   unsigned long epoch = secsSince1900 - seventyYears;  
   // print Unix time:
   // Syncing the UTC to the RTC
   SetTimeDate(DateTime(epoch).day(), DateTime(epoch).month(), DateTime(epoch).year() - 2000, DateTime(epoch).hour(), DateTime(epoch).minute(), DateTime(epoch).second());
   // print the hour, minute and second:
   Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
   Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
   if ( ((epoch % 3600) / 60) < 10 ) {
     // In the first 10 minutes of each hour, we'll want a leading '0'
   Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
   if ( (epoch % 60) < 10 ) {
     // In the first 10 seconds of each minute, we'll want a leading '0'
   Serial.println(epoch %60); // print the second
 // wait a second before asking for the time again

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
 // set all bytes in the buffer to 0
 memset(packetBuffer, 0, NTP_PACKET_SIZE);
 // Initialize values needed to form NTP request
 // (see URL above for details on the packets)
 packetBuffer[0] = 0b11100011;   // LI, Version, Mode
 packetBuffer[1] = 0;     // Stratum, or type of clock
 packetBuffer[2] = 6;     // Polling Interval
 packetBuffer[3] = 0xEC;  // Peer Clock Precision
 // 8 bytes of zero for Root Delay & Root Dispersion
 packetBuffer[12]  = 49;
 packetBuffer[13]  = 0x4E;
 packetBuffer[14]  = 49;
 packetBuffer[15]  = 52;
 // all NTP fields have been given values, now
 // you can send a packet requesting a timestamp:        
 Udp.beginPacket(address, 123); //NTP requests are to port 123

String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year
       for(int i=0; i<=6;i++){
digitalWrite(RTCchipSelect, LOW);
unsigned int n = SPI.transfer(0x00);        
digitalWrite(RTCchipSelect, HIGH);
int a=n & B00001111;    
int b=(n & B00110000)>>4; //24 hour mode
else if(b==B00000001)
else if(i==4){
int b=(n & B00110000)>>4;
else if(i==5){
int b=(n & B00010000)>>4;
else if(i==6){
int b=(n & B11110000)>>4;
int b=(n & B01110000)>>4;
temp.concat("20") ;
       if (TimeDate[6] < 10 ) {
          // In the first 10 minutes of each hour, we'll want a leading '0'
temp.concat("-") ;
       if (TimeDate[5] < 10 ) {
          // In the first 10 minutes of each hour, we'll want a leading '0'
temp.concat("-") ;
       if (TimeDate[4] < 10 ) {
          // In the first 10 minutes of each hour, we'll want a leading '0'
temp.concat("\t") ;
       if (TimeDate[2] < 10 ) {
          // In the first 10 minutes of each hour, we'll want a leading '0'
temp.concat(":") ;
       if (TimeDate[1] < 10 ) {
          // In the first 10 minutes of each hour, we'll want a leading '0'
temp.concat(":") ;
       if (TimeDate[0] < 10 ) {
          // In the first 10 minutes of each hour, we'll want a leading '0'

int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
       for(int i=0; i<=6; i++){
int b= TimeDate[i]/10;
int a= TimeDate[i]-b*10;
if (b==2)
else if (b==1)
TimeDate[i]= a+(b<<4);
digitalWrite(RTCchipSelect, LOW);
digitalWrite(RTCchipSelect, HIGH);

Here's a screen shot from the Serial console in the attachment.

Go Up