Lesson 14. EEPROM in Arduino. Data integrity control.

EEPROM Arduino

The lesson describes how to work with the internal EEPROM of the Arduino board, about data integrity monitoring during storage and transmission.

Previous lesson     List of lessons     Next lesson

Internal EEPROM in Arduino system.

EEPROM is non-volatile memory. It stands for electrically erasable programmable read-only memory. It is used to store data that must be saved when power is turned off.
Arduino boards have EEPROM of different volumes depending on the type of microcontroller used.

Microcontroller Volume Internal EEPROM
ATmega328 1024 bytes
ATmega8, ATmega168 512 bytes
ATmega1280, ATmega2560 4096 bytes

This amount of memory is enough for storing modes, process parameters, coefficients, etc.

  • The principle of operation of EEPROM is based on the creation of electric charge in the dielectric of a semiconductor structure. The charges are not stored forever, but the developers guarantee 20 years.
  • EEPROM has a limited number of write cycles, usually at least 100,000.
  • It takes a rather long time to write information to the EEPROM, about 3 ms.
  • Reading EEPROM occurs without delay and the memory resource does not reduce.

 

EEPROM library.

To work with non-volatile memory in Arduino there is EEPROM library. It has two main functions for writing and reading a byte in an EEPROM.

byte read (int address)

The function returns the byte read to address.

#include <EEPROM.h>
byte dt = EEPROM.read (15); // read byte from adress 15

void write (int address, byte value)

Writes a byte to the EEPROM. Recording is performed in 3.3 ms. 100,000 write cycles guaranteed.

#include <EEPROM.h>
EEPROM.write (15, 0); // write 0 to adress 15

 

The program for checking operations with EEPROM.

Let's write a simple program to test the operation of the EEPROM. The program must record up to 16 characters received from the serial port and output in a cycle 16 characters read from the EEPROM. Using the Arduino IDE port monitor, we can write data to the EEPROM and monitor the contents of non-volatile memory.

// test the operation of the EEPROM
#include <EEPROM.h>
int i, d;

void setup() {
  Serial.begin(9600); // initialize the port, rate 9600
}

void loop() {
  // read EEPROM and output 16 data to serial port
  Serial.println();
  Serial.print("EEPROM= ");
  i= 0; while(i < 16) {
    Serial.print((char)EEPROM.read(i));
    i++;
  }

  // check if there is data to write
  if ( Serial.available() != 0 ) {
    delay(50); // waiting for the end of data reception

    // write to eeprom
    i= 0; while(i < 20) {
      d= Serial.read();
      if (d == -1) d= ' '; // if characters are out, fill in with spaces
      EEPROM.write(i, (byte)d); // write to eeprom
      i++;
    }
  }
  delay(500);
}

We load the program into the Arduino board, we check it. Open the serial port monitor, send data to the board: ”EEPROM check”.

Test window

Data is recorded in the EEPROM and cyclically displayed in the monitor window. If we disconnect the board from the computer and then reconnect, the data will continue to be sent to the computer. They are stored in non-volatile memory and do not change when the power is turned off.

 

Data integrity control.

EEPROM usually stores data absolutely necessary for the program to work. For example, if you are making a temperature controller, then this could be: a predetermined temperature, coefficients of regulators, parameters of protective functions, etc. Imagine that these parameters will be corrupted, and the regulator will continue to work. It can turn on the heating element at full power and set an unacceptably high temperature. If the coefficients are incorrect, the regulator may enter the oscillation mode.
Protective functions will stop working. There will be big trouble.

But the situation when in the EEPROM there may be inaccurate data is quite real.

  • When the device is first turned on, the data in the EEPROM is invalid. They just no one has installed yet.
  • At the time of writing data to the EEPROM, the power to the board may be turned off, then some of the data will be written, and some will not have time.
  • EEPROM has a limited number of write cycles, and this property can also lead to data corruption in this memory.

