Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

RTC Pi storing double values in RTC memory

A tutorial showing how to use the internal memory for storing double values on the RTC Pi

This demo shows how to write to and read from the internal battery-backed memory on the DS1307 RTC chip as used on the RTC Pi and Expander Pi.

Step 1: With your Raspberry Pi switched off, install the RTC Pi on the Raspberry Pi GPIO port and insert a CR2032 coin battery into the battery holder. Using the RTC Pi without a battery installed may damage the RTC Pi and will stop it from appearing on the I2C bus.

Step 2: Follow the instructions on how to install and configure I2C on Raspbian Linux.

We will create a new Python program file for this tutorial called demo_memory_double.py. You can use your favourite text editor to write the program. You can find a complete example of demo_memory_double.py in the ABElectronics_Python_Libraries/RTCPi/demos/ folder.

At the top of your program, you must import the RTC class from the RTCPi library and import struct.

from RTCPi import RTC
import struct

We need some helper functions to convert an integer into an eight-byte array and also to convert an eight-byte array into a double

def double_to_array(val):
    # convert a double into an eight byte array
    buf = bytearray(struct.pack('d', val))
    x = [0,0,0,0,0,0,0,0]
    for i in range(0, 8):
    	x[i] = buf[i]
    return x


def array_to_double(val):
    # convert an eight byte array into a double
    dval, = struct.unpack('d', bytearray(val))
    return (dval)

Create a new instance of the RTC class

rtc = RTC()

We will define an integer to be written to the RTC memory

value = 0.0005

Now, convert the number into an array of bytes

writearray = double_to_array(value)

We now write the array to the RTC memory

rtc.write_memory(0x08, writearray)

We can now read eight bytes from the RTC memory into an array to check that the data has been saved.

read_array = rtc.read_memory(0x08, 8)

To convert it back, you must combine the array values into a double and print it.

print (array_to_double(read_array))

To run the demo in a console window, enter the following:

python3 demo-memory-double.py

The complete code for this demo is as follows:

#!/usr/bin/env python

from RTCPi import RTC
import struct

"""
================================================
ABElectronics RTC Pi real-time clock | RTC memory demo

run with: python3 demo_memory_double.py
================================================

This demo shows how to write to and read from the internal battery 
backed memory on the DS1307 RTC chip
"""



def double_to_array(val):
    # convert a double into an eight byte array
    buf = bytearray(struct.pack('d', val))
    x = [0,0,0,0,0,0,0,0]
    for i in range(0, 8):
    	x[i] = buf[i]
    return x

def array_to_double(val):
    # convert an eight byte array into a double
    dval, = struct.unpack('d', bytearray(val))
    return (dval)

# create a new instance of the RTC class
rtc = RTC()

# number to be written to the RTC memory
value = 0.0005

# convert the number into an array of bytes
writearray = double_to_array(value)

# write the array to the RTC memory
rtc.write_memory(0x08, writearray)

# read eight bytes from the RTC memory into an array
read_array = rtc.read_memory(0x08, 8)

# combine the array values into an number and print it
print (array_to_double(read_array))

Was this article helpful? Yes No

Please tell us how we can improve this article:

Submit
Created 17/06/2017 | Last Updated: 11/11/2023

Related Expansion Boards

Order a RTC Pi Photo of RTC Pi
RTC Pi

Real-time clock for the Raspberry Pi

£11.39 Ex VAT: £9.49
Add to Basket

Related Articles

RTC Pi setup on Raspberry Pi OS
A tutorial showing how to use the RTC Pi on a Raspberry Pi
RTC Pi with Raspberry Pi Pico
Using the RTC Pi with Raspberry Pi Pico and MicroPython
RTC Pi storing integers in RTC memory
A tutorial showing how to use the internal memory for storing integers on the RTC Pi
RTC Pi storing double values in RTC memory
A tutorial showing how to use the internal memory for storing double values on the RTC Pi
RTC Pi and Arduino
Adding a Real Time clock to an Arduino