Avatar

Write Anything to EEPROM Arduino

Oct 19th, 2020 | by: ELECTRONOOBS
Views 269
If you want to save permanent data to the Arduino taht won't be erased when you reset the microcontroller, well you have to store the data to the EEPROM, See how to do that. 

Simple Write to the EEPROM


//We use EEPROM.write function to store data to the memory. Each place is of 8 bits so values from 0 to 254. If you want to store a number higher than 254 you need to save the number into two positions and then merge the values together. For example, in the code below if you make EEPROM.write(0, number_to_save); since number_to_save is 375 it will store to the 0 position the number (375-254) = 121, which is the overflow from the maximum of 254. So we will have to find a solution of how to store higher numbers than 254 and we will se taht in the next part. 

Using EEPROM.read(), we can then read the byte of a specific position. Again, if our number is too high and divided into 2 or more bytes, we have to make multiple EEPROM.read() in order to get all the data. 
003c006400690076003e00230069006e0063006c00750064006500200026006c0074003b0045004500500052004f004d002e0068002600670074003b0026006e006200730070003b00200026006e006200730070003b002f002f005700650020006e006500650064002000740068006900730020006c006900620072006100720079003c002f006400690076003e003c006400690076003e00230069006e0063006c00750064006500200026006c0074003b00410072006400750069006e006f002e0068002600670074003b0026006e006200730070003b0020002f002f00200066006f00720020007400790070006500200064006500660069006e006900740069006f006e0073003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e0069006e00740020006e0075006d006200650072005f0074006f005f00730061007600650020003d0020003300370035003b003c002f006400690076003e003c006400690076003e0069006e007400200061006e006f0074006800650072005f006e0075006d0062006500720020003d0020003100380035003b003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e0076006f00690064002000730065007400750070002800290020007b0026006e006200730070003b00200026006e006200730070003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b0020002f002a00530069006e00630065002000770065002000630061006e002000730074006f0072006500200075007000200074006f0020003200350034002c002000740068006500200065006e007800740020006c0069006e0065002000770069006c006c002000740072007900200074006f007000200073007400720065002000610020006e0075006d0062006500720020006f006600200033003700350020006200750074002000690074002000630061006e00270074003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200053006f002c00200069006e007300740065006100640020006f00660020003300370035002000690074002000770069006c006c002000730074006f0072006500200028003300370035002d00320035003400290020003d002000310032003100200069006e00730074006500610064002a002f003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200045004500500052004f004d002e0077007200690074006500280030002c0020006e0075006d006200650072005f0074006f005f00730061007600650029003b0026006e006200730070003b00200026006e006200730070003b002f002f00530074006f007200650020007400650020006e0075006d006200650072005f0074006f005f00730061007600650020006e0075006d0062006500720020006f006e0020007400680065002000220030002200200070006f0073006900740069006f006e003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e0026006e006200730070003b0026006e006200730070003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b0020002f002a0042007500740020006e006f0077002c00200074006800650020006e0065007800740020006c0069006e0065002000770069006c006c002000740072007900200074006f00200073006100760065002000310038003500200061006e006400200074006800690073002000740069006d0065002000690074002000770069006c006c00200062006500200070006f0073007300690062006c0065002000730069006e00630065003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200031003800350020006900730020006c006f0077006500720020007400680061006e0020003200350034002e0020002a002f003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200045004500500052004f004d002e0077007200690074006500280031002c00200061006e006f0074006800650072005f006e0075006d0062006500720029003b0026006e006200730070003b00200026006e006200730070003b002f002f00530074006f0072006500200074006500200061006e006f0074006800650072005f006e0075006d0062006500720020006e0075006d0062006500720020006f006e0020007400680065002000220031002200200070006f0073006900740069006f006e003c002f006400690076003e003c006400690076003e007d003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e0076006f006900640020006c006f006f0070002800290020007b003c002f006400690076003e003c006400690076003e0026006e006200730070003b0020002f002a00570065002000720065006100640020007400680065002000760061006c00750065002000740068006100740020007700650020006800610064002000730074006f007200650020006f006e00200070006f0073006900740069006f006e0020003100200073006f0020007700690074006800200074006800650020006e0065007800740020006c0069006e00650020006d0079005f00760061006c00750065002000770069006c006c003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200062006500200065007100750061006c00200074006f0020003100380035002c0020007700680069006300680020006900730020007400680065002000760061006c0075006500200077006500200068006100760065002000730074006f0072006500640020006200650066006f007200650020006f006e00200070006f0073006900740069006f006e0020002200310022002e0020002a002f003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200069006e00740020006d0079005f00760061006c007500650020003d00200045004500500052004f004d002e0072006500610064002800310029003b003c002f006400690076003e003c006400690076003e007d003c002f006400690076003e

Write/Read Anything to the EEPROM


