#include <EEPROM.h>
int configured = 1;
int eeAddress = 0;
int isConfigured;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
EEPROM.put(eeAddress, configured);
isConfigured = EEPROM.read(eeAddress);
Serial.println(isConfigured);
}
My goal is to write a byte to EEPROM, and then read it and print it. However, for some reason, isConfigured is 0 even after EEPROM.read(eeAddress). Why would that be? Any help would be greatly appreciated. Thank you.
configured is an int. Depending on the processor, that could be 16 or 32 bits. Your code is putting that.
Then for reasons that are unclear to me you use read to fetch a byte back rather than using get to retrieve the entire value that was just written.
And you didn't mention what processor this is running on. Some EEPROM libraries, like on the ESP32 and ESP8266, require EEPROM.begin(size) and EEPROM.commit() calls.
Okay, thank you. I see in the example the size is set to 64. What unit of measurement is that, and is that the maximum size? And would that mean that using .put(), I can store data in locations 0-63?
Actually, EEPROM for the ESP32 is deprecated. See ESP32 EEPROM:
EEPROM is deprecated. For new applications on ESP32, use Preferences. EEPROM is provided for backwards compatibility with existing Arduino applications. EEPROM is implemented using a single blob within NVS, so it is a container within a container. As such, it is not going to be a high performance storage method. Preferences will directly use nvs, and store each entry as a single object therein.
Do you mean the size of an int in bytes or the maximum value of an int ?
The answers depend on which Arduino board you are using
For the Nano ESP32 an int of any kind, signed or unsigned, uses 4 bytes of memory and the range of values that a signed int can hold is -2,147,483,648 to 2,147,483,647
An unsigned int can hold values 0 to 4,294,967,295
1. The Nano ESP32 has no dedicated EEPROM like Arduino UNO. Some part of the non-volatile flash memory can be ivoked to behave as an EEPROM like the Arduini UNO and thus making it compatible with EEPROM.h Library. However, the following three extra commands are needed here (in the case of Nano ESP32).
EEPROM.begin(size); //amout of bytes requested ofr mulated EEPROM
EEPROM.commit(); //must be inseted after EEPROM.write()/put() command
EEPROM.end(); // must be executed to release the EEPROM back to flash memory
2. Determing size of int data type in Nano ESP32 Platform (1) Nano ESP32 uses a 32-bit MCU. Let us see, how many 8-byte wide memory locations are allocated for an int type variable by the Compiler? The following test sketch says that it is 4-byte. Test Sketch: