ESP32 EEPROM

I’m trying to write some values to the EEPROM on an esp32 but I just get zeros after reboot up.
I’ve added a simple little boot up routine so it only outputs the data once on the serial monitor.
This is the first program I upload.

//first upload 
#include <Arduino.h>
#include "EEPROM.h"
float Variable_1 = 12.36; // dummy value
float Variable_2 = 10.36; //Dummy value
int Eaddress = 0;
boolean send_data = true;

void setup() {
  Serial.begin(115200);
   EEPROM.writeFloat(Eaddress, Variable_1);//store the vatibale
  Eaddress += sizeof(float);
  EEPROM.writeFloat(Eaddress, Variable_2);//store the vatibale
  EEPROM.commit();
  delay(500);
  //read the variables back
  EEPROM.begin(Eaddress);
  EEPROM.get(Eaddress, Variable_1);
  Eaddress += sizeof(float);
  EEPROM.get(Eaddress, Variable_2);
}

void loop() {
  if (send_data == true){
    Serial.print("VOLTS SCALE");
    Serial.println(Variable_1);
    Serial.print("VOLTS CAL: ");
    Serial.println(Variable_2);
    send_data = false;
  }

}

This is the second code I upload just to read the EEPROM on boot up and send data to Serial port but all I get is 0.00 for both values.

//Second up load
#include <Arduino.h>
#include "EEPROM.h"
float Variable_1; // dummy value
float Variable_2 ; //Dummy value
int Eaddress = 0;
boolean send_data = true;

void setup() {
  Serial.begin(115200);
  //read the variables back
  EEPROM.begin(Eaddress);
  EEPROM.get(Eaddress, Variable_1);
  Eaddress += sizeof(float);
  EEPROM.get(Eaddress, Variable_2);
}

void loop() {
  if (send_data == true) {
    Serial.print("VOLTS SCALE");
    Serial.println(Variable_1);
    Serial.print("VOLTS CAL: ");
    Serial.println(Variable_2);
    send_data = false;
  }

}

I may be doing something wrong as this is the first time having ago writing to the EEProm on a EPS32

In your first sketch, you must call EEPROM.begin() before writing to it.
And EEPROM.begin() takes a size as argument, not an address. In your sketches, Eaddress has value zero when you call begin, effectively hindering any use, as the size is zero.
For example, try using EEPROM.begin(1024), or any other value that fits your needs, up to 4096 bytes.

Thanks for that that’s working ok now, But I’ve now tried to correct my other code where I have a little menu system and once I get to the last menu 4 it should write the new values to the EEProm but it does not seem to.

I’ve done a demo version where I’ve cut the code down and added a little counter to show that something is happening, I press the menu button, This then jumps into the menu I scroll though adjust to values and then restart the system and and the new values I’ve just changed have not been stored.

Here’s my test code in case I’ve down something wrong or missed something

#include <Arduino.h>
#include "EEPROM.h"
#include <U8g2lib.h>
#include <SPI.h>
#include <TimedAction.h>
#include <Wire.h>              // SDA=pin4, SCL=pin5
#include <MCP342x.h>
float Volts_Cal ;
float Volt_scale; //Change this if the incoming reading with nothing connected on the input terminals
int Eaddress = 0;
int menubuttonPushCounter = 0;
int menubuttonState = 0;
int lastmenuButtonState = 0;
boolean menubuttonbool = false;
int upbuttonState;
int lastupbuttonState;
int upbutton;
int downbuttonState;
int lastdownbuttonState;
int downbutton;
int menucounter = 0;
long counting = 0 ;
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;
boolean send_data = true;
#define  menubuttonPin 36//pin to which the button is conected to
#define downbuttonpin 39//Pin to which downbutton is conected to.
#define upbuttonpin 34//Pin to which upbutton is conected to.
//############## LCD Pins ####################################
U8G2_ST7920_128X64_2_HW_SPI u8g2(U8G2_R2, /* CS=*/ 17); // Feather
//####################################################
//#  Initializes TimedAction - Timer Interrupt       #
//####################################################
void TimerService01();// Timer service routinte
TimedAction Timedact01 = TimedAction(80, TimerService01);// Timer1 for ADC'S, called every 80 Ms seconds

void setup() {
  Serial.begin(115200);
  u8g2.begin();
  pinMode(menubuttonPin, INPUT);
  digitalWrite(menubuttonPin, HIGH);
  pinMode(downbuttonpin, INPUT);
  digitalWrite(downbuttonpin, HIGH);
  pinMode(upbuttonpin, INPUT);
  digitalWrite(upbuttonpin, HIGH);
  u8g2.setColorIndex(1);        // pixel on
  u8g2.setFontRefHeightExtendedText();
  u8g2.clear();
  //read the variables back
  EEPROM.begin(1024);
  EEPROM.get(Eaddress, Volt_scale);
  Eaddress += sizeof(float);
  EEPROM.get(Eaddress, Volts_Cal);
  Eaddress += sizeof(float);
}

