create new tag
view all tags

The Realtime Clock


The real time clock and eeprom module uses the DS1307 real time clock chip which can be programmed using its I2C bus interface. The pigpio library contains I2C access functions perfectly adapted to programming the chip.

In this exercise we will write a program that allows to set the Realtime Clock (RTC) and to read the current date and time. In order to set the time we must be able to transmit time and date value into the program which we do through program arguments. This is done through the getopt function, which is part of the C library.

rtcfront.png rtcCircuit.png
RTC circuit board Circuit diagram

You can connect the RTC board to the Raspberry Pi either on P1 or P2. DS is the signal line of a DS18B20 digital temperature chip, which is not mounted on the board and therefore not used. You just neew Vcc and ground and the SCL and SDA signals from the I2C bus. The DS1307 RTC chip is the bottom one, right to the 32 kHz quartz (metallic cylinder). The other chip is an Atmel AT24C32 I2C EEPROM, which we use in another exercise.

Getting access to the chip

The RTC card uses a DS1307 realtime clock chip from Maxim. It is interfaced to the Raspberry Pi through the I2C bus. From the datasheet we can find out its default I2C address which we verify with the i2cdetect 1 command (the chip is on bus 1).

Try to see if you can access the chip correctly by just setting a single register and reading it back.


In order to allow the user of your program (which is probably yourself) to enter the date and time setting, you must provide a method to define which parameter is to be set (e.g. sec or min or h) and to which value it is to be set. This can be done using command line arguments which can be treated with the getopt library call. Have a look on the WEB how this is done.

The full program

The full program will allows to set any of the RTC parameters and/or to read back date and time. Since the RTC uses a battery, once set it will keep the correct time for very long.

The is a function in my own solution, which prints a "usage" message, explaining how the program is to be used:

void printUsage()
  printf("The rtc program, called without parameters, will print the\n");
  printf("current date and time\n");
  printf("If called with arguments it will set the realtime clock\n");
  printf("arguments are:\n");
  printf("   long argument, short argument, value\n");
  printf("   --help,        ?               n/a\n");
  printf("   --seconds      -s              0..59\n");
  printf("   --minutes      -m              0..59\n");
  printf("   --weekday      -w              1..7\n");
  printf("                                  or \"mon\",\"tue\",\"wed\"...\n");
  printf("   --date         -D              date within month\n");
  printf("   --month        -M              1..12\n");
  printf("                                  or \"jan\",\"feb\",\"mar\"...\n");
  printf("   --year         -Y              0..99\n");

-- Uli Raich - 2017-04-06


Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng rtcCircuit.png r1 manage 37.6 K 2017-04-06 - 11:59 UnknownUser  
PNGpng rtcfront.png r1 manage 2290.3 K 2017-04-06 - 11:45 UnknownUser  
Topic revision: r1 - 2017-04-06 - uli
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback