I have a different strategy: I use flash writer ( preferences ) It is in the standard examples.
Very easy to use, but the 10.000 writes limit is relevant.
Then, only when the power-supply goes off, I do an actual “flash write.” Mostly I convert any variables that I want in my flash to a string. That takes much more memory, but there is generally more than enough memory space available. Strings also can be written directly. So on any project I always use the same trick for “static” memory.
Disadvantage is in floats: accuracy is reduced, but for counting the sum of liters beer I do not think it is relevant.
Power for the chip to do the write, you hold in a 1000uf capacitor with a diode to keep the 5V a second more.
On an analogue pin, with a voltage divider, you measure the power voltage before the diode. As soon as that collapses, you start the flash writing and other close down procedures.
Preference Example for 8 string:
add at start:
#include <Preferences.h>
Preferences flashwriter ;
String fStore[8]={"leeg","leeg","leeg","leeg", "leeg","leeg","leeg","leeg"} ; // array starts at 0 until 7n 0 tot 7
String kStore ; // key voor flash memory, integer is added at the end to complete the key
int i ; // general counter
Add at Setup:
for (i=0; i<=7; i++){ //can be used anywhere where you want to read the content or fill the string array
kStore= "kStore"+String(i)… ; // create key used to mark the flash memory location
flashwriter.begin(kStore.c_str(), false) ; // address name key can have max 15 characters
fStore[i] = flashwriter.getString(kStore.c_str(),"leeg"); // get String , when not available show leeg
flashwriter.end() ;
..Serial.println("Found in flash location"+String(i)+” : "+fStore[i]);
}
Add at onay location where you want to write to the flash:
String fStore(i) holds the string you want to save in flash on location i
kStore= "kStore"+String(i) ; // create key, where ‘i’ is string number
flashwriter.begin(kStore.c_str(), false) ; // open the address of name kStore#
flashwriter.putString(kStore.c_str(),fStore[i]) ; // actual writing
flashwriter.end() ;
..Serial.println("Stored in flash on location kStore”+String(i)+”: "+fStore[i]);
In general power supplies will have a significant capacitor on board. Often you do not need the 1000µF, as the power will degrade slowly. By the time voltage at A3 is <1.8V you know power is off/failing and you need the shut-down action.
Check whether Vraw of your processer board is not connected with the 5V of the USB entry. (like for multiple ESP32 types boards). Then using this approach would bring 12V to the USB powerline of your computer which is not compatible…. Then only a 5.5V power supply is allowed and your strictly need the diode: 5.5V to compensate for the voltage drop over the diode; diode to avoid the USB supply feeds back to the power in case that is not connected.
Obvious: as long as your processor board is also connected over USB, for instance to serve Serial communication, this is a risky approach.
I do use it with ESP32, with Serial communication but there are precautions:
- ESP32 only measures up to 3.3 V so power on the voltage divider should give not more than 3.3V ( in the example it is 2 volts.)
- For Serial communication to my laptop I use UART2 of the ESP32, with a separate TTL to USB board: [USB To TTL Serial Port Optoelectronic Isolation Module Type C H340]
- If you have to transport the signal a long distance ( >2meters) better to use RS485 or BT, and convert RS485 back to USB just before you PC/laptop. ( 19200 Baudrate for >20M)
With the flashwriter you might update your static memory like every hour, so that when something goes wrong you only miss the last hour….