element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Vertical Farming
  • Challenges & Projects
  • Design Challenges
  • Vertical Farming
  • More
  • Cancel
Vertical Farming
Blog Automated Green House Blog:9.2 - Arduino EEPROM [Has it ever been written]
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: m.ratcliffe
  • Date Created: 19 Nov 2015 7:57 PM Date Created
  • Views 1137 views
  • Likes 3 likes
  • Comments 3 comments
  • adapted_greenhouse
  • EEPROM
  • ardexpert
  • arduino
Related
Recommended

Automated Green House Blog:9.2 - Arduino EEPROM [Has it ever been written]

m.ratcliffe
m.ratcliffe
19 Nov 2015

Sometimes we want to store data on the arduino that will survive a reset, calibration settings and set points are an example of this. EEPROM is a non volatile memory on the arduino that we can write values to and read in the setup loop to keep the values after a reset.

 

There are many blogs that will talk about, writing and reading EEPROM, but not many [if any] talk about checking if the memory slot has ever been written to in the past. We want to be able to use the value of a variable [ie setpoint] from the eeprom if there is one there, if there is not we want to use a defined value from the sketch.

 

What this code does, it reads the eeprom to see if there is a value to read, if not it ignores the eeprom and substitutes the one from the top of the code.

 

here is a serial screenshot showing it in action, resetting makes it move onto the next memory slot:

 

image

 

 

Header 1

/*

Script to test how to check if eprom is written or not

it will start at memory slot 1 and print value if it has ever been written

it will then write the next empty slot to the value you put at the top of the code

 

 

   28/8/2015  Michael Ratcliffe  Mike@MichaelRatcliffe.com

   

   

          This program is free software: you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation, either version 3 of the License, or

    (at your option) any later version.

 

 

    This program is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.

 

 

    You should have received a copy of the GNU General Public License

    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

 

 

#include <EEPROM.h>

int backupvalue=512;

 

 

 

 

int i=1;

int value=100;

 

 

void setup() {

  Serial.begin(9600);

Serial.print("Arduino Reset button pressed");

  while (value>=2) {

     i++;

     value = EEPROM.read(i);

     Serial.print("Memory _Slot:");

     Serial.print(i);

     Serial.print("  has saved value:");

     Serial.println(value*4);

     delay(10);

 

 

     //We will stop at 100, you must have got the point by now and I dont know what happens if you write a eprom memory slot that is out of range

     if(i>=100) return;

  };

value=backupvalue/4;

    EEPROM.write(i, value);

 

 

  // turn the LED on when we're done

  digitalWrite(13, HIGH);

}

 

 

void loop() {

  /** Empty loop. **/

}

 

 

 

Want to clear your EEPROM for your next project, here is the code to do it:

 

Header 1

/*

* EEPROM Clear

*

* Sets all of the bytes of the EEPROM to 0.

* Please see eeprom_iteration for a more in depth

* look at how to traverse the EEPROM.

*

* This example code is in the public domain.

*/

 

 

#include <EEPROM.h>

 

 

void setup() {

 

 

  /***

    Iterate through each byte of the EEPROM storage.

 

 

    Larger AVR processors have larger EEPROM sizes, E.g:

    - Arduno Duemilanove: 512b EEPROM storage.

    - Arduino Uno:        1kb EEPROM storage.

    - Arduino Mega:       4kb EEPROM storage.

 

 

    Rather than hard-coding the length, you should use the pre-provided length function.

    This will make your code portable to all AVR processors.

  ***/

 

 

  for (int i = 0 ; i < EEPROM.length() ; i++) {

    EEPROM.write(i, 0);

  }

 

 

  // turn the LED on when we're done

  digitalWrite(13, HIGH);

}

 

 

void loop() {

  /** Empty loop. **/

}

 

 

This will come in handy in our later projects.

  • Sign in to reply
Parents
  • DAB
    DAB over 9 years ago

    Nice, I will take a look to see if this is something I can use.

     

    Thanks

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • DAB
    DAB over 9 years ago

    Nice, I will take a look to see if this is something I can use.

     

    Thanks

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • m.ratcliffe
    m.ratcliffe over 9 years ago in reply to DAB

    It comes in handy when you are putting a new system in but wont have time to calibrate it for a while, you can specify a good estimate that will keep it up and running  until calibration. It is only 8 bit memory slots, but the ide has a few other eprom functions to store/read longs etc. Hopefully I will find a way to do the same check with some longs.

     

    are any of your books on kindle?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 9 years ago in reply to m.ratcliffe

    No, we provide them in PDF and print.

     

    We feel that PDF is the most versatile format for most devices.

     

    Please note that Amazon does not support our PDF.  Go to Lulu.com to get them.

    Plus Amazon steals about 75% of our profit and charges us for the privilege.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube