Index
1. What is an EEPROM
2. Requirements
3. EEPROM.write() example
4. EEPROM.read() example
5. EEPROM.put() example
6. EEPROM.get() example
What is an EEPROM
EEPROM is a memory whose values are kept when the Arduino board of the PLC controller Arduino automation is turned off. Below, there are examples with the EEPROM.write(), EEPROM.read(), EEPROM.put() and EEPROM.get() function. They show how to write and read from the EEPROM.
In our programmable logic controllers for industrial automation,
Ardbox PLC Arduino >>> has 1KB
M-Duino PLC Arduino >>> has 4KB of EEPROM.
Requirements
- Ethernet or 20 I/Os PLC:
Ethernet PLC >>>
- Arduino EEPROM Library:
EEPROM.h Arduino IDE Library >>>
Software
In this Arduino EEPROM example we will see how to use functions EEPROM.write(), EEPROM.read(), EEPROM.put() and EEPROM.get().
EEPROM.write() example
The first Arduino EEPROM example code is implemented how to store a byte from analog input 0 into the EEPROM memory using the EEPROM.write(). EEPROM.write() store a byte to the EEPROM:
#include <EEPROM.h> int addr = 0; // current adress in the EEPROM void setup() { /** Empty setup. **/ } void loop() { /*** Need to divide by 4 because analog inputs range from 0 to 1023 and each byte of the EEPROM can only hold a value from 0 to 255. ***/ int val = analogRead(0) / 4; /*** Write byte val to addres addr of the EEPROM. ***/ EEPROM.write(addr, val); /*** Increment value addr to store next byte to the EEPROM. ***/ addr = addr + 1; if (addr == EEPROM.length()) { addr = 0; } delay(100); }
EEPROM.read() example
The second example code is implemented how to read a byte from the EEPROM memory using the EEPROM.read() function:
#include <EEPROM.h> int address = 0; byte value; void setup() { // initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Ardbox only } } void loop() { // read a byte from the current address of the EEPROM value = EEPROM.read(address); // print value to the serial port Serial.print(address); Serial.print("\t"); Serial.print(value, DEC); Serial.println(); address = address + 1; if (address == EEPROM.length()) { address = 0; } delay(500); }
EEPROM.put() example
The third Arduino EEPROM example code is implemented how to use the EEPROM.put(). EEPROM.put() is a function that allows writing into the EEPROM, but with EEPROM.put() you can write more complex data to the EEPROM memory like a structure, float, etc.:
#include <EEPROM.h> struct MyObject { float field1; byte field2; char name[10]; }; void setup() { Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Ardbox only } float f = 123.456f; //Variable to store in EEPROM. int eeAddress = 0; //Location we want the data to be put. //One simple call, with the address first and the object second. EEPROM.put(eeAddress, f); Serial.println("Written float data type!"); /** Put is designed for use with custom structures also. **/ //Data to store. MyObject customVar = { 3.14f, 65, "Working!" }; eeAddress += sizeof(float); //Move address to the next byte after float 'f'. EEPROM.put(eeAddress, customVar); Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!"); } void loop() { /* Empty loop */ }
EEPROM.get() example
The fourth example code is implemented how to use the EEPROM.get(). It is similar that the EEPROM.read(), but instead of reading one byte with EEPROM.get() you can get more complex data as a structure:
#include <EEPROM.h> void setup() { float f = 0.00f; //Variable to store data read from EEPROM. int eeAddress = 0; //EEPROM address to start reading from Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Ardbox only } Serial.print("Read float from EEPROM: "); //Get the float data from the EEPROM at position 'eeAddress' EEPROM.get(eeAddress, f); Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float. secondTest(); //Run the next test. } struct MyObject { float field1; byte field2; char name[10]; }; void secondTest() { int eeAddress = sizeof(float); //Move address to the next byte after float 'f'. MyObject customVar; //Variable to store custom object read from EEPROM. EEPROM.get(eeAddress, customVar); Serial.println("Read custom object from EEPROM: "); Serial.println(customVar.field1); Serial.println(customVar.field2); Serial.println(customVar.name); } void loop() { /* Empty loop */ }
See more information on the official Arduino IDE website
Touch up data from the EEPROM