DCF77 library for Arduino - synchronize with atomic clock

I wrote yet another DCF77 library for Arduino.
It is based on the sketch by Matthias Dahlheimer, but with some differences

  • Plays nice with the Time Library and setSyncProvider callback
  • Uses a single interrupt for signal-changed based triggering
  • More robust against noise: rejects the majority of incorrect spikes
  • More sanity checking on decoded time.
  • Can return UTC time, useful for timezone conversion

You can find the library here:
http://thijs.elenbaas.net/downloads/?did=1
and additional information here
http://thijs.elenbaas.net/2012/04/arduino-dcf77-radio-clock-receiver-library/

Below is an example of how to use it using setSyncProvider

#include "DCF77.h"
#include "Time.h"
using namespace Utils;
#define DCF_PIN 2   // Connection pin to DCF 77 device
#define DCF_INTERRUPT 0      // Interrupt number associated with pin
time_t prevDisplay = 0; // when the digital clock was displayed
time_t time;
DCF77 DCF = DCF77(DCF_PIN,DCF_INTERRUPT);
void setup() {
Serial.begin(9600);
DCF.Start();
setSyncInterval(30);
setSyncProvider(getDCFTime);
// It is also possible to directly use DCF.getTime, but this function gives a bit of feedback
//setSyncProvider(DCF.getTime);
Serial.println("Waiting for DCF77 time ... ");
Serial.println("It will take at least 2 minutes until a first update can be processed.");
while(timeStatus()== timeNotSet) {
// wait until the time is set by the sync provider
Serial.print(".");
delay(2000);
}
}
void loop()
{
if( now() != prevDisplay) //update the display only if the time has changed
{
prevDisplay = now();
digitalClockDisplay();
}
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.println("");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
unsigned long getDCFTime()
{
time_t DCFtime = DCF.getTime();
// Indicator that a time check is done
if (DCFtime!=0) {
Serial.print("X");
}
return DCFtime;
}

I would appreciate it if anybody would give it a testdrive. Let me know what you find.
Thijs

excuse my cruiosity, but what does a dcf77 do and where do you buy them?

Hi,
You're excused :wink: DCF77 is officially the name of a radio station in Germany. This radio tower sends out an atomic clock synchronized signal modulated on a 77kHz longwave that can be received in most of Europe (see this post for more information). The hardware that you need to receive this signal is quite cheap and simple to connect to your Arduino ).

However, you profile says that you reside in Australia, and I'm not aware of any existing Australian equivalent of DCF77, so this might not be an option for you. A GPS module could be a good alternative and isn't that expensive either.

Thijs

thanks for explanation. I'm already up to speed on GPS, I use a LS20031 that gives me time just fine (using time library). However although I have been able to get setsyncprovider to work with a DS1307 RTC I can't get GPS to respond... hence my interest in your thread.
I ran a thread on it here: has anyone succeeded with timeGPS from time library? - Programming Questions - Arduino Forum

I don't have a GPS so I cannot reproduce this, but I would use a stub for the callback. Below is a sketch that I used for testing the DCF77 library.

#include "DCF77.h"
#include "Time.h"

time_t time;
DCF77 DCF = DCF77(2,0);
int count = 0;

void setup() {
  Serial.begin(9600); 
  DCF.Start();
  setSyncInterval(30);
  setSyncProvider(getDCFTime);
}
void loop()
{    
  count++;
  delay(3000);
  if  (timeStatus()== timeNotSet) {           
     Serial.print("-");
  } else {
    time = now();
    Serial.print("+");
  }
  if (count==20) {
      count=0;
      Serial.println("");       
  }
}

// Stub for GetTime function
unsigned long getDCFTime()
{ 
  time_t DCFtime = DCF.getTime();
  if (DCFtime==0) {
    Serial.print("n");  
  } else {
    Serial.print("u");  
  }
  return DCFtime;
}

The setSyncProvider is set to query the time provider every 30 sec, the main loop asks for a new time every 30s.
The output is the following:
n = time provider is queried but did not return update
u = time provider is queried and returned update

  • = 3s have past, internal clock is not set
  • = 3s have past, internal clock is set

The results I found where:

n--------------------
-----n---------------
-----nnnn---------------
-----nnnnu++++++++++nnnn+n+n+n+n+
n+n+n+n+n+nnnnnu++++++++++n+n+n+n+n+
n+n+n+n+n+nnnnnu++++++++++n+n+n+n+n+
n+n+n+n+n+nnnnnu++++++++++n+n+n+n+n+
n+n+n+n+n+nnnnnu++++++++++n+n+n+n+n+

What I learned from this:

  • timeStatus() and now() together somehow trigger a time provider query 4 times, which can be seen by the "nnnn" sequences
  • When the time between updates has past (30s) the setSyncProvider will query the timeprovider every call to now() or timeStatus(), which can be seen by the "n+n+n+n+"sequence

I imagine that you can learn a bit about the GPS time update in this manner without diving into the library itself. Hope this helps.

that's a very neat and clever testing technique!
I will steal your idea and give it a try on my GPS, but it will be a while before I get to it because I'm focused on getting code working for my new accelerometer code at present - too many toys to choose from :slight_smile: but I'll let you know what I find

I'm interested to hear what you find! Good luck with your accelerometer code.

Library update:
After finding the TimeZone library by Jack Christensen (see this thread), I added an example to convert DCF time (which is in CET) to another timezone. However, since the local time to UTC time conversion is sometimes ambiguous, I included a function that returns UTC time, taking into account the daylight saving time bits

#include <DCF77.h>       //https://github.com/thijse/Arduino-Libraries/downloads
#include <Time.h>        //http://www.arduino.cc/playground/Code/Time
#include <Timezone.h>    //https://github.com/JChristensen/Timezone

#define DCF_PIN 2	         // Connection pin to DCF 77 device
#define DCF_INTERRUPT 0		 // Interrupt number associated with pin

//United Kingdom (London, Belfast)
TimeChangeRule rBST = {"BST", Last, Sun, Mar, 1, 60};        //British Summer Time
TimeChangeRule rGMT = {"GMT", Last, Sun, Oct, 2, 0};         //Standard Time
Timezone UK(rBST, rGMT);

time_t prevDisplay = 0;          // when the digital clock was displayed
time_t time;
DCF77 DCF = DCF77(DCF_PIN,DCF_INTERRUPT);

void setup() {
  Serial.begin(9600); 
  DCF.Start();
  setSyncInterval(30);
  setSyncProvider(getDCFTime);
  // It is also possible to directly use DCF.getTime, but this function gives a bit of feedback
  //setSyncProvider(DCF.getTime);

  Serial.println("Waiting for DCF77 UK local time ... ");
  Serial.println("It will take at least 2 minutes until a first update can be processed.");
  while(timeStatus()== timeNotSet) { 
     // wait until the time is set by the sync provider     
     Serial.print(".");
     delay(2000);
  }
}

void loop()
{  
  if( now() != prevDisplay) //update the display only if the time has changed
  {
    prevDisplay = now();
    digitalClockDisplay();  
  }
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.println("");
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

unsigned long getDCFTime()
{ 
  time_t DCFtime = DCF.getUTCTime(); // Get  UTC time
  
  if (DCFtime!=0) {
    time_t LocalTime = UK.toLocal(DCFtime);  Convert to UK time
    return LocalTime;
  }
  return 0;
}

Since no big issues have arisen, I have added the library to the playground: Arduino Playground - DCF77

Hi,

I am building a Nixie clock ( who isn't... :wink: ) and I use the DCF77 of Conrad. Not the most stable and best buy but at least it is cheap.

At first I was not able to get the time with your example. Even some tweaks didn't do the trick.
However, the tracer showed me a lot of distortion so I went looking in your code and found a missing filter in the DCF77::int0handler function of the file DCF77.cpp.
You do filter out distortion after the pulse ( for 700ms ) but not at the beginning of the pulse.

I added 1 if-statement at the beginning of the function and now it works perfectly now within my system!!!!

 void DCF77::int0handler() {
	int flankTime = millis();
	int sensorValue = digitalRead(2);

	// If flank is detected quickly after previous flank up
	// this will be an incorrect pulse that we shall reject
	if ((flankTime-PreviousLeadingEdge)<DCFRejectionTime) {
		return;
	}

	// ********
	// If the detected pulse is to short it will be an
	// incorrect pulse that we shall reject
	if ((flankTime-leadingEdge)<50) {
		return;
	}
	// ********

	if(sensorValue==HIGH) {
		if (!Up) {
			// Flank up
			leadingEdge=flankTime;
			Up = true;		                
		} 
....

Great yob by the way... saves me a lot of worries to design it myself :wink:

Hi,

Thanks for the suggestion! Indeed, the Conrad receiver really is not all that stable. But then again, many receivers seem to have that problem.
Nevertheless, my Conrad receiver has not given me that many false flanks during a pulse, so I never saw the need to implement this kind of rejection.

It seems like a good idea, though! I've added it to the library, together with some minor fixes. It can still be downloaded here:
http://thijs.elenbaas.net/downloads/?did=1

Let me know what if it works for you!

Ps. I'm part of the other 50%: I'm building a Word-Clock instead of a Nixie Clock. :slight_smile:

I can change the pin and therefore the interrupt?
because I would use 2 pins for other purposes and to "read" the signal DCF77 use pin 18 with interrupt 5.
but I think it does not work.
I have to change something?

thanks

P.S. I use arduino mega 2560

Yes, you can initialize the library with any pin and interrupt number. You need to use one of the latest versions, there used to be a bug in this part of the code. According to this page, you can use the following pin/interrupt combinations on the Arduino Mega:

Pin 2 - interrupt 0
Pin 3 - interrupt 1
Pin 18 - interrupt 5
Pin 19 - interrupt 4
Pin 20 - interrupt 3
Pin 21 - interrupt 2

When using, for example, pin 18, you can start your code with

#define DCF_PIN 18	         // Connection pin to DCF 77 device
#define DCF_INTERRUPT 5		 // Interrupt number associated with pin

DCF77 DCF = DCF77(DCF_PIN,DCF_INTERRUPT);

I'm using the DCF-2 module from ELV, and the DCF77 library at first did not work for me.
The problem is solved now, but here are the symptoms I had, for others using the same hardware ( http://www.elv.de/dcf-empfangsmodul-dcf-2.html ).

  • The hardware seems to work; the sketch "DCFSignal" shows received data
    in the expected 1000ms intervals.

  • The pulse length seems to be wrong: the "DCFPulseLength" sketch shows that a pulse is not around 100ms and 200ms long, but nearer 800ms or 900ms instead.

  • Since a pulse longer than 180ms is considered "1", the "DCFBinaryStream" sketch shows received "1"s exclusively. The buffer is filled with the "1", and afterwards the parity is wrong.

After much debugging the reason for this behaviour became clear: the ELV "DCF77-2" module (which only has one signal output) delivers an inverted signal.

The following code change solved the problem: DCF77.cpp 0.9.7, line 95:

        int sensorValue = digitalRead(dCF77Pin);

to

        int sensorValue = !digitalRead(dCF77Pin);

(Note the exclamation mark before digitalRead() used to convert each "0" to a "1" and vice versa).

If somebody pointed me to the right direction (mrTee?) I could provide (hopefully) more elegant changes which also fix all warnings that compilation using Arduino 1.0.1 currently cause.

Hi,

That was thorough bug hunting! I had a look at the specs, and I could not find any mention on outputting an inverted signal. I've send you a test version of the library with a small change that should make it able to deal with inverted signals. If it works I will publish it (after my holiday).

Where there any warnings while compiling the library before making changes? I checked, but in Arduino 1.0.1 with verbose logging turned on, I do not see any warnings.

A little later than expected, the library now supports inverted pulses as input.

The constructor now has an optional parameter OnRisingFlank:

DCF77(DCF77Pin, DCFinterrupt, OnRisingFlank)

If OnRisingFlank set to false, the algorithm will trigger on falling edge.

Hi!

I wired a cheap DCF77 receiver from Pollin to an Arduino Uno: DCF 77 Empfangsmodul DCF1 online kaufen | Pollin.de

It works out of the box. No extra components were necessary.

Then I ran the examples bundled with the DCF77 library. It turns out that SyncProvider is one minute ahead at the first sync. InternalClockSync works as expected.

Please find the output of InternalClockSync at Output of InternalClockSync - Pastebin.com

The output of SyncProvider is here: Output of SyncProvider - Pastebin.com
In line 4 of the output of SyncProvider it’s 12:01:00 actually. After the next sync the time is corrected (line 126).

Another output is here: Output of SyncProvider 2 - Pastebin.com

I enabled VERBOSE_DEBUG and recorded a video of the output:
http://dl.dropbox.com/u/87685034/SyncProvider_3_converted.avi

I tried both examples a couple of times. The DCF77 signal is quite clear.

Any idea what’s going wrong?

Best regards
Franz

I implemented a DCF77 clock with an additional exponential filter in order to improve noise tolerance. You can find the experiment here: Binary DCF77 Clock | Blinkenlight. This is actually part of a larger project where I intend to push the noise tolerance quite a lot further: DCF77 Project | Blinkenlight.

I am trying this module to get to work. According the example DCFSignal, the module seems to work, but the pulses seem to be to short so that the library cant interpret it (see attached screenshot). Does anyone have a clue how to fix that issue?

How did you connect this module to your Arduino? This is a 3.3V module with very limited driving capability. In doubt I would say you are overloading the module.