How To Store Data In Arduino

Table of contents:

How To Store Data In Arduino
How To Store Data In Arduino

Video: How To Store Data In Arduino

Video: How To Store Data In Arduino
Video: Arduino Workshop - Chapter 4 - Using EEPROM 2024, December
Anonim

Arduino boards have several types of memory. First, it is static RAM (random access memory), which is used to store variables during program execution. Secondly, it's the flash memory that stores the sketches you've written. And thirdly, it is an EEPROM that can be used to permanently store information. The first type of memory is volatile, it loses all information after rebooting the Arduino. The second two types of memory store information until it is overwritten with a new one, even after the power is turned off. The last type of memory - EEPROM - allows data to be written, stored and read as needed. We will consider this memory now.

Storing data in EEPROM Arduino
Storing data in EEPROM Arduino

Necessary

  • - Arduino;
  • - computer.

Instructions

Step 1

EEPROM stands for Electrically Erasable Programmable Read-Only Memory, i.e. electrically erasable read-only memory. Data in this memory can be stored for tens of years after a power outage. The number of rewriting cycles is on the order of several million times.

The amount of EEPROM memory in Arduino is quite limited: for boards based on the ATmega328 microcontroller (for example, Arduino UNO and Nano), the amount of memory is 1 KB, for boards based on ATmega168 and ATmega8 - 512 bytes, for ATmega2560 and ATmega1280 - 4 KB.

Step 2

To work with the EEPROM for Arduino, a special library has been written, which is included in the Arduino IDE by default. The library contains the following features.

read (address) - reads 1 byte from EEPROM; address - the address where the data is read from (cell starting from 0);

write (address, value) - writes the value value (1 byte, number from 0 to 255) into memory at address;

update (address, value) - replaces the value at address if its old content differs from the new one;

get (address, data) - reads data of the specified type from memory at address;

put (address, data) - writes data of the specified type to memory at address;

EEPROM [address] - allows you to use the "EEPROM" identifier as an array to write data to and read from memory.

To use the library in the sketch, we include it with the #include EEPROM.h directive.

Step 3

Let's write two integers to the EEPROM and then read them from the EEPROM and output them to the serial port.

There are no problems with numbers from 0 to 255, they occupy just 1 byte of memory and are written to the desired location using the EEPROM.write () function.

If the number is greater than 255, then using the operators highByte () and lowByte () it must be divided by bytes and each byte must be written to its own cell. The maximum number is 65536 (or 2 ^ 16).

Look, the serial port monitor in cell 0 simply outputs a number less than 255. A large number 789 is stored in cells 1 and 2. At the same time, cell 1 stores the overflow factor 3, and cell 2 stores the missing number 21 (i.e. 789 = 3 * 256 + 21). To reassemble a large number, parsed into bytes, there is the word () function: int val = word (hi, low), where hi and low are the values of the high and low bytes.

In all other cells that we have never written down, numbers 255 are stored.

Writing integers to EEPROM Arduino
Writing integers to EEPROM Arduino

Step 4

To write floating point numbers and strings, use the EEPROM.put () method, and to read, use EEPROM.get ().

In the setup () procedure, we first write the floating point number f. Then we move by the number of memory cells occupied by the float type and write a char string with a capacity of 20 cells.

In the loop () procedure we will read all memory cells and try to decrypt them first as "float" type, and then as "char" type, and output the result to the serial port.

You can see that the value in cells 0 through 3 was correctly defined as a floating point number, and starting from the 4th - as a string.

The resulting values ovf (overflow) and nan (not a number) indicate that the number cannot be correctly converted to a floating point number. If you know exactly what type of data which memory cells are occupying, then you will not have any problems.

Writing floating point numbers and strings to EEPROM Arduino
Writing floating point numbers and strings to EEPROM Arduino

Step 5

A very convenient feature is to refer to memory cells as elements of an EEPROM array. In this sketch, in the setup () procedure, we will first write data to the first 4 bytes, and in the loop () procedure, every minute we will read data from all cells and output them to the serial port.

Recommended: