Project tutorial

DCF77 OLED Clock © CC BY-SA

Pick the time from a DCF77 radio signal and show it on an OLED display.

  • 605 views
  • 0 comments
  • 1 respect

Components and supplies

Apps and online services

About this project

I've played around with individual components and this clock is my first practical device I made. It uses the DCF77 signal from a radio tower in Munich, Germany. The signal can only be received in Europe. The time is then shown on an OLED display using the U8g2 graphical library.

Schematics

Schema
Dcf77 oled clock bb bjebwungsg
Fritzing scheme
dcf77-oled-clock_3Z0ahL00Cl.fzz

Code

dcf77-oled-clock.inoArduino
/* Clock by Edwin Martin <edwin@bitstorm.org>    */
/* Using DCF77 receiver and SSD1306 128X64 OLED  */
/* Copyright 2017 Edwin Martin. MIT license      */

#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
#include "DCF77.h"
#include "TimeLib.h"

/* Configure u8g2 for SSD1306 128X64 OLED */
U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI u8g2(U8G2_R0, 12, 11, 8, 9, 10);

#define DCF_PIN 2
#define DCF_INTERRUPT 0

/* Configure DCF77 reveiver */
DCF77 DCF = DCF77(DCF_PIN,DCF_INTERRUPT);

boolean show_init = true;
char time_string[6];
char date_string[20];
const char* weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const char* months[] = {"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."};
int min_now, min_old = -1;

void setup(void) {
  Serial.begin(9600); 
  Serial.println("Initialising DCF77");
  DCF.Start();
  u8g2.begin();
}

void loop(void) {
  time_t DCFtime = DCF.getTime();
  if (DCFtime != 0) {
    Serial.println("Time is updated");
    setTime(DCFtime);
    show_init = false;
  }

  min_now = minute();
  if (min_now != min_old) {
    set_time_string(hour(), min_now, time_string);
    set_date_string(date_string);

    u8g2.firstPage();
    do {
      if (show_init) {
        u8g2.setFont(u8g2_font_mercutio_basic_nbp_tf);
        u8g2.setCursor(15, 40);
        u8g2.print("Waiting for signal...");
      } else {
        u8g2.setFont(u8g2_font_inb30_mn);
        u8g2.setCursor(64 - u8g2.getStrWidth(time_string) / 2, 33);
        u8g2.print(time_string);
        u8g2.setFont(u8g2_font_mercutio_basic_nbp_tf);
        u8g2.setCursor(64 - u8g2.getStrWidth(date_string) / 2, 60);
        u8g2.print(date_string);
      }
    } while (u8g2.nextPage());

    min_old = min_now;
  }

  delay(1000);
}

void set_time_string(int hour_num, int min_num, char time_string[]) {
  int position = 0;
  if (hour_num / 10 > 0) {
    time_string[position++] = '0' + hour_num / 10;
  }
  time_string[position++] = '0' + hour_num % 10;
  time_string[position++] = ':';
  time_string[position++] = '0' + min_num / 10;
  time_string[position++] = '0' + min_num % 10;
  time_string[position++] = '\0';
}

/* British notation like "Thursday, 24 Aug."
 * because because of European DCF77
 */
void set_date_string(char date_string[]) {
  int day_num = day();
  int month_num = month();
  int year_num = year();
  int weekday_num = weekday();
  char day_of_month[3];
  int position = 0;

  if (weekday_num == 0 || month_num == 0) {
    return;
  }
  
  strcpy(date_string, weekdays[weekday_num - 1]);
  strcat(date_string, ", ");
  if (day_num / 10 > 0) {
    day_of_month[position++] = '0' + day_num / 10;
  }
  day_of_month[position++] = '0' + day_num % 10;
  day_of_month[position++] = '\0';
  strcat(date_string, day_of_month);
  strcat(date_string, " ");
  strcat(date_string, months[month_num - 1]);
}

Comments

Similar projects you might like

LoRa Gateway for DeviceHive

Project tutorial by DeviceHive IoT team

  • 526 views
  • 1 comment
  • 13 respects

Carfox: A Device to Find Them All

Project tutorial by Luis Roda Sánchez

  • 251 views
  • 0 comments
  • 6 respects

Pac-Man LED Pixel Panel Costume

Project tutorial by Ben Muller

  • 3,558 views
  • 3 comments
  • 62 respects

Raspberry Pi and Arduino Laptop

Project tutorial by Dante Roumega

  • 16,932 views
  • 6 comments
  • 42 respects

IoT Bird Feeder with Sigfox and Tweeter

Project showcase by Gaël Porté

  • 210 views
  • 0 comments
  • 3 respects

Arduino-Based Automatic Guitar Tuner

Project tutorial by Ben Overzat

  • 496 views
  • 0 comments
  • 7 respects
Add projectSign up / Login