Therefore, it is extremely important to be sure that reliable data is stored in the EEPROM. In case they are erroneous, the program must take certain actions. It may be:

  • use a copy of the data from the EEPROM;
  • use data specially created for emergency mode;
  • turn off the device;
  • signal about the error;
  • and many other options.

Data integrity is an indicator that the data has not been changed during storage, transfer, display, etc. Data integrity can be checked in any situation potentially dangerous for data destruction. Usually, data reliability control is carried out:

  • In non-volatile memory (EEPROM, FLASH, HDD ...).
  • When transferring data (serial interfaces, WiFi, GSM, TCP / IP ...).
  • In RAM for sensitive data.
  • Some components (such as the DS18B20 sensor) have a data protocol with integrity monitoring.

For integrity monitoring, a control code is added to the data block. This code is calculated according to a specific algorithm when writing data. When checking the data, the code is re-calculated and compared with the code generated during the recording. If the codes do not match - the data is erroneous.

The control code calculation algorithm determines the probability of determining the data error. There are a large number of control code generation algorithms: cyclic codes, various hashing functions. But the easiest way to calculate a control code is a checksum.

A simple mathematical sum of all bytes of a data block is made. Such an algorithm requires a minimum of resources for implementation, and at the same time it allows a rather high probability to determine erroneous data in the case of small amounts of information. Checksum may have different bit depth. In the minimum version, this is one byte. In this case, the result of the sum overflows, which reduces probability of detecting errors.

I always apply an XOR operation with a code like E5h to the calculated checksum. This allows you to exclude a very likely situation when all data is equal to 0. The sum of 0 is 0. Therefore, if the data block is mistakenly zeroed (and this happens), then a simple sum will not detect this error. And when the checksum for all 0 equals E5h, the error will be detected.

Let's add data integrity control to the previous program.

// test the operation of the EEPROM
#include <EEPROM.h>
int i, d;
byte sum; // check sum

void setup() {
  Serial.begin(9600); // initialize the port, rate 9600
}

void loop() {
  // calculation of the checksum
  sum= 0;
  i= 0; while(i < 16) {
    sum += EEPROM.read(i);
    i++;
  }
  // checksum check
  if ( (sum^0xe5) == EEPROM.read(i)) {
    // checksum is correct

    // read EEPROM and output 16 data to serial port
    Serial.println();
    Serial.print("EEPROM= ");
    i= 0; while(i < 16) {
      Serial.print((char)EEPROM.read(i));
      i++;
    }
  }
  else {
    // bad checksum
    Serial.println();
    Serial.print("EEPROM= data error");
  }

  // check if there is data to write
  if ( Serial.available() != 0 ) {
    delay(50); // waiting for the end of data reception

    // write to eeprom
    sum= 0;
    i= 0; while(i < 16) {
      d= Serial.read();
      if (d == -1) d= ' '; // if characters are out, fill in with spaces
      EEPROM.write(i, (byte)d); // write eeprom
      sum += (byte)d; // calculation of the checksum
      i++;
    }
    EEPROM.write(i, sum ^ 0xe5); // // write checksum
  }
  delay(500);
}

I will only note that the program accepts not 16, but 14 characters, since The port monitor adds the line breaks \ r and \ n to each line.

Download the program and launch the monitor.

Test window

In the window, data error messages are running. We have not yet loaded any data into the EEPROM, and the algorithm recognizes this situation.

Send a string, for example, "EEPROM test". Now the window displays data from the EEPROM.

Test window

In the same way, it is possible to protect the integrity of data during transmission over the serial port. Any impulse noise can distort the signal in the communication cable and cause a data error. Of course, the program for receiving data on a computer must support a protocol with a checksum. The Arduino IDE Port Monitor does not have this feature.

In the next lesson we will learn what are pointers in the C language for Arduino, we will learn how to write data of different types (int, float ...) in EEPROM.

Previous lesson     List of lessons     Next lesson

Leave a Reply

Your email address will not be published. Required fields are marked *