{"id":77038,"date":"2018-11-06T13:36:28","date_gmt":"2018-11-06T13:36:28","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=77038"},"modified":"2021-03-02T15:59:51","modified_gmt":"2021-03-02T15:59:51","slug":"esp32-flash-memory","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-flash-memory\/","title":{"rendered":"ESP32 Flash Memory &#8211; Store Permanent Data (Write and Read)"},"content":{"rendered":"\n<p>In this article we&#8217;ll show you how to store and read values from the ESP32 flash memory using Arduino IDE. The data saved in the flash memory remains there even when the ESP32 resets or when power is removed. As an example we\u2019ll show you how to save the last GPIO state.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/esp32-flash-memory-thumbnail.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Save permanent data on the ESP32 flash memory\" class=\"wp-image-77048\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/esp32-flash-memory-thumbnail.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/esp32-flash-memory-thumbnail.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/esp32-flash-memory-thumbnail.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/esp32-flash-memory-thumbnail.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntcred\"><strong>This tutorial is outdated<\/strong>. Follow the new tutorial instead: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-save-data-permanently-preferences\/\"><strong>[NEW]<\/strong> ESP32 Save Data Permanently using Preferences Library<\/a><\/p>\n\n\n\n<p>Before proceeding with this tutorial you should have the ESP32 add-on installed in your Arduino IDE. Follow one of the following tutorials to install the ESP32 on the Arduino IDE, if you haven\u2019t already.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Installing the ESP32 Board in Arduino IDE (Windows instructions)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-mac-and-linux-instructions\/\">Installing the ESP32 Board in Arduino IDE (Mac and Linux instructions)<\/a><\/li><\/ul>\n\n\n\n<p>We also recommend taking a look at the following resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-esp32\/\" target=\"_blank\" rel=\"noreferrer noopener\">Getting Started with ESP32 Dev Module<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Watch the Video Tutorial<\/h2>\n\n\n\n<p><em>This tutorial is available in video format (watch below) and in written format (continue reading).<\/em><\/p>\n\n\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/MxL1PqS2JR0?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To follow this tutorial you need the following components:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 DOIT DEVKIT V1 Board<\/a>&nbsp; \u2013 read <a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 Development Boards Review and Comparison<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noreferrer noopener\">5mm LED<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">330 Ohm resistor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/pushbuttons-kit\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pushbutton<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">10k Ohm resistor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Flash Memory<\/h2>\n\n\n\n<p>The data saved in the flash memory remains there even when the ESP32 resets or when power is removed. The flash memory is very similar to the EEPROM. Both are non-volatile memories.<\/p>\n\n\n\n<p>Saving data in the flash memory is specially useful to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>remember the last state of a variable;<\/li><li>save settings;<\/li><li>save how many times an appliance was activated;<\/li><li>or any other type of data that you need to have saved permanently.<\/li><\/ul>\n\n\n\n<p>One limitation with flash memory is the number of times you can write data to it. Data can be read from flash as many times as you want, but most devices are designed for about 100,000 to 1,000,000 write operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">EEPROM Library<\/h2>\n\n\n\n<p>To read and write from the ESP32 flash memory using Arduino IDE, we\u2019ll be using the EEPROM library. Using this library with the ESP32 is very similar to using it with the Arduino. So, if you\u2019ve used the Arduino EEPROM before, this is not much different.<\/p>\n\n\n\n<p>So, we also recommend taking a look at our article about <a href=\"https:\/\/randomnerdtutorials.com\/arduino-eeprom-explained-remember-last-led-state\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino EEPROM<\/a>.<\/p>\n\n\n\n<p>With the ESP32 and the EEPROM library you can use up to 512 bytes in the flash memory. This means you have 512 different addresses, and you can save a value between 0 and 255 in each address position.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/flash-memory-bytes.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 flash memory number of bytes available\" class=\"wp-image-77039\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/flash-memory-bytes.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/flash-memory-bytes.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Write<\/h3>\n\n\n\n<p>To write data to the flash memory, you use the <span class=\"rnthl rntliteral\">EEPROM.write()<\/span> function that accepts as arguments the location or address where you want to save the data, and the value (a byte variable) you want to save:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>EEPROM.write(address, value);<\/code><\/pre>\n\n\n\n<p>For example, to write 9 on address 0, you\u2019ll have:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>EEPROM.write(0, 9);<\/code><\/pre>\n\n\n\n<p>Followed by<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>EEPROM.commit();<\/code><\/pre>\n\n\n\n<p>For the changes to be saved.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Read<\/h3>\n\n\n\n<p>To read a byte from the flash memory, you use the <span class=\"rnthl rntliteral\">EEPROM.read()<\/span> function. This function takes the address of the byte you want to read as an argument.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>EEPROM.read(address);<\/code><\/pre>\n\n\n\n<p>For example, to read the byte stored previously in address 0, use:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>EEPROM.read(0);<\/code><\/pre>\n\n\n\n<p>This would return <strong>9<\/strong>, which is the value we stored in address 0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Remember Last GPIO State<\/h2>\n\n\n\n<p>To show you how to save data in the ESP32 flash memory, we\u2019ll save the last state of an output, in this case an LED. For example, imagine the following scenario:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>You\u2019re controlling a lamp with the ESP32<\/li><li>You set your lamp to turn on<\/li><li>The ESP32 suddenly loses power<\/li><li>When the power comes back on, the lamp stays off \u2013 because it doesn\u2019t keep its last state<\/li><\/ol>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"229\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/without-saving-in-flash.png?resize=750%2C229&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"output resets state after ESP32 restarts\" class=\"wp-image-77040\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/without-saving-in-flash.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/without-saving-in-flash.png?resize=300%2C92&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>You don\u2019t want this to happen. You want the ESP32 to remember what was happening before losing power and return to the last state.<\/p>\n\n\n\n<p>To solve this problem, you can save the lamp\u2019s state in the flash memory. Then, you just need to add a condition at the beginning of your sketch to check the last lamp state, and turn the lamp on or off accordingly.<\/p>\n\n\n\n<p>The following figure shows what we\u2019re going to do:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"228\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/save-in-flash.png?resize=750%2C228&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"keep output last state after reset ESP32\" class=\"wp-image-77041\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/save-in-flash.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/save-in-flash.png?resize=300%2C91&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic<\/h3>\n\n\n\n<p>Wire a pushbutton and an LED to the ESP32 as shown in the following schematic diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1038\" height=\"801\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/debounce_flash_memory_bb.png?resize=1038%2C801&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-81505\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/debounce_flash_memory_bb.png?w=1038&amp;quality=100&amp;strip=all&amp;ssl=1 1038w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/debounce_flash_memory_bb.png?resize=300%2C232&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/debounce_flash_memory_bb.png?resize=768%2C593&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/debounce_flash_memory_bb.png?resize=1024%2C790&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1038px) 100vw, 1038px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>Copy the following code to the Arduino IDE and upload it to your ESP32. Make sure you have the right board and COM port selected.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at http:\/\/randomnerdtutorials.com  \n*********\/\n\n\/\/ include library to read and write from flash memory\n#include &lt;EEPROM.h&gt;\n\n\/\/ define the number of bytes you want to access\n#define EEPROM_SIZE 1\n\n\/\/ constants won't change. They're used here to set pin numbers:\nconst int buttonPin = 4;    \/\/ the number of the pushbutton pin\nconst int ledPin = 16;      \/\/ the number of the LED pin\n\n\/\/ Variables will change:\nint ledState = HIGH;         \/\/ the current state of the output pin\nint buttonState;             \/\/ the current reading from the input pin\nint lastButtonState = LOW;   \/\/ the previous reading from the input pin\n\n\/\/ the following variables are unsigned longs because the time, measured in\n\/\/ milliseconds, will quickly become a bigger number than can be stored in an int.\nunsigned long lastDebounceTime = 0;  \/\/ the last time the output pin was toggled\nunsigned long debounceDelay = 50;    \/\/ the debounce time; increase if the output flickers\n\nvoid setup() { \n  Serial.begin(115200);\n  \n  \/\/ initialize EEPROM with predefined size\n  EEPROM.begin(EEPROM_SIZE);\n\n  pinMode(buttonPin, INPUT);\n  pinMode(ledPin, OUTPUT);\n\n  \/\/ read the last LED state from flash memory\n  ledState = EEPROM.read(0);\n  \/\/ set the LED to the last stored state\n  digitalWrite(ledPin, ledState);\n}\n\nvoid loop() {\n  \/\/ read the state of the switch into a local variable:\n  int reading = digitalRead(buttonPin);\n\n  \/\/ check to see if you just pressed the button\n  \/\/ (i.e. the input went from LOW to HIGH), and you've waited long enough\n  \/\/ since the last press to ignore any noise:\n\n  \/\/ If the switch changed, due to noise or pressing:\n  if (reading != lastButtonState) {\n    \/\/ reset the debouncing timer\n    lastDebounceTime = millis();\n  }\n\n  if ((millis() - lastDebounceTime) &gt; debounceDelay) {\n    \/\/ whatever the reading is at, it's been there for longer than the debounce\n    \/\/ delay, so take it as the actual current state:\n\n    \/\/ if the button state has changed:\n    if (reading != buttonState) {\n      buttonState = reading;\n\n      \/\/ only toggle the LED if the new button state is HIGH\n      if (buttonState == HIGH) {\n        ledState = !ledState;\n      }\n    }\n  }\n  \/\/ save the reading. Next time through the loop, it'll be the lastButtonState:\n  lastButtonState = reading;\n  \n  \/\/ if the ledState variable is different from the current LED state\n  if (digitalRead(ledPin)!= ledState) {  \n    Serial.println(&quot;State changed&quot;);\n    \/\/ change the LED state\n    digitalWrite(ledPin, ledState);\n    \/\/ save the LED state in flash memory\n    EEPROM.write(0, ledState);\n    EEPROM.commit();\n    Serial.println(&quot;State saved in flash memory&quot;);\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-Course\/raw\/master\/code\/EEPROM_Last_LED_State\/EEPROM_Last_LED_State.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Let\u2019s take a quick look at the code.<\/p>\n\n\n\n<p>This is a debounce code that changes the LED state every time you press the pushbutton. But there\u2019s something special about this code \u2013 it remembers the last LED state, even after resetting or removing power from the ESP32. Let\u2019s see what you have to do to make the ESP32 remember the last state of a GPIO.<\/p>\n\n\n\n<p>First, you need to include the EEPROM library.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;EEPROM.h&gt;<\/code><\/pre>\n\n\n\n<p>Then, you define the EEPROM size. This is the number of bytes you\u2019ll want to access in the flash memory. In this case, we\u2019ll just save the LED state, so the EEPROM size is set to 1.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define EEPROM_SIZE 1<\/code><\/pre>\n\n\n\n<p>We also define other variables that are required to make this sketch work.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ constants won't change. They're used here to set pin numbers: \nconst int buttonPin = 4; \/\/ the number of the pushbutton pin \nconst int ledPin = 16; \/\/ the number of the LED pin \n\n\/\/ Variables will change: \nint ledState = HIGH; \n\/\/ the current state of the output pin \nint buttonState; \/\/ the current reading from the input pin \nint lastButtonState = LOW; \/\/ the previous reading from the input pin \n\n\/\/ the following variables are unsigned longs because the time, measured in \n\/\/ milliseconds, will quickly become a bigger number than can be stored in an int. unsigned long lastDebounceTime = 0; \/\/ the last time the output pin was toggled unsigned long debounceDelay = 50; \/\/ the debounce time; increase if the output flickers<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the setup() you initialize the EEPROM with the predefined size.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>EEPROM.begin(EEPROM_SIZE);<\/code><\/pre>\n\n\n\n<p>To make sure your code initializes with the latest LED state, in the <span class=\"rnthl rntliteral\">setup()<\/span>, you should read the last LED state from the flash memory. It is stored on address zero.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>ledState = EEPROM.read(0);<\/code><\/pre>\n\n\n\n<p>Then, you just need to turn the LED ON or OFF accordingly to the value read from the flash memory.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>digitalWrite (ledPin, ledState);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>The following part of the <span class=\"rnthl rntliteral\">loop()<\/span> checks if the pushbutton was pressed and changes the <span class=\"rnthl rntliteral\">ledState<\/span> variable every time we press the pushbutton. To make sure we don\u2019t get false positives we use a timer. This snippet of code is based on the pushbutton debounce sketch example from the Arduino IDE.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ read the state of the switch into a local variable:\nint reading = digitalRead(buttonPin);\n\n\/\/ check to see if you just pressed the button\n\/\/ (i.e. the input went from LOW to HIGH), and you've waited long enough\n\/\/ since the last press to ignore any noise:\n\n\/\/ If the switch changed, due to noise or pressing:\nif (reading != lastButtonState) {\n  \/\/ reset the debouncing timer\n  lastDebounceTime = millis();\n}\n\nif ((millis() - lastDebounceTime) &gt; debounceDelay) {\n  \/\/ whatever the reading is at, it's been there for longer than the debounce\n  \/\/ delay, so take it as the actual current state:\n\n  \/\/ if the button state has changed:\n  if (reading != buttonState) {\n    buttonState = reading;\n\n    \/\/ only toggle the LED if the new button state is HIGH\n    if (buttonState == HIGH) {\n      ledState = !ledState;\n    }\n  }\n}\n\/\/ save the reading. Next time through the loop, it'll be the lastButtonState:\nlastButtonState = reading;<\/code><\/pre>\n\n\n\n<p>You simply need to save the LED state in the flash memory every time the LED state changes.<\/p>\n\n\n\n<p>We check if the state of the GPIO is different from the <span class=\"rnthl rntliteral\">ledState<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (digitalRead(ledPin)!= ledState) {<\/code><\/pre>\n\n\n\n<p>If it is, we\u2019ll change the LED state using the <span class=\"rnthl rntliteral\">digitalWrite()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>digitalWrite(ledPin, ledState);<\/code><\/pre>\n\n\n\n<p>And then, we save the current state in the flash memory. For that, we use <span class=\"rnthl rntliteral\">EEPROM.write()<\/span>, and pass as arguments the address position, in this case 0, and the value to be saved, in this case the <span class=\"rnthl rntliteral\">ledState<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>EEPROM.write(0, ledState);<\/code><\/pre>\n\n\n\n<p>Finally, we use the <span class=\"rnthl rntliteral\">EEPROM.commit()<\/span> for the changes to take effect.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>EEPROM.commit();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After uploading the code to your ESP32, press the pushbutton to turn the LED on and off. The ESP32 should keep the last LED state after resetting or removing power.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"415\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/keep-last-state-flash-memory.jpg?resize=750%2C415&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-77042\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/keep-last-state-flash-memory.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/keep-last-state-flash-memory.jpg?resize=300%2C166&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In summary, in this unit you\u2019ve learned how to save data in the ESP32 flash memory using the EEPROM library. Data saved on the flash memory remains there even after resetting the ESP32 or removing power.<\/p>\n\n\n\n<p>We have other articles about ESP32 that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/esp32-dual-core-arduino-ide\/\" target=\"_blank\">How to use ESP32 Dual Core with Arduino IDE<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/esp32-pwm-arduino-ide\/\" target=\"_blank\">ESP32 PWM with Arduino IDE<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\" target=\"_blank\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-deep-sleep-arduino-ide-wake-up-sources\/\">ESP32 Deep Sleep and Wake Up Sources<\/a><\/li><\/ul>\n\n\n\n<p><em>This is an excerpt from our course:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a>. If you like ESP32 and you want to learn more, we recommend enrolling in&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE course<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll show you how to store and read values from the ESP32 flash memory using Arduino IDE. The data saved in the flash memory remains there even &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 Flash Memory &#8211; Store Permanent Data (Write and Read)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-flash-memory\/#more-77038\" aria-label=\"Read more about ESP32 Flash Memory &#8211; Store Permanent Data (Write and Read)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":77048,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-77038","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/11\/esp32-flash-memory-thumbnail.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/77038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=77038"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/77038\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/77048"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=77038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=77038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=77038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}