Storing settings between restarts in EEPROM on Arduino

Posted on Jan 2, 2012 in Arduino, C++, Programming, UberFridge | 3 comments

In my UberFridge project I have a self learning algorithm to control the fridge temperature.
Two parameters to estimate heating and cooling overshoot are adjusted continuously. I don’t want the Arduino to forget what it has learned every time it is reset, so I store these settings in EEPROM.

The Atmega328 has 1K of EEPROM, which is plenty to store a few settings. The EEPROM is byte addressable, so I had to write a few functions to read and write integers and floats.

Keep in mind that the EEPROM has only 100.000 write/erase cycles, so do not write to it too often. I write my settings to the EEPROM everytime they are updated, but before I write I check whether the value is the same as the one already in EEPROM to prevent unnecessary writes.

Here are my functions to store and reload settings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <EEPROM.h>
#include "globals.h"
 
int eepromReadInt(int address){
   int value = 0x0000;
   value = value | (EEPROM.read(address) << 8);
   value = value | EEPROM.read(address+1);
   return value;
}
 
void eepromWriteInt(int address, int value){
   EEPROM.write(address, (value >> 8) & 0xFF );
   EEPROM.write(address+1, value & 0xFF);
}
 
float eepromReadFloat(int address){
   union u_tag {
     byte b[4];
     float fval;
   } u;   
   u.b[0] = EEPROM.read(address);
   u.b[1] = EEPROM.read(address+1);
   u.b[2] = EEPROM.read(address+2);
   u.b[3] = EEPROM.read(address+3);
   return u.fval;
}
 
void eepromWriteFloat(int address, float value){
   union u_tag {
     byte b[4];
     float fval;
   } u;
   u.fval=value;
 
   EEPROM.write(address  , u.b[0]);
   EEPROM.write(address+1, u.b[1]);
   EEPROM.write(address+2, u.b[2]);
   EEPROM.write(address+3, u.b[3]);
}
 
void saveSettings(void){
  // write new settings to EEPROM to save be able to reload them after a reset
  // Check if settings in EEPROM are still  correct to keep writes minimal (only 100.000 cycles)
  if(mode!=EEPROM.read(EEPROM_MODE)){
    EEPROM.write(EEPROM_MODE,mode);
  }
  int beerTemperatureSettingInt = int(beerTemperatureSetting+.5);
  if(beerTemperatureSettingInt != eepromReadInt(EEPROM_BEER_SETTING)){
    eepromWriteInt(EEPROM_BEER_SETTING,beerTemperatureSettingInt);
  }
  int fridgeTemperatureSettingInt = int(fridgeTemperatureSetting+.5);
  if(fridgeTemperatureSettingInt != eepromReadInt(EEPROM_FRIDGE_SETTING)){
    eepromWriteInt(EEPROM_FRIDGE_SETTING,fridgeTemperatureSettingInt);
  }
  if(heatOvershootEstimator != eepromReadFloat(EEPROM_HEAT_ESTIMATOR)){
     eepromWriteFloat(EEPROM_HEAT_ESTIMATOR, heatOvershootEstimator);
  }
  if(coolOvershootEstimator != eepromReadFloat(EEPROM_COOL_ESTIMATOR)){
     eepromWriteFloat(EEPROM_COOL_ESTIMATOR, coolOvershootEstimator);
  }
}
 
void loadSettings(void){
  mode = EEPROM.read(EEPROM_MODE);
  if(mode > BEER_PROFILE){
    //setting in EEPROM is invalid
    mode = BEER_CONSTANT;
  }
  beerTemperatureSetting = eepromReadInt(EEPROM_BEER_SETTING);
  if(beerTemperatureSetting > 300 | beerTemperatureSetting <0){
    //setting in EEPROM is invalid
    beerTemperatureSetting = 200;
  }
  fridgeTemperatureSetting = eepromReadInt(EEPROM_FRIDGE_SETTING);  
  if(fridgeTemperatureSetting > 300 | fridgeTemperatureSetting <0){
    //setting in EEPROM is invalid
    fridgeTemperatureSetting = 200;
  }
  heatOvershootEstimator = eepromReadFloat(EEPROM_HEAT_ESTIMATOR);
  if(heatOvershootEstimator < 0 || heatOvershootEstimator > 1000 || heatOvershootEstimator==0x0000){ //incorrect value in EEPROM
     heatOvershootEstimator=0.2;
  }  
  coolOvershootEstimator = eepromReadFloat(EEPROM_COOL_ESTIMATOR);
  if(coolOvershootEstimator < 0 || coolOvershootEstimator > 1000 || coolOvershootEstimator==0x0000){ //incorrect value in EEPROM{
    coolOvershootEstimator=5;
  }  
}

3 Comments

  1. Thanks for this snippit. A real help!

  2. Thanks for the example! I thought this bit shifting trick would work for storing larger values, but wasn’t sure as I’m pretty new to this coding thing. Now I can play around with storing settings! Thanks again!

    • In my BrewPi project, I use an even easier way. Put everything you want to store in a struct, then do this:

      #include <avr/eeprom.h>
      void TempControl::storeConstants(void){
      	eeprom_update_block((void *) &cc, (void *) EEPROM_CONTROL_CONSTANTS_ADDRESS, sizeof(ControlConstants));
      }
       
      void TempControl::loadConstants(void){
      	eeprom_read_block((void *) &cc, (void *) EEPROM_CONTROL_CONSTANTS_ADDRESS, sizeof(ControlConstants));
      }

Leave a Reply

Your email address will not be published. Required fields are marked *