www.NewsDownload.co.uk Page 28

Real Time Clock Python Source Code

2015-08-27 00:51 By Jason Birch

Python source code to talk to a Real Time Clock DS1302 connected to the Raspberry Pi GPIO pins.

VIDEO
The video here demonstrates the project which this article describes how to build.

The article breaks the project down into several stages:

  • Circuit
  • Build
  • Software
  • Project Costing

Reference:


Circuit for RTC Module

Circuit
The DS1302 can operate from a supply voltage of 3V3 directly from the Raspberry Pi. The first five pins in a row, from pin 1, can be used to connect to the RTC module: 3V3, GPIO 2, 3, 4 and GND.

There is a CE pin which enables the module for use. When this pin is low, the other GPIO pins can be reused for other purposes. If you are going to do this you must ensure the pins are only used for one purpose at a time. This can be tricky so I suggest you dedicate the pins for this module unless you know how to reuse pins when they are not in use.

It is easy to reassign the GPIO pins being used in the Python class, so if these pins need to be used for another purpose, just edit the assignments at the top of the Python class file.




The crystal runs at 32.768KHz. If repeatedly divided by two this can be reduced to a 1Hz clock, one cycle per second, which is why this frequency is used. When computers move the bits of a number one position to the right, this divides the number by two. For example 52 decimal is 110100. Shifting the bits one to the right gives 11010 which is 26 decimal, half of 52.

Use this principle on 32768Hz. 32768 / 2 = 16384 / 2 = 8192 / 2 = 4096 / 2 = 2048 / 2 = 1024 / 2 = 512 / 2 = 256 / 2 = 128 / 2 = 64 / 2 = 32 / 2 = 16 / 2 = 8 / 2 = 4 / 2 = 2 / 2 = 1. Thus providing a signal which occurs exactly once every second, which can be used to count the seconds in a date and time.

The DS1302 has a trickle charge circuit built in, if the backup battery used is rechargeable, the DS1302 can be configured to trickle charge the backup battery while the supply voltage is present. In the Python code this feature has been turned off as a non-rechargeable coin cell battery is being used.

GPIO Pin Allocation
Raspberry Pi DS1302
3V3 Vcc (Pin 1)
SCLK (GPIO 2) SCLK (Pin 7)
CE (GPIO 3) CE (Pin 5)
I/O (GPIO 4) I/O (Pin 6)
GND GND (Pin 4)

A 3V battery is required to maintain the date and time when no power supply is present. When the date and time is to be written to or read from the device, a 3V3 supply is required. The data is transferred via a three wire bi-directional serial interface, using GPIO pins on the Raspberry Pi.

Software
The Python class RTC_DS1302.py provides the following functions to control the module. There are additional functions in the class which are used internally to talk to the module:

WriteRAM(Data)
The module has 31 bytes of RAM which can be written to and read. You can write binary or character data to it. This maybe useful if you wanted to take the module from device to device, or maybe as a secret location to store a small encrypted message. Call this function to write some data to the RAM.

ReadRAM()
Call this function to retrieve the data written in the 31 bytes of RAM from the module.

WriteDateTime(Year, Month, Day, DayOfWeek, Hour, Minute, Second)
Call this function to set the modules date and time. This only needs to be called initially and when replacing the backup battery.

ReadDateTime(DateTime)
This function returns the data and time in a string and the individual values in the structure:
DateTime = { "Year":0, "Month":0, "Day":0, "DayOfWeek":0, "Hour":0, "Minute":0, "Second":0 }

The Python application PythonRTC.py is a demonstration of how to call the functions in the Python class. Run the application by starting the PythonRTC.sh shell script, this will suppress warning messages being displayed.
Build
On this project a pre built circuit was cheaper to buy on eBay than a single IC. Three 1K resistors where placed inline with each of the wires for the serial interface. This is just to limit current and protect the Raspberry Pi GPIO pins, in case of a short circuit.

Source Code GIT Repository.

Install
Download or clone the git repository.

To download, open a terminal window and type the following:
wget https://github.com/BirchJD/RTC_DS1302/archive/master.zip

Unzip the source code:
unzip master.zip

Run the demonstration application:
cd RTC_DS1302-master
chmod +x PythonRTC.sh
./PythonRTC.sh

The demonstration application will write to the RTC RAM, and then read back what has been written. Then will set the RTC date and time, and read back the date and time from the RTC.

Project Cost
Item Reference Qty Each Cost
RTC Module eBay 1 £1.50 £1.50
CR2032 Battery Poundland 1 £1.00 £1.00
1K Resistor Carbon film 1/4W 5% BitsBox 3 £0.05 £0.15
5-Way Single Row Socket BitsBox 2 £0.26 £0.52
TOTAL £3.17

Loading...