void loop() {
  if (send_data == true) {
    Serial.print("VOLTS SCALE");
    Serial.println(Volt_scale, 3);
    Serial.print("VOLTS CAL: ");
    Serial.println(Volts_Cal, 2);
    send_data = false;
  }
  unsigned long currentMillis = millis();
  // just so I know it working
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    counting ++;
  }
  menubuttonState = digitalRead(menubuttonPin);
  if (menubuttonState != lastmenuButtonState)
  {
    if (menubuttonState == LOW)
    {
      Serial.println("MENU");
      delay(100);
      menubuttonPushCounter = 4;
      menubuttonbool = true;
    }
    if (menubuttonState == HIGH)
    {
      menubuttonbool = false;
    }
  }
  if (menubuttonbool == true && menubuttonPushCounter == 4)
  {
    menucounter = 0;
    menu();
  }
  lastmenuButtonState = menubuttonState;
  Timedact01.check();
}
///############ Displays the Menu ##########################################
int menu()
{
  Timedact01.check();

  delay(1000);
  while (menucounter < 4) // Runs in a loop while menucounter is less than 4
  {
    menubuttonState = digitalRead(menubuttonPin);
    // scrollbuttonState = digitalRead(scrollbuttonpin);
    upbutton = digitalRead(upbuttonpin);
    downbutton = digitalRead(downbuttonpin);

    if (menubuttonState != lastmenuButtonState)
    {
      // u8g2.firstPage();          // Update LCD 1
      // do {
      //   u8g2.clear();
      // } while ( u8g2.nextPage() );
      if (menubuttonState == LOW)
      {
        delay(200);
        menucounter++;

      }
    }
    lastmenuButtonState = menubuttonState;

    switch (menucounter) {
      case 0:
        u8g2.firstPage();          // Update LCD 1
        do {
          u8g2.setFont(u8g_font_6x10);
          u8g2.drawStr(7, 5, "CALIBRATE VREF CH1");
          u8g2.setFont(u8g2_font_fub11_tf);
          u8g2.setCursor(40, 35);
          u8g2.print(Volt_scale, 5); // 3 dp's
          if (upbutton == LOW) {
            Volt_scale = Volt_scale + 0.00001; //(Volt_scale, 5); //Volts_disp_Ch1
            delay(200);
          }
          if (downbutton == LOW)
          {
            Volt_scale = Volt_scale - 0.00001;;
            delay(200);
          }
        } while ( u8g2.nextPage() );
        break;
      //####### voltage calibration screen #########################
      case 1:
        u8g2.firstPage();          // Update LCD 1
        do {
          u8g2.setFont(u8g_font_6x10);
          u8g2.drawStr(7, 5, "CALIBRATE VOLTS CH1");
          u8g2.setFont(u8g2_font_fub11_tf);
          u8g2.setCursor(40, 35);
          u8g2.print(Volts_Cal, 1); // 3 dp's
          if (upbutton == LOW) {
            Volts_Cal = Volts_Cal + 0.1;
            delay(200);
          }
          if (downbutton == LOW)
          {
            Volts_Cal = Volts_Cal - 0.1;;
            delay(10);
          }
        } while ( u8g2.nextPage() );
        break;
      //####### current calibration screen #########################
      case 2:
        u8g2.firstPage();          // Update LCD 1
        do {
          u8g2.setFont(u8g2_font_fub17_tn);
          u8g2.drawStr(22, 33, "MENU 3");
        } while ( u8g2.nextPage() );
        break;
      //####### current calibration screen #########################
      case 3:
        u8g2.firstPage();          // Update LCD 1
        do {
          u8g2.setFont(u8g_font_6x10);
          u8g2.drawStr(22, 33, "MENU 4");
        } while ( u8g2.nextPage() );

        break;


      //########## Stores the set values and reads the new values #######################
      case 4:

        u8g2.firstPage();          // Update LCD 1
        do {
          u8g2.setFont(u8g_font_6x10);
          u8g2.drawStr(7, 5, "STORE VALUES");
          menucounter = 4;
          menubuttonPushCounter = 0;
        } while ( u8g2.nextPage() );
        EEPROM.begin(1024);
        EEPROM.writeFloat(Eaddress, Volt_scale);//store the vatibale
        Eaddress += sizeof(float);
        EEPROM.writeFloat(Eaddress, Volts_Cal);//store the vatibale
        Eaddress += sizeof(float);
        EEPROM.commit();
        delay(1000);
        break;
    }
  }

}
void LCD1updatedraw() {

  // Write large text to LCD (col, row, text), Sizes = 11,14,17,20,25,30,35,42,49
  u8g2.setFont(u8g2_font_fub11_tn);
  u8g2.setCursor(10, 2);
  u8g2.setFontPosTop();
  u8g2.print(counting); // 3 dp's
}
void TimerService01() {
  // Service LCDs
  u8g2.firstPage();          // Update LCD 1
  do {
    LCD1updatedraw();
  } while ( u8g2.nextPage() );
}

you keep increasing Eaddress. You need to read and write always from the same places

You are not resetting the value of Eaddress to zero before storing the values so they are not stored where you think they are. Why call EEPROM.begin() twice in the program ?

Why are you using EEPROM.writeFloat() rather than EEPROM.put() ?

J-M-L:
you keep increasing Eaddress. You need to read and write always from the same places

Thank you that solved my problem. I did not realise I missed or had to add that

UKHeliBob:
You are not resetting the value of Eaddress to zero before storing the values so they are not stored where you think they are. Why call EEPROM.begin() twice in the program ?

Why are you using EEPROM.writeFloat() rather than EEPROM.put() ?

I just found out I missed the not resting Eaddress to 0, I forgot to remove the second one as I was trying different things to resolve it,
Does the EEPROM.put() store longs,floats and int's then ?

Once again thank you all, been a great help

Why not check the reference?