In compliance with high industry standards

Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Storing the MAC of your PLC Arduino in the EEPROM (non-volatile memory)

Storing data in the EEPROM (non-volatile memory) of your controller Arduino

      Introduction

      All industrial Arduino programmable logic controllers have 3 different types of memory:

      • Flash: non-volatile, where we recorded the sketch (including the bootlader).

      • SRAM (Static Random Acces Memory): volatile, where variables are stored during operation. 

      • EEPROM: non-volatile, that we can use to store information between restarts. 

      If we store values in the flash or SRAM memory, these will be lost once the industrial PLC controller is restarted, but sometimes we want certain values to be retained between restarts. For example, unique identifiers, calibration values, measurements and dates or times to make dataloggers, save a counter, or know what the state of the processor was when it lost power, among many others.

      On this occasion, we will save the MAC address in our EEPROM, but the process to store any other type of data in the EEPROM is the same.

      EEPROM Memory
                          Requirements

                          Requirements

                          Hardware requirements:  

                          All our PLCs Arduino have an internal EEPROM. You can check all the range of our industrial controllers for industrial automation that allow you to store data in the EEPROM:

                          Software requirements: 


                          Characteristics of the memories in a PLC Arduino

                          • Flash memory (program space) is where the Arduino sketch is stored.

                          • SRAM (Static Random Access Memory) is where the sketch creates and manipulates variables when it runs.

                          • EEPROM is a memory space that programmers can use to store long-term information.


                          Flash memory and EEPROM memory are non-volatile (the information persists after the power is turned off). SRAM memory is volatile and will be lost when the power is cycled.

                          The memory sizes are different depending on the Arduino-based PLC model you have

                          For Ardbox Family Models: 

                           Flash 32k bytes (of which 4k is used for the bootloader)
                           SRAM 2,5k bytes
                           EEPROM 1k byte

                          For M-Duino Family Models:

                           Flash 256k bytes (of which 8k is used for the bootloader)
                           SRAM 8k bytes
                           EEPROM 4k bytes

                          Characteristics of the memories in a PLC Arduino
                          EEPROM main characteristics

                          EEPROM main characteristics

                          EEPROM memory has its own characteristics and peculiarities that distinguish it from other memories. First and foremost, as we have seen before, it is non-volatile, so it keeps the values ​​stored when the power is lost.

                          A disadvantage of EEPROM memory is that it is much slower than SRAM memory. The process of writing a cell (byte) costs around 3.3 ms. The reading process is much faster (although it is still slower than the SRAM), reading 1024 bytes costs around 0.3ms, that is, 10,000 times faster than writing.

                          Another peculiarity of EEPROM memory is that it has a limited life, which is reduced with each write operation. There are no limits for reading operations. 

                          The specifications guarantee that each cell has a lifespan of at least 100,000. Although in practice it can be much higher, up to one million operations, over 100,000 operation is not guaranteed. 

                          It is sensible to say that EEPROM memory is designed to write with long times between them, not a constant use of it


                          Software

                          The Arduino Standard IDE incorporates an EEPROM.h library that contains the functions necessary to manipulate Arduino's non-volatile memory.

                          The simplest functions are the Read and Write function that, respectively, read and write a byte in a memory address. The memory address may have values from 0 to N-1, where N is the number of available bytes (0 to 1023 in Ardbox Family models and 0 to 4095 in M-Duino Family models).

                          Software

                          The next functions record a single byte to the EEPROM

                          //Reads a unique byte from address "addr"
                          EEPROM.Read(addr);
                          //Writes a unique byte from address "addr"
                          EEPROM.Write(addr);

                          Frequently, we will need to save variables that are larger than one byte. For this we have the functions Put, Get and Update, which are the ones we will use most frequently.

                          // Functions for complete variables (takes into account the size of the variable)
                          EEPROM.Put(address, variable)

                          // Read a variable in the address addr
                          EEPROM.Get(address, variable) 

                          //Update the value of a variable, that is, read it first, and only save it if its value is different from the one we are going to save. This favors reducing the number of writes, and extending the useful life of the memory.
                          EEPROM.Update(addr,variable)

                          Example Sketch

                          The random MAC address is generated on first boot. On each subsequent boot after that it is read back out of EEPROM to ensure it stays the same.

                          #include <SPI.h>
                          #include <Ethernet.h>
                          #include <EEPROM.h>
                          
                          byte mac[6] = { 0xBA, 0xBE, 0x00, 0x00, 0x00, 0x00 };
                          char macstr[18];
                          
                          void setup() {
                            Serial.begin(9600);
                            // Random MAC address stored in EEPROM
                            if (EEPROM.read(1) == '#') {
                              for (int i = 2; i < 6; i++) {
                                mac[i] = EEPROM.read(i);
                              }
                            } else {
                              randomSeed(analogRead(0));
                              for (int i = 2; i < 6; i++) {
                                mac[i] = random(0, 255);
                                EEPROM.write(i, mac[i]);
                              }
                              EEPROM.write(1, '#');
                            }
                            snprintf(macstr, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
                          
                            // Start up networking
                            Serial.print("DHCP (");
                            Serial.print(macstr);
                            Serial.print(")...");
                            Ethernet.begin(mac);
                            Serial.print("success: ");
                            Serial.println(Ethernet.localIP());
                          }
                          
                          void loop() {
                          
                          }

                          Looking for your ideal PLC?


                          Take a look at this product comparison with other industrial controllers.

                          We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


                          PLC comparison >>

                          Do you want more information?

                          Just fill the form!

                          Tell me more!