When saving a number from 0-255, EEPROM.write is sufficient. When saving a larger number, you have to call the EEPROM routines more than once, to save a "high byte" and a "low byte" for 16-bit numbers, or even more often for bigger numbers or other data types that cannot fit in one byte.

With the following code, you can write and read any data structure or variable, using any number of bytes of EEPROM to do it (of course, only as many bytes as your chip holds), all in a single call.

A common use of this would be to have a group of related "persistent" variables that you want to save for future sessions. This could be a high-score list in a game, or a set of configuration choices in a device like an alarm clock.

The "EEPROM_writeAnything" and "EEPROM_readAnything" functions will automatically jump from the first byte to the next and save or read the entire number, even if the value if higher than 254. What you have to amke sure is to not overlap the EEPROM positions. For example, if you store first the number 1200 on position 0, you have to know that the process will ocupy positions 0, 1, 2, 3 and 4. So, the enxt number must be stored on position 5 higher. 


FILES TO DOWNLOAD FOR THIS STEP
EEPROM Write anything code  DOWNLOAD
003c006400690076003e00230069006e0063006c00750064006500200026006c0074003b0045004500500052004f004d002e0068002600670074003b0026006e006200730070003b00200026006e006200730070003b002f002f0020005700650020006e006500650064002000740068006900730020006c006900620072006100720079003c002f006400690076003e003c006400690076003e00230069006e0063006c00750064006500200026006c0074003b00410072006400750069006e006f002e0068002600670074003b0026006e006200730070003b0020002f002f00200066006f00720020007400790070006500200064006500660069006e006900740069006f006e0073003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e002f002f005700650020006300720065006100740065002000740077006f0020006600750063006e00740069006f006e007300200066006f0072002000770072006900740069006e006700200061006e0064002000720065006100640069006e006700200064006100740061002000660072006f006d002000740068006500200045004500500052004f004d003c002f006400690076003e003c006400690076003e00740065006d0070006c00610074006500200026006c0074003b0063006c00610073007300200054002600670074003b00200069006e007400200045004500500052004f004d005f007700720069007400650041006e0079007400680069006e006700280069006e0074002000650065002c00200063006f006e007300740020005400260061006d0070003b002000760061006c007500650029003c002f006400690076003e003c006400690076003e007b003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b00200063006f006e0073007400200062007900740065002a002000700020003d002000280063006f006e0073007400200062007900740065002a002900280063006f006e0073007400200076006f00690064002a002900260061006d0070003b00760061006c00750065003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b00200075006e007300690067006e0065006400200069006e007400200069003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b00200066006f00720020002800690020003d00200030003b0020006900200026006c0074003b002000730069007a0065006f0066002800760061006c007500650029003b00200069002b002b0029003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200045004500500052004f004d002e00770072006900740065002800650065002b002b002c0020002a0070002b002b0029003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b002000720065007400750072006e00200069003b003c002f006400690076003e003c006400690076003e007d003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e00740065006d0070006c00610074006500200026006c0074003b0063006c00610073007300200054002600670074003b00200069006e007400200045004500500052004f004d005f00720065006100640041006e0079007400680069006e006700280069006e0074002000650065002c0020005400260061006d0070003b002000760061006c007500650029003c002f006400690076003e003c006400690076003e007b003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b00200062007900740065002a002000700020003d002000280062007900740065002a002900280076006f00690064002a002900260061006d0070003b00760061006c00750065003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b00200075006e007300690067006e0065006400200069006e007400200069003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b00200066006f00720020002800690020003d00200030003b0020006900200026006c0074003b002000730069007a0065006f0066002800760061006c007500650029003b00200069002b002b0029003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b0020002a0070002b002b0020003d00200045004500500052004f004d002e0072006500610064002800650065002b002b0029003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200026006e006200730070003b002000720065007400750072006e00200069003b003c002f006400690076003e003c006400690076003e007d003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e0076006f00690064002000730065007400750070002800290020007b003c002f006400690076003e003c006400690076003e0026006e006200730070003b002000530065007200690061006c002e0062006500670069006e002800390036003000300029003b0026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b002f002f005500730065002000730065007200690061006c00200074006f00200063006800650063006b002000740068006500200064006100740061003c002f006400690076003e003c006400690076003e0026006e006200730070003b0020002f002a0054006800650020006e0065007800740020006c0069006e0065002c002000770069006c006c002000730074006f007200650020007400680065002000310032003000300020006e0075006d0062006500720020006f006e00200070006f0073006900740069006f006e007300200030002c00200031002c00200032002c0020003300200061006e0064002000340020006f0066002000740068006500200045004500500052004f004d0020006100750074006f006d00610074006900630061006c006c0079003c002f006400690076003e003c006400690076003e0026006e006200730070003b002000730069006e00630065002000310032003000300020006400690076006900640065006400200062007900200032003500340020003d00200034002c003700200073006f002000690074002000770069006c006c002000660069007400200069006e00200035002000620079007400650073002e0020002a002f003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200045004500500052004f004d005f007700720069007400650041006e0079007400680069006e006700280030002c002000310032003000300029003b0026006e006200730070003b00200026006e006200730070003b0026006e006200730070003b003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e0026006e006200730070003b0020002f002a005700650020006b006e006f0077002000740068006100740020006200790074006500730020003000200074006f00200035002000610072006500200061006c007200650061006400790020006f006300750070006900650064002000620079002000700072006500760069006f007500730020006c0069006e006500200073006f00200077006500200077007200690074006500200074006800650020007300650063006f006e00640020006e0075006d0062006500720020007300740061007200740069006e00670026006e006200730070003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b002000660072006f006d00200070006f0073006900740069006f006e00200035002e0020005400680065002000660075006e006300740069006f006e002000770069006c006c0020006100750074006f006d00610074006900630061006c006c0079002000660069006c006c00200062007900740065007300200035002c00200036002c0020003700200061006e006400200073006f0020006f006e002e002e002e002a002f003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200045004500500052004f004d005f007700720069007400650041006e0079007400680069006e006700280035002c002000320030003000300029003b0026006e006200730070003b00200026006e006200730070003b003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e007d003c002f006400690076003e003c006400690076003e003c00620072003e003c002f006400690076003e003c006400690076003e0076006f006900640020006c006f006f0070002800290020007b003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200069006e00740020006d0079005f00760061006c00750065005f0031002c0020006d0079005f00760061006c00750065005f0032003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b0020002f002a005700690074006800200074006800650020006e0065007800740020006c0069006e00650020007700650020007200650061006400200074006800650020006e0075006d0062006500720020007300740061007200740069006e00670020006f006e00200070006f0073006900740069006f006e002000220030002200200061006e0064002000730074006f0072006500200069006e0074006f00200074006800650020006d0079005f00760061006c00750065005f00310020007600610072006900610062006c0065002e003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200053006f002c0020007700690074006800200074006800650020006e0065007800740020006c0069006e0065002c0020006d0079005f00760061006c00750065005f0031002000770069006c006c00200062006500200065007100750061006c00200074006f00200031003200300030002c0020007700680069006300680020006900730020007400680065002000760061006c0075006500200077006500200068006100650076002000730074006f0072006500640020006200650066006f007200650020002a002f003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200045004500500052004f004d005f00720065006100640041006e0079007400680069006e006700280030002c0020006d0079005f00760061006c00750065005f00310029003b0026006e006200730070003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b0026006e006200730070003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b0020002f002a005700690074006800200074006800650020006e0065007800740020006c0069006e00650020007700650020007200650061006400200074006800650020006e0075006d0062006500720020007300740061007200740069006e00670020006f006e00200070006f0073006900740069006f006e002000220035002200200061006e0064002000730074006f0072006500200069006e0074006f00200074006800650020006d0079005f00760061006c00750065005f00320020007600610072006900610062006c0065002e003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200053006f002c0020007700690074006800200074006800650020006e0065007800740020006c0069006e0065002c0020006d0079005f00760061006c00750065005f0031002000770069006c006c00200062006500200065007100750061006c00200074006f00200032003000300030002c0020007700680069006300680020006900730020007400680065002000760061006c0075006500200077006500200068006100650076002000730074006f0072006500640020006200650066006f007200650020002a002f003c002f006400690076003e003c006400690076003e0026006e006200730070003b00200045004500500052004f004d005f00720065006100640041006e0079007400680069006e006700280035002c0020006d0079005f00760061006c00750065005f00320029003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b0026006e006200730070003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b002000530065007200690061006c002e007000720069006e00740028002200560061006c0075006500200031003a002000220029003b0026006e006200730070003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b002000530065007200690061006c002e007000720069006e00740028006d0079005f00760061006c00750065005f00310029003b0026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b002f002f0043006800650063006b0020007400680065002000760061006c00750065003c002f006400690076003e003c006400690076003e0026006e006200730070003b002000530065007200690061006c002e007000720069006e0074002800220026006e006200730070003b00200026006e006200730070003b002000560061006c0075006500200032003a002000220029003b0026006e006200730070003b0026006e006200730070003b003c002f006400690076003e003c006400690076003e0026006e006200730070003b002000530065007200690061006c002e007000720069006e0074006c006e0028006d0079005f00760061006c00750065005f00320029003b0026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b00200026006e006200730070003b002f002f0043006800650063006b0020007400680065002000760061006c00750065003c002f006400690076003e003c006400690076003e007d003c002f006400690076003e

2 Comments

  • Dimitrios Chr. Ioannidis

    about 1 month ago

    Very nice ! How about a tutorial for the use of the EEMEM attribute and uploading eep files for reduced SRAM usage ?

    mo fateh

    about 18 days ago

    The EEPROM memory inside the ATmega microcontroller is sometimes called EEMEM It is the same

Login or Sign Up to post comments on this tutorial.