EEPROM - VCSFA-MARS/TSLPB GitHub Wiki

The TSLPB has a dedicated EEPROM memory module you can use to store data. Since it's nonvolatile, the data will persist even if your ThinSat powers down.

Check out the example sketch EEPROM to see these functions in action.

File > Examples > ThinSat Program TSLPB Library > EEPROM

Using the EEPROM

The TSLPB Library includes 4 helpful functions for using the EEPROM:

  • getMemByte()
  • putMemByte()
  • readMemVar()
  • writeMemVar()

Accessing individual registers

The EEPROM chip stores data in slots called registers. Each register can hold one byte of data (8 individual 1s or 0s). The registers are numbered starting at 0.

All of the EEPROM functions take the register as the first argument. If you are passing a variable as the register argument, it needs to be of type word, or a 2-byte unsigned integer like uint16_t.

Writing to a register

If you wanted to store the value 255 in the first register, you could use the following code:

#include "TSLPB.h"

TSLPB pb;

void setup() {
    pb.begin();
}

void loop() {
    pb.putMemByte(0, 255);
    delay(60000);
}

The first argument of putMemByte() is the register, and the second is the value you want to store. You could also use hexadecimal notation for either or both of the arguments:

pb.putMemByte(0x00, 0xFF);

Reading from a register

getMemByte() returns a byte (which is an unsigned 8 bit integer uint_8)

If you want to read the data stored in register 0, and save it to a variable called myByte you can use the following code:

byte myByte = pb.getMemByte(0x00);

You can also use the return value directly. If you want to print the contents of register 0 to the Diagnostic port, you could use this code:

Serial.print("Register 0 contains: ");
Serial.println(pb.getMemByte(0x00));

And when you run the program, you will see the following on your serial monitor:

Register 0 contains: 255

Reading and Writing whole variables

The functions above allow you to read and write one byte at a time, which can be useful. But many data types need more than one byte. Instead of writing your own code to break your data into individual bytes and save/load them, you can use these built-in library functions!

Saving a variable's contents to EEPROM

Let's say you have a floating point variable, like 3.14159.

float pi = 3.14159;

floats take up 4 bytes of memory (and they use a cool scheme to store their values without huge losses in precision across an incredibly wide range of values).

With the TSLPB Library, you can save this value in one line:

pb.writeMemVar(1, pi);

That will write your variable to the EEPROM chip starting at register 1. Since you passed a float, writeMemVar() will write 4 bytes, filling registers 1, 2, 3, and 4.

**Warning: ** writeMemVar() doesn't know if you have other data on the EEPROM, and it will overwrite whatever was there. So it is important to know how much data your variables take up to prevent overwriting registers that you are already using to store data.

Reading the EEPROM into a variable

Now that we know how to save data types other than bytes, let's work on reading them back. If you have run the previous code, you will have some data in your EEPROM.

Let's say you want to retrieve a floating point value that starts at register 1.

float newFloat;
pb.readMemVar(1, newFloat);
Serial.print("newFloat now contains: ");
Serial.println(newFloat);

When that code runs, you will see the following on your serial monitor:

newFloat now contains: 3.14

Easy as pi(e)!

Note: Serial.print formats floating point values to truncate some of the decimal places.