Given in EEPROM returns to FF

Guys, I made a program for the Arduino in which it reads a value from the eeprom and with this value I make a calculation, this value is assigned by a nextion ihm and whenever it changes in the eeprom.

It very rarely happens that when the Arduino is powered, this eeprom value that was previously at 100, 50 or any other value out of nowhere is displayed with 255. It is as if the eeprom was turned off.

Is there anything that could cause this?

If there is Arduino/Nextion communication involved in the process of assigning a value to the variable to be stored, a failure in the reading/writing could lead to a bad value in the arduino eeprom. How do you confirm that the value from the Nextion is correct?

The value is read from the eeprom and displayed on the display and I have a button to increase and decrease the value, I always use the value within the range of 0 to 100 and every time the value is changed it is written to the eeprom. What is strange and rarely happens is that sometimes the value shown on the display is 255 (FF).

Please post some simple example code for the Arduino and Nextion which demonstrates the problem.

Are you talking about the eeprom on the Arduino?
Which Arduino are you using?

Some Nextions have eeproms as well. Is that the case with your issue?

I'm using Arduino Uno and its EEPROM

#define EEPROM_ADDRESS_1 1
#define EEPROM_ADDRESS_2 2


volatile byte DadoSerial;
volatile byte Setpoint = 0;
int SetpointAjustado = 0;
volatile byte AjusteSetpoint = 0;
int ValorPWM = 0;


void setup() {

  Setpoint = EEPROM.read(EEPROM_ADDRESS_1);      
  AjusteSetpoint = EEPROM.read(EEPROM_ADDRESS_2);  

  Serial.print("page1.set.val=")
  Serial.print(Setpoint);
  Serial.write(0xFF);  
  Serial.write(0xFF);
  Serial.write(0xFF);

  Serial.print("page6.n0.val=");
  Serial.print(AjusteSetpoint);
  Serial.write(0xFF);
  Serial.write(0xFF);
  Serial.write(0xFF);


}





void loop() {

  EscritaSerial();  
  PWM();              
}








void LeituraSerial() {
  if (Serial.available() > 1) {
    DadoSerial = Serial.read();  

    if (DadoSerial == 0xAA) {
      DadoSerial = Serial.read();  
      wdt_reset();                

      if (DadoSerial == 0x84) {
        if (AjusteSetpoint > 0) {
          AjusteSetpoint--;

          Serial.print("n0.val="); 
          Serial.print(AjusteSetpoint);
          Serial.write(0xFF);  
          Serial.write(0xFF);
          Serial.write(0xFF);

          EEPROM.write(EEPROM_ADDRESS_2, AjusteSetpoint); 
        }
      }


      if (DadoSerial == 0x85) {
        if (AjusteSetpoint < 100) {
          AjusteSetpoint++;

          Serial.print("n0.val=");  
          Serial.print(AjusteSetpoint);
          Serial.write(0xFF);  
          Serial.write(0xFF);
          Serial.write(0xFF);

          EEPROM.write(EEPROM_ADDRESS_2, AjusteSetpoint);  
        }
      }
    }
  }
}


void PWM() {
  SetpointAjustado = (Setpoint * AjusteSetpoint) / 100;
  ValorPWM = map(SetpointAjustado, 0, 100, 0, 255);  
  ValorPWM = constrain(ValorPWM, 0, 255);            

  analogWrite(PD5, ValorPWM);  



What Arduino do you have? That is a common problem with the ESP Arduinos as the EEPROM is non existent but simulated in RAM and saved in flash. You need to commit the EEPROM to flash with this statement"
EEPROM.commit();, for more information check ESP32 Flash Memory - Save Permanent Data | Random Nerd Tutorials

Your code does not compile.
What Serial baud rate are you using?

What is strange and rarely happens is that sometimes the value shown on the display is 255 (FF).

When you post more complete code, can you identify the eeprom read or write which gives the wrong value on the display?

Is it the eeprom read and Nextion writes in setup()?

I had the same issue you described and in my case the cause was the corruption of the EEPROM during power cycling.
The solution was to enable brown-out detection by changing the fuses of the MCU, this ensures the CPU turns off when the power goes too low to prevent erratic execution of code.

What values ​​should you put in the BOD?

Any BOD voltage setting should be fine to protect the EEPROM data.
From the Table 28-5 you could try both 2.7V setting and 4.3V setting and tell us your experience. Assuming the BOD has anything to do with your EEPROM problem.
https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.