Go Down

Topic: DCF77 library for Arduino - synchronize with atomic clock (Read 18600 times) previous topic - next topic

mrTee

Apr 19, 2012, 11:22 am Last Edit: Jul 25, 2012, 04:17 pm by mrTee Reason: 1
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
Code: [Select]
#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




ninja2

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

mrTee

#2
Apr 19, 2012, 03:42 pm Last Edit: Apr 19, 2012, 08:28 pm by mrTee Reason: 1
Hi,
You're excused  ;) 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

ninja2

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: http://arduino.cc/forum/index.php/topic,81175.msg612786.html#msg612786

mrTee

#4
Apr 20, 2012, 09:25 pm Last Edit: Apr 20, 2012, 09:26 pm by mrTee Reason: 1
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.
Code: [Select]
#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:
Code: [Select]
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.

ninja2

#5
Apr 20, 2012, 11:51 pm Last Edit: Apr 20, 2012, 11:54 pm by ninja2 Reason: 1
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  :) but I'll let you know what I find

mrTee

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

mrTee

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

Code: [Select]
#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;
}

mrTee

Since no big issues have arisen, I have added the library to the playground: http://arduino.cc/playground/Code/DCF77

Schueler

Hi,

I am building a Nixie clock ( who isn't...  ;) ) 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!!!!

Code: [Select]
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     ;)

mrTee

#10
Jul 03, 2012, 09:17 pm Last Edit: Jul 25, 2012, 04:18 pm by mrTee Reason: 1
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.   :)

sarzillabruno

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

mrTee

#12
Jul 25, 2012, 10:26 am Last Edit: Jul 25, 2012, 10:27 am by mrTee Reason: 1
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

Code: [Select]
#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);

dreyfus

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:
Code: [Select]
        int sensorValue = digitalRead(dCF77Pin);

to
Code: [Select]
        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.

mrTee

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.

Go Up