preferences.ino
The ESP32 has no EEPROM but an NVS partition in flash: the Preferences library files key-values there with wear spreading. This sketch persists a setting editable over serial — the clean base of a product configuration.
Hardware
An Arduino Nano ESP32 (dual-core ESP32-S3, 2.4 GHz Wi-Fi + Bluetooth Low Energy, Nano-compatible physical pinout, native USB, 3.3 V pins only). Bare Nano ESP32; NVS is a partition of the onboard flash (up to ~200 KB per partitioning).
Wiring
No wiring; test "seuil=700" on the serial monitor then reboot.
How it works
prefs.begin(namespace, readOnly) opens a sealed store; put* writes to flash with internal wear-leveling. get* with default value covers first boot.
Running it
Install the Arduino ESP32 Boards package (or follow the official Nano ESP32 guide), select Arduino Nano ESP32, upload. Pins are 3.3 V: never feed 5 V into an input. The serial monitor uses native USB.
Variants
Keep WiFi passwords in a separate namespace, or wipe all (prefs.clear()) via a button held at boot for a factory reset.
preferences.ino
// La NVs de l'ESP32 (partition flash) remplace l'EEPROM des AVR.
// La lib Preferences offre un key-value persistant simple.
#include <Preferences.h>
Preferences prefs;
void setup() {
Serial.begin(115200);
prefs.begin("config", false); // namespace ferme
String ssid = prefs.getString("ssid", "");
int seuil = prefs.getInt("seuil", 512);
bool premier = prefs.getBool("deja", false);
if (!premier) {
Serial.println(F("Premier demarrage : reglages par defaut"));
prefs.putString("ssid", "MonReseau");
prefs.putInt("seuil", 512);
prefs.putBool("deja", true);
}
Serial.print(F("ssid=")); Serial.print(ssid);
Serial.print(F(" seuil=")); Serial.println(seuil);
prefs.end();
}
void loop() {
static String ligne = "";
while (Serial.available()) {
char c = Serial.read();
if (c == '\n') {
if (ligne.startsWith("seuil=")) {
prefs.begin("config", false);
prefs.putInt("seuil", ligne.substring(6).toInt());
prefs.end();
Serial.println(F("persiste"));
}
ligne = "";
} else ligne += c;
}
}