# Knowledge Base ## RTC Pi storing integers in RTC memory

### A tutorial showing how to use the internal memory for storing integers 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 with Python.

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_int.py. You can use your favourite text editor to write the program. You can find a complete example of demo_memory_int.py in the ABElectronics_Python_Libraries/RTCPi/demo folder.

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

```from RTCPi import RTC

```

We need some helper functions to convert an integer into a four-byte array and a four-byte array into an integer.

```def int_to_array(val):
# convert an integer into a four byte array
x = [0, 0, 0, 0]
x = val & 0xFF
val >>= 8
x = val & 0xFF
val >>= 8
x = val & 0xFF
val >>= 8
x = val & 0xFF
return x

def array_to_int(x):
# convert a four byte array into an integer
val = (x<<24) + (x<<16) + (x<<8) + x
return val
```

Create a new instance of the RTC class

`rtc = RTC()`

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

`writeval = 176247`

And now, convert the integer into an array of bytes

`writearray = int_to_array(writeval)`

Next, write the array to the RTC memory

`rtc.write_memory(0x08, writearray)`

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

`read_array = rtc.read_memory(0x08, 4)`

Print the individual array values to the console.

`print (read_array, read_array, read_array, read_array)`

To convert it back to an integer, you must combine the array values into an integer and print it.

`print (array_to_int(read_array))`

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

`python3 demo-memory-int.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_int.py
================================================

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

def int_to_array(val):
# convert an integer into a four byte array
x = [0, 0, 0, 0]
x = val & 0xFF
val >>= 8
x = val & 0xFF
val >>= 8
x = val & 0xFF
val >>= 8
x = val & 0xFF
return x

def array_to_int(x):
# convert a four byte array into an integer
val = (x<<24) + (x<<16) + (x<<8) + x
return val

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

# integer to be written to the RTC memory
writeval = 176247

# convert the integer into an array of bytes
writearray = int_to_array(writeval)

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

# read four bytes from the RTC memory into an array

# print the individual array values

# combine the array values into an integer and print it

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

